0

I'm attempting to perform an action in a preprocess node hook inside my .theme file, but I can't seem to get it to fire.

Here's the content of my customtheme.theme file

<?php

function mytheme_preprocess_page(&$variables){

    $menu = \Drupal\block\Entity\Block::load('custom_main_menu');

    $variables['main_menu'] = \Drupal::entityTypeManager()
        ->getViewBuilder('block')
        ->view($menu);
}

function mytheme_preprocess_node(&$variables){
    die('Test');
}

Everytime I load a node, even though I have a die() statement in my mytheme_preprocess_node function it never fires, and the page loads just fine. I'm making sure that I'm loading pages that are nodes. Such as basic pages, and other custom content types. What's odd is that the mytheme_preprocess_page function does fire. It's able to load the main_menu variable inside the page.html.twig template just fine. I also made sure I was clearing the cache.

Why would a preprocess_page hook function fire but not a preprocess_node function.

  • Are you using display suite? – Gabor Szanto Jan 12 '18 at 14:24
0

You need check your page url. Hook_preprocess_node only run with page is node (example node/1...) or page have content is node (example page views have entity is node). If you done, you need clear cache.

  • I'm checking this on pages that are nodes. I'm checking when I'm loading a basic page, and nodes of other custom content types. – Matt Jan 3 '17 at 17:38
  • "Hook_preprocess_node only run with page is node" <- that's actually incorrect. _preprocess_node() is run ahead of every node, regardless of whether it's a teaser, a node page, or wherever the node appears. – Jaypan Jan 13 at 22:04

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.