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?
I guess you do not need to convert
$entity into an array,
this would simply work.
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.
in drupal 8 , $node being of class node
will return an array of values.
If you expect only one, you might as well stringify the array with
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
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();
For me, having an
$entity (a node) this worked:
use Drupal\node\Entity\Node $node = Node::load($nid); $title = $node->get('title')->value;
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
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();
There is a contrib module that helps you do just that called Twig Field Value.
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?