0

I know how to get a particular field (title field) of a node by following code:

$publications = \Drupal::entityManager()->getStorage('node')->load($node_id);
$title = $publications->getTitle(); 

But I need to get all fields and values in that particular node. I tried print_r($publications). Is there any way to get this?

2

This is how you can add all values in a custom array. The EntityManager class and the entity.manager service are deprecated; you should use the entity_type.manager service instead.

$publications = \Drupal::entityTypeManager()
  ->getStorage('node')
  ->load($node_id);

foreach ($publications->getFields() as $name => $field) {
  $myFields[$name] = $field->getString();
}

kint($myFields);

You need to improve this code because for some field types that can be different.

  • For multi-value fields you use $field->getValue()
  • For entity reference fields, $field->entity->field_name->value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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