How do I access a field value for an entity (e.g. node) object?

Solutions

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

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

Tags: Drupal 8 / Entities

Similar questions

How can I programmatically create a media object and a new file and associate the file with the media object?
I am trying to programmatically create a media object and a file, and then associate the file with the media object. The first part works--I can successfully create the media object and file. But the second part fails--the media object has no file associated with it. Here is my code: my_example.txt is created in the files directory, and the media o...
Using the $object in Actions.I've created a module but parameter _action($object) is not work
Using the $object in Actions. $object: Many actions act on one of Drupal’s built-in objects: nodes, users, taxonomy terms, and so on. When an action is executed by trigger.module, the object that is currently being acted upon is passed along to the action in the $object parameter. For example, if an action is set to execute when a new node is creat...
Uncaught TypeError: Object#<Object> has no method 'dispatchEvent'
I'm trying to combine Drupal with Picasa web integrator. I have these 3 lines in the section of my page.tpl.php: I also added this script to my head section, to test an album (the username and albumname are not important, I tested it offline with my username and album and that worked fine): Google Chrome gives me this error when I try it on my Drup...
Accessing an object within another object in Drupal
I have an array output like this from drupal, how do I access the value of [name] from field_episode_tags, I have this so far When I get up to 'taxonomy term' there is another object i tried using another -> poitner but that did not work.
jquery.js?v=1.9.1:4421 Uncaught Error: Syntax error, unrecognized expression: [object Object]
i have a dropdown menu with some submenus. When I click one li item, jquery should add a class to the closest ul to leave the ul open. But when I click one li item, it redirects to a new webpage. How to remember the added class after site refresh? I have tried this: But my console say: jquery.js?v=1.9.1:4421 Uncaught Error: Syntax error, unrecogniz...
How to access the value of a Drupal\Core\StringTranslation\TranslatableMarkup object within a form?
I tried this : dpm($button['#value']->string but got this error: access protected property Drupal\Core\StringTranslation\TranslatableMarkup::$string Within a form object I need to be able to read that value and also change it. how can I access it? thanks

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.