1

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.

0

2 Answers 2

1

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 and acknowledge you have read our privacy policy.

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