Scroll
Drupal 8 Get field value programmatically
Drupal 8 Get field value programmatically
Drupal 8 Get field value programmatically
1 answer
votes: 1368
Answer
You do not need to convert $entity
into an array, this would simply work.
$entity->get('field_name')->getValue();
FYI : Using kint()
from the devel_kint
module will let you know all the available methods that could be used to access the object elements, so comparatively var_dump()
is less helpful.
$node = \Drupal\node\Entity\Node::load($nid);
$title_field = $node->get('title');
$title = $title_field->value;
We don't really need to call get()
use Drupal\node\Entity\Node
$title = Node::load($nid)->title->value;