0

I'm trying to print out the $entity variable im hook_entity_presave because I want to see all of the properties and their values at the time of saving.

The problem is that every way I have tried to print out the information, I get out of memory limit PHP errors. My PHP settings are set to 1024M max memory.

I have tried print_r(), var_dump() and debug(), all with the same result. I also tried dpm(), which allows the page to continue without running out of memory, but nothing gets output.

function my_module_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
  debug($entity, NULL, TRUE);
}
  • This happen during a node save I assume. Can you elaborate on which action triggers the presave hook? drush could be your friend... – Stefanos Petrakis Apr 25 '16 at 17:14
  • Try devel Kint which is part of the devel module for Drupal 8. kint($entity); Kint is the preferred method of inspecting arrays in Drupal 8. – Danny Englander Apr 25 '16 at 17:27
4

You don't want to see all properties. That's internal data that doesn't help you much anyway. What you want to see is the field values. And the easiest way to do that is this:

debug($entity->toArray());

That gives you a simple structure of all fields and their values. You can access them in a similar way as that array looks like, e.g. $entity->field[$delta]->property, [$delta] is optional and defaults to 0. See http://wizzlern.nl/drupal/drupal-8-entity-cheat-sheet for more details.

The other way is to use an IDE with a debugger, then you can set break points, inspect anything at runtime and even execute code.

1

I had the same issue with not being able to print variables of a large Drupal array. I came across this solution on the Drupal site. Credit goes to the user malaynayak for the solution.

$result = json_decode(json_encode($array));
var_dump($result);

or

$result = unserialize(serialize($array));
var_dump($result);
0

Have you tried:

$export_str = var_export($entity, TRUE); dpm($entity_str);

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.