0

I need to debug the $result property of the ViewExecutable object passed to hook_views_post_execute but for what ever reason, Drupal can't handle it. $view->result is an array of type ResultRow, and because it's an array, I'm attempting to log it like so:

\Drupal::logger('foo')->notice('<pre>' . print_r($view->result, TRUE) . '</pre>');

When I do this and then load the view I'm attempting to mess with, it loads the page without css and then the recent log messages just shows up blank, no errors or anything that would actually be helpful.

I've tried a number of different ways but this is always the result. Why can't this logger print a simple array?

I've even tried looping through the array and printing just the value:

foreach($view->result as $key => $value) {
  \Drupal::logger('bar')->notice(print_r($value, true));
}

And that will render the view without css and then the logger doesn't print anything at all. I double checked and printed a regular string and that worked, but when I attempt to print the value it just breaks and doesn't do anything.

3
  • Install the Devel module. It gives you a handful debugging helpers like dpm($view->result) or when you also have Kint installed ksm($view->result) to pretty-print variables into the Status Messages block. – leymannx Sep 28 '20 at 21:20
  • @leymannx I have that and was trying, but 1/2 the time when I'm printing those my site breaks and it takes me to the install page and the logs say SQL connection broke 2006... – user96646 Sep 28 '20 at 22:02
  • 2
    In fact, if you don't have debug logs that express your frustrations at the platform, are you really a developer? In fact, it means you don't know how to use a debugger. I'd strongly suggest using an IDE like PHPStorm or Visual Studio Code with Xdebug. e.g. jetbrains.com/help/phpstorm/… Live is much less frustrating with a real debugger. – Hudri Sep 28 '20 at 22:17
0

Your code works fine for me so perhaps your view has no results?

Anyways, the Drupal log is not really meant to be used for printing variables for debugging purposes. Use an appropriate tool like Devel which has/comes with another module called Kint, enable Kint.

use Drupal\views\ViewExecutable;

/**
 * Implements hook_views_post_execute()
 */
function mymodule_views_post_execute(ViewExecutable $view) {
  kint($view->result);
}

Flush cache and you'll see this

enter image description here

For more info on how to use Kint, see Welcome to Kint 101

3
  • Thanks, it appears to work with $value now, however, it doesn't work with some of the objects that exist inside of $value.. I keep getting an SQL disconnect error. Specifically, when I try to print out a entity reference that exists inside of $value. – user96646 Sep 28 '20 at 22:10
  • 1
    @TheSnowboardingDev you'll need to lower Kint's recursion – No Sssweat Sep 28 '20 at 22:12
  • 1
    @TheSnowboardingDev I'd recommend using XDebug for debugging. It saved my life. – Armando Silva Sep 28 '20 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy