55

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?

78

I guess 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.

  • 11
    Actually it should be: $entity->get('field_name')->value; – Ed Hollinghurst Mar 10 '16 at 23:12
  • 1
    Do 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. – autopoietic Apr 8 '16 at 10:45
  • 2
    getValue() works for me (both on 8.1.x and 8.2.x) see api reference – leon.nk May 19 '16 at 4:48
  • 12
    ->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 a value property. It will not work for more complex types (eg. date or entity reference). – Pierre Buyle Sep 6 '16 at 18:13
  • 3
    This 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)) – oknate Apr 19 '17 at 18:45
47

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();
  • I don't "get" (string and value) "it". Can you please add some more details to explain this answer? FYI: not doing so puts your answer at risk of being deleted via moderation (sorry ...) – Pierre.Vriens Sep 6 '16 at 15:56
32

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).

  • how do you render that field then? – Alex Apr 28 '16 at 13:38
  • this seems to be outdated – Alex May 20 '16 at 14:24
  • @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. – tenken May 20 '16 at 14:45
  • 5
    Check 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. – Mirsoft Jul 12 '16 at 9:53
  • 2
    @tenken: Actually, updating an existing answer is preferable over adding a redundant question with new answers. – Pierre Buyle Sep 6 '16 at 18:04
15

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.

  • $node->toArray() don't work for me on Drupal 8. – Axel Briche Jan 30 '15 at 22:26
  • Please add a new question detailing that. Drupal\Core\Entity\EntityInterface has toArray. – user49 Jan 31 '15 at 1:23
  • 1
    This answer doesn't provide the solution; downvoting. – colan Jul 12 '16 at 20:26
  • While this answer is not necessarily a direct answer to this question, it is a damn great function. Thanks. – Stef Aug 10 '18 at 14:28
13

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();
  • 2
    Load the node like this: $nid = \Drupal::routeMatch()->getRawParameter('node'); $node = \Drupal::entityTypeManager()->getStorage('node')->load($nid); – rémy Oct 28 '16 at 8:40
7

For me, having an $entity (a node) this worked:

$entity->field_name->getValue();

using 8.1.

  • 1
    This 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 – Mirsoft Jul 12 '16 at 9:58
4

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();
3
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

0

There is a contrib module that helps you do just that called Twig Field Value.

  • Link-only answers are discouraged here. While the module project might solve the problem, it doesn't explain why or how. Please try to address how to use the module so readers/OP know how it solve the question asked. – Shawn Conn Jan 26 '17 at 1:56

protected by Community Feb 16 '17 at 18:11

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.