1

I'm trying to see what variables I have available in a $vars object in a Views hook. Unfortunately, like many Drupal object, this one is huge and attempting to output it using the Drupal debug() function (with or without the 3rd boolean argument) causes the server to crash with an out of memory error.

Note that this is after increasing the memory limit quite high, the memory limit isn't the problem here (UPDATE: I later discovered the array output was over 370 megabytes!).

What's the least intensive (fewest contrib modules, patches, custom code, 3rd party libraries etc) way to explore the contents of a Drupal object like this that's too big for Drupal's debug()?

4

Devel

Devel module provides dd() command which is very useful for dumping very large object. Just run dd($vars) in the middle of code, it'll dump any object to the temporary file in your Drupal temp folder (temporary://drupal_debug.txt).


XDebug

Use XDebug PHP extension, so var_dump() will be replaced by xdebug_var_dump() so your big dump won't consist circular dependencies and would be limited only by specific depth.


Alternatively use TVarDumper, array_slice, serialize or json_encode, so check below for further details:

  • Nice, +1. For anyone wondering how to locate their temporary directory, see this question - tldr; it's a setting configured by Drupal under Home » Administration » Configuration » Media » File system - that said, for now I'll stick to my tree walking method, the text file exported by dd() for this variable is 370mb! – user56reinstatemonica8 Apr 21 '16 at 17:03
0

Here's a very quick and reliable way to explore any object or array (here named $vars) one layer at a time:

  debug( array_keys((array)$vars), 'vars: '.  gettype($vars), false);

array_keys() gives only the names of the keys, giving you enough info to see where you need to go in the tree, regardless of what those keys contain.

Since you might not know if this variable is an array or an object, casting it as an array with (array) makes array_keys() work on either. You'll need to know which it is for the syntax for the next step, so that's ouput in the Drupal debug() title string with gettype().


Then you can simply choose a likely-looking from the output and walk up the tree. For example on seeing that $vars is an array which contains a relevant-looking key field:

  debug( array_keys((array)$vars['field']), 'field: '.  gettype($vars['field']), false);

Then on learning that $vars['field'] was an object containing a relevant-looking property items:

  debug( array_keys((array)$vars['field']->items), 'items: '.  gettype($vars['field']->items), false);

...and so on.

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.