1

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?

1

1 Answer 1

6

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
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.