0

Drupal 8.7.2

function mymodule_node_presave(Drupal\Core\Entity\EntityInterface $entity) {
  data_dump($entity->original); //is empty because its a new node, not a node being updated.
  // How to get all values at one time?
  // By values I mean the same value you would get from:
  data_dump($entity->get('title')->value);
  // but from all fields and not just one field at a time.
}

I have a custom content type that has 30 custom fields that I need to examine inside this hook. I'd like to avoid getting the value of each field individually.

Thank you for your time.

  • $entity->toArray()? – Clive Jun 1 at 17:46
  • @Clive YES!! That's it. Please put that in an answer and I'll mark it. Thanks for your help. I couldn't find that in the docs anywhere. If you have a link please include it. Thanks. – rayjamesfun Jun 1 at 17:53
0

There are probably a few different ways to get that info, but if the returned format suits your needs then EntityInterface::toArray(), which Gets an array of all property values, is probably the most succinct:

$field_data = $entity->toArray();
0

You can try use Content Entity Field definitions

Sample code:

foreach ($entity->getFieldDefinitions() as $field => $definition) {
    $entity->get($field)->getValue()
}

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.