1

How can I display a list of preprocess block variables so I can conditionally include a library? Using the 'plugin_id' doesn't seem to work and when I echo all of the variables all I see is a blank page. Is there a Drupal page that will show me what all the variables are for blocks?

My custom block module is called, 'leaflet_block'.

function leaflet_block_preprocess_block(&$variables) {
  echo "<pre>"; print_r($variables); echo "</pre>"; // shows a blank page

  if ($variables['plugin_id'] == 'leaflet_block') {
    $variables['#attached']['library'][] = 'leaflet_block/leaflet_block';
  }
}

1 Answer 1

0

echo "<pre>"; print_r($variables); echo "</pre>";

That's not how it should be done, instead, install the Devel module.

This will allow you to do:

dump($variables);

If you haven't turned caching off, for developing purposes, then you will have to flush all caches to see the dump.


In addition, if you also install the kint package/library with:

composer require kint-php/kint

now you could also do:

kint($variables);

Related: Welcome to Kint 101

1
  • Thank you for the help. After installing Devel I was able to dump the variables. I still wasn't able to access the module name/title, which is probably another questions, so I used '\Drupal::service('path.current')->getPath();' to get the path and write a condition against that.
    – jbreslow
    Dec 9, 2020 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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