6

I'm trying to preprocess my variable array in drupal 8, But it's not working well.I'm checked with the Bartik theme also, But getting the same error here. When I'm trying to print the variable array its giving a blank page result. How can I preprocess my variable array for a node template?

`function bartik_preprocess_node(&$variables) {
  // Remove the "Add new comment" link on teasers or when the comment form is
  // displayed on the page.
  if ($variables['teaser'] || !empty($variables['content']['comments']['comment_form'])) {
    unset($variables['content']['links']['comment']['#links']['comment-add']);
  }
  print_r($variables);
}`

This is the function I'm tried to checkout the issue in bartik theme.

2 Answers 2

12

Instead of using print_r() enable devel module and their new submodule called kint. And use kint() instead of print_r(). You can also use print_r() but is messy.

How can I preprocess my variable array for a node template?

function YOURTHEME_preprocess_node(&$variables){
  kint($variables);
}

If this does not work it might be a cache problem. I will recommend you to set you website into dev mode using DrupalConsole. drupal site:mode dev

Why ?

The Twig engine provides options for configuring debugging, automatic reloading (recompiling) of templates, and caching compiled templates in the filesystem. By default, the Twig theming engine compiles templates into PHP code and stores the compiled code in memory. Compiled code is unsuitable for development, since changes in Twig templates are not immediately updated in your Drupal site. Twig cache can be cleared through Drupal's clear cache interface, but for ongoing development it's easier to change Drupal's settings so that Twig doesn't cache anything at all.

Running drupal site:mode dev will disable all twig cache. For more information about debugging please visit - Debugging compiled Twig templates

2
2

Another option is to use the VarDumper module which isn't quite so memory intensive as Kint. I find it a bit more user-friendly and better-looking!

function YOURTHEME_preprocess_node(&$variables){
  vardumper($variables);
}

Drupal VarDumper example

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.