node_load((int) $nid)
loads a fully-populated node entity. $node->title->value
gets the title, but how do I access field values in Drupal 8?
12 Answers
I guess you do not need to convert $entity
into an array,
this would simply work.
$entity->get('field_name')->getValue();
Alternatively, we can make it easier to read as follows:
$entity->field_name->value;
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.
-
24Actually it should be: $entity->get('field_name')->value; Commented Mar 10, 2016 at 23:12
-
1Do you know why this is @EdHollinghurst? getValue does indeed error for me, but (using dpm) it is listed in the available methods, whereas 'value' is not. Commented Apr 8, 2016 at 10:45
-
2
getValue()
works for me (both on 8.1.x and 8.2.x) see api reference– leon.nkCommented May 19, 2016 at 4:48 -
27
->getValue()
is generic and will works regardless of the field type. If you use->value
, it only works if the field value is stored in avalue
property. It will not work for more complex types (eg. date or entity reference). Commented Sep 6, 2016 at 18:13 -
7This code alone will throw an error if the entity doesn't have a field named "field_name". Make sure this code only gets run on entities where you know that field exists, you can use $entity->hasField('field_name') to test, or if (!empty($entity->field_name))– oknateCommented Apr 19, 2017 at 18:45
in drupal 8 , $node being of class node
$node->field_machine_name->getValue();
or
$node->get('field_machine_name')->getValue();
will return an array of values.
If you expect only one, you might as well stringify the array with
$node->field_machine_name->getString();
or
$node->get('field_machine_name')->getString();
-
You can directly access values with a specific delta:
$node->field_name[0]->value
– imcleanCommented Jan 17, 2020 at 3:16 -
This worked for single value fields: $node->get('field_machine_name')->getString(); Commented Mar 12, 2022 at 16:35
-
Surely best practice to always use a
get
method rather than access the property directly (->value
)– JonoCommented Jul 26, 2023 at 14:58
For fields you should be able to use $node->body->value
for example. If you don't know what the field names are I recommend installing the Devel module for Drupal 8 and inspecting the node via like dsm($node)
.
-
1
-
1
-
1@Alex I suggest you ask a new question with the problem you're having. At the time this was an ok answer, it seems disengenous to edit the answer now that is an answer on this page that has more points than this one.– tenkenCommented May 20, 2016 at 14:45
-
6Check this article for a nice explanation. You can either use $node->field_name->value to get a single value (or first key of multi-value field). Or you can use $node->field_name->getValue() to get all the values of a field as an array.– MirsoftCommented Jul 12, 2016 at 9:53
-
2@tenken: Actually, updating an existing answer is preferable over adding a redundant question with new answers. Commented Sep 6, 2016 at 18:04
You can always do $entity->toArray()
to see what's in an entity. Even when using xdebug (for example with phpstorm) making sense of an entity object on its own is very hard. I doubt dsm($node)
in Drupal 8 makes any sense. If you insist on in-browser debugging (but you shouldn't, Drupal 8 is too complex to debug without xdebug) dsm($node->toArray())
is what you want.
Use following code snippet to get node field values
This is used to get node id from url
$nid = \Drupal::routeMatch()->getRawParameter($node_id);
To load node data using nid
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
This is used to get field value.
$terms = $node->get('field_Name')->getValue();
This is used to get node title.
$product_name = $node->getTitle();
-
2Load the node like this: $nid = \Drupal::routeMatch()->getRawParameter('node'); $node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);– rémyCommented Oct 28, 2016 at 8:40
It's simply:
$node->get('field_name')->getValue();
If the field is an entity reference field, there are some special ways to get the referenced entities.
To get the target id (first element):
$entity->get('field_entity_reference')->target_id;
To get the loaded entity (first element):
$entity->get('field_entity_reference')->entity;
When using ->entity
on the field it automatically returns the loaded entity.
If the field can contain multiple references, you can fetch them all like this:
$entities = $entity->get('field_entity_reference')->referencedEntities();
That will return an array of entities.
For me, having an $entity
(a node) this worked:
$entity->field_name->getValue();
using 8.1.
-
2This one gets all the values of a multi-value field into an array. It can be of course used to get value of single-value field too, however the output will then contain an array with a single value. To get a single value of a field (or first value of a multi-value field), use $entity->field_name->value– MirsoftCommented Jul 12, 2016 at 9:58
While using (the actual correct way):
$node = \Drupal\node\Entity\Node::load($nid);
$node->get('nid')->value; // returns a raw value - Node ID
// Following both returns array structure containing value/target id nested value.
$node->get('nid')->value();
$node->nid->getValue();
use Drupal\node\Entity\Node
$node = Node::load($nid);
$title = $node->get('title')->value;
Or
use Drupal\node\Entity\Node
$title = Node::load($nid)->get('title')->value;
Here is nice write up about accessing values from the node object https://www.frobiovox.com/posts/2016/03/28/simplify-drupal-8-field-value-calls.html
When not using nodes but entities (like the OP asked) things get a little more murky. For example when using an address entity it only took me 8 tries to get the organization value.
$v1 = $address->get('field_address_organization')->getString();
$v2 = $address->get('organization')->getString();
$v3 = $address->get('field_organization')->getString();
$v4 = $address->field_organization->getValue();
$v5 = $address->organization->getString();
$v6 = $address->field_organization->getString();
$v7 = $address->organization->getValue();
$v8 = $address->organization;
The only one that didn't throw an exception was the last one. And I'm lying when I say it only took me 8 tries to get the right answer - but 8 is all I'll admit to publicly. :)
I had to get the address entity and render it programmatically. After a lot of searching and debugging, this is what worked best for me
if ($field_name == 'field_address') {
$render_array = $entity->get($field_name)->view('address_plain');
$body .= \Drupal::service('renderer')->renderRoot($render_array);
}
As someone already mentioned you can do something like this to get the value if you have a Drupal DateRange field:
$node->field_date_range->value
That will return the timestamp.
You can also do something like this:
$node->field_date_range->timezone
to get the timezone.