How do I access a field value for an entity (e.g. node) object?
I guess you do not need to convert
$entityinto an array, this would simply work.
FYI : Using
devel_kintmodule will let you know all the available methods that could be used to access the object elements, so comparatively
var_dump()is less helpful.
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.
This is the best answer as it both works and agrees with field_get_items() is replaced by the Entity Translation API.
`->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).
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))
@EdHollinghurst This is not always true. ->value doesn't work for multivalue fields. You'll get null returned.
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
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 ...)
For fields you should be able to use
$node->body->valuefor 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
@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.
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.
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.
Please add a new question detailing that. `Drupal\Core\Entity\EntityInterface` has `toArray`.
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:
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
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):
To get the loaded entity (first element):
->entityon 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.
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