Search code examples
phpdrupalmoduledrupal-8preprocessor

Drupal 8 - template_preprocess_node not working


I have created an article listing view with a view mode "Teaser Full Width". The page looks fine and articles listed. When I tried to edit something using template_preprocess_node(&$variables), the hook is not working in Teaser Full Width. But the same work in the individual node when I click any article.

<?php
function templatename_preprocess_node(&$variables) {
   kint($variables);die('test');
}
?>

I have already gone through different questions in StackOverflow.

  1. Devel module enabled.
  2. Tried with condition if ($variables['view_mode'] == 'teaser_full_width'){ ... }
  3. Tried print_r instead of kint.
  4. Tried by changing max_allowed_packet.
  5. Verified templatename is enabled and set as default.
  6. Cache Cleared.

(Article Listing Page) Template file:- node--article--teaser-full-width.html.twig

template_preprocess_html is working fine on this page.

template_preprocess_node not working on this page.

(Article Single Page) Template file:- node--article--full.html.twig

Both hooks are working fine.

Does anyone know what the issue is? My drupal version is 8.6.10.


Solution

  • As per drupal's documentation template_preprocess_node() is the hook for node templates. Since the views listing page is containing the nodes but is not a node page so template_preprocess_node() is not working there. However, It is still an html page so template_preprocess_html() works there.

    In simple terms, your "article listing view" is a page not node so template_preprocess_node() doesn't work.