2

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.

1
  • 1
    Are you using display suite? Jan 12, 2018 at 14:24

1 Answer 1

1

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.

2
  • 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.
    – Amy
    Jan 3, 2017 at 17:38
  • 1
    "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, 2019 at 22:04

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.