3

I use the following code in a theme whose machine name is yokai.

// works like expected, output visible
function yokai_preprocess_html(&$variables) {
  dpm('yokai_preprocess_html');
  dpm($variables);
}

// does not work, no output visible
function yokai_preprocess_node(&$variables) {
  dpm('yokai_preprocess_node');
  dpm($variables);
}

Why doesn't hook_preprocess_node() get triggered?

8
  • It looks like this is by design and DS hijacks HOOK_preprocess_node() like described over here. I assume this is still the case in Drupal 8?
    – 4aficiona2
    Apr 8, 2016 at 15:32
  • Did you clear all caches? Btw, dsm() is just calling dpm(), there's no need to do the same thing twice. Just use dpm(). You can see the source here. Apr 8, 2016 at 15:50
  • just used both to see if either of them works. Rebuilt the cache several times with drush dr cr.
    – 4aficiona2
    Apr 8, 2016 at 15:54
  • Strange; the code looks OK; there's nothing else that comes to my mind. Maybe someone else will be able to point you in the right direction. I would try to do this from another theme - even hack Bartik temporarily and see if hook_preprocess_node() works from there. Apr 8, 2016 at 15:57
  • Only thing I can think of is either you are not printing the $messages, you misspelled the theme name.. try putting an exit in there and you'll know the code is being hit. I also recommend xdebug in instances just like this.
    – Kevin
    Apr 8, 2016 at 15:59

1 Answer 1

1

The problem is with dpm(). This uses drupal_set_message(), which uses the session variable to store messages:

function dpm($input, $name = NULL, $type = 'status') {
  if (\Drupal::currentUser()->hasPermission('access devel information')) {
    $export = kprint_r($input, TRUE, $name);
    drupal_set_message($export, $type, TRUE);
  }
  return $input;
}

When the node template is processed, the page is already built with the session variables at this point. Any messages that come later will not make it on the page. In this case better use kint(). dpm() is more usefull for debugging code that doesn't produce a page output, like form validation.

6
  • thanks for your answer, but unfortunately kint() does not work either. I tried to log if the HOOK_preprocess_node with \Drupal::logger('theme')->notice('yokai_preprocess_node was called'); which does not work either therefore the HOOK never gets called. If I log HOOK_preprocess_html like that \Drupal::logger('theme')->notice('yokai_preprocess_html was called'); everything works like expected and I can see the msg in the watchdog.
    – 4aficiona2
    Apr 11, 2016 at 12:33
  • what could cause that HOOK_preprocess_node does not get called? Never occurred to me before.
    – 4aficiona2
    Apr 11, 2016 at 12:35
  • I wanted to point out, that this is not easy to debug with dpm(). Other than that I could think of two reasons 1) the node is cached 2) the page has no node. Did you enable Twig debugging as suggested by Eyal? Then look, if there is <!-- THEME HOOK: 'node' --> in your html source.
    – 4uk4
    Apr 11, 2016 at 13:50
  • Page is definitely a node and Twig debugging is on too. Created a super basic new content type (only body field and no config) which does not use Display Suite nor Paragraphs (enabled for my other content types) and HOOK_preprocess_node gets triggered and I can see the notice in the watchdog. Now checking if DS or Paragraphs is causing this ...
    – 4aficiona2
    Apr 11, 2016 at 16:22
  • 1
    DS is causing the not calling of HOOK_preprocess_node. Until I enable a Display Suite layout the HOOK gets called, but just after enabling the default One-Column layout HOOK_preprocess_node does not anymore get called
    – 4aficiona2
    Apr 11, 2016 at 21:00

Your Answer

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

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