Score:0

For specific block use unique template file located in custom module's templates folder

cn flag

Within my custom module 'footermenu' I would like to specify a template file for a specific menu block. I also want to locate the template file within the /templates folder of my 'footermenu' module.

The specific block #id is ‘mainmenu’.

As such I’ve been able to use the function below to change the template file to ‘menu__grid’.

function footermenu_preprocess_block__mainmenu(&$variables) {
  $variables['content']['#theme'] = 'menu__grid';
}

After which (and after clearing cache) it uses 'menu—grid.html.twig' which is all very good.

However, it only searches within the theme’s /templates directory.

How can I make it search within my custom modules templates folder please?

I've tried using the below but it doesn't work in this case.

/**
 * Implements hook_theme_registry_alter().
 */
function footermenu_theme_registry_alter(&$theme_registry) {
  $theme_registry['menu__grid']['path'] = drupal_get_path('module', 'footermenu') . '/templates';
}
Jaypan avatar
de flag
That's not how the flow works. Drupal's flow works core -> module -> theme. You shouldn't be overriding a theme from a module. Modules provide default templates, themes can override them, not the other way around.
websoft avatar
cn flag
Okay many thanks for your help.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.