1

I Want create a custom module with custom template for override a menu created from the back office administration. I don't want put my custom template in the theme, I want put this file in my custom module (in custommodule/templates/..)

1) I activated twig debug, I can see what is the suggested template menu name : (menu--main.html.twig)

enter image description here

2) I added in my custom .module file the hook theme :

function custommodule_menu_theme($existing, $type, $theme, $path)
{
  return array(
    'menu__main' => array(
      'base hook' => 'menu',
      // 'render element' => 'menu', // I tried this, nothing change
      // 'template' => 'menu--main', // I tried this, nothing change
    ),
  );
}

3) From my custommodule, I created a directory 'templates'. In this directory, I add the twig file menu--main.html.twig :

<h1> Hello ! <h1>

4) I cleared caches (drush cr)

5) I reloaded the page from my browser. I noticed my custom template isn't called. And if I look in the html structure page, I see :

enter image description here

Why this is the template from the global theme called ? and not my templates/main--menu.html.twig ? I don't understand..

UP

My new code :

function custommodule_theme($existing, $type, $theme, $path) {
  $theme=array();
  $theme['menu__main'] = array(
    'base hook' => 'menu',
    'render element' => 'menu',
      );
  return $theme;
}

But my custommodule/templates/menu--main.html.twig is still not called (I cleared caches). My page always uses the themes/boostrap/templates/menu/menu--main.html.twig in the

2 Answers 2

4

Your module function custommodule_menu_theme() is incorrectly named so hook_theme() isn't evoked and returning incomplete output; it needs, at least, the base hook & render element elements entered so that it knows what hook to target and the render array to supply to the template.

/**
 * Implements hook_theme().
 */
function custommodule_theme($existing, $type, $theme, $path) {
  $theme = array();
  $theme['menu__main'] = array(
    'base hook' => 'menu',
    'render element' => 'menu',
  );
  return $theme;
}
2
  • Thanks you for answer ! I got an error with your code then I needed add "$theme=array()" in the begin of the custommodule_theme function. But after your correct and mine, my custom template keeps not called :/ I update my main post Oct 13, 2016 at 6:37
  • Yeah, that would cause an uninitialized warning, fixed. As for why its not picking up template, you'll need to debug it further. I can only guess at the problem from here.
    – Shawn Conn
    Oct 13, 2016 at 6:58
3

A theme will always win over a module when it comes to theming. So you need to either

  • remove template from theme (so your module's template will be used.
  • create a custom template suggestion for the main menu that only your module has using hook_theme_suggestions_HOOK_alter
  • Modify the theme registry to let your module win using hook_theme_registry_alter

Of the 3 I would suggest going for option 2, as it is the most flexible one (themes could overwrite the module defined template if needed).

Also remember that the template suggestion can be anything, but your module needs to implement hook_theme to register the template.

1
  • If I only remove the 'menu--main.html.twig' template from my customs templates directory (bootstrap and the bootstrap startkit) and I keep the 'menu--main.html.twig' in my custom module, my page use the "themes/bootstrap/templates/menu/menu.html.twig" template x) Ill try your option 2 in a few hours :) Oct 13, 2016 at 11:58

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.