2

I have a project on D8 where I don't use any custom themes, I use default seven theme for everything because there is no need of any visual theming.

So My question: Is it possible to override theme template using module? without implementing theme at all?

I have a case where I want to add custom third party generated value in node's template.

This is what I have done:

Implemented custom template:

function MY_MODULE_theme($existing, $type, $theme, $path) {
  return array(
    'video_teaser' => [
      'template' => 'video_teaser',
      'variables' => NULL,
    ]
  );
}

Implemented hook_theme_suggestions_alter:

function MY_MODULE_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
  if(in_array('node__teaser', $suggestions)){
    $suggestions[] = 'video_teaser';
  }
} 

And this, works, instead of default template, my video_teser template is used.

BUT: I have a problem passing default node variables to my custom template, they aren't passed automatically and I couldn't find the way to do this using current approach.

8

You can do this in hook_theme() with the base hook 'node':

function MY_MODULE_theme($existing, $type, $theme, $path) {
  return [
    'node__video_teaser' => [
      'template' => 'node--video-teaser',
      'base hook' => 'node',
    ],
  ];
}

You don't need to define the base hook. Drupal can discover this automatically. What's important is that you use the double underscore, because this works exactly like the theme suggestions. After Drupal found the base hook it will clone the preprocess functions of this hook and will provide the variables for the node.

  • Thanks a lot, it worked but with little change, I had to add node__video_teaser to $suggestions using hook_theme_suggestions_alter. Another way I found was that you can add 'preprocess functions' => [ 'template_preprocess_node', ] to template definition as described on the link @Ivan commented. – otarza May 10 '16 at 13:18
  • Are you sure about not defining the base hook? I had an issue where none of the rest of the page was getting loaded (none of the variables) until I added it. – UnsettlingTrend Feb 9 '17 at 18:10
  • @4k4 - Thank you for the tip on the double underscore. This needs more visibility as it's quite simple to override a template from a module when you know the proper naming conventions within hook_theme. – rtd1123 Aug 30 '18 at 12:17

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.