Score:1

Override contrib module's template for an admin module

us flag

I need to override a contrib module's template for an admin module. For example, the webform_composite module has hook_theme() in its module file.

/**
 * Implements hook_theme().
 */
function webform_composite_theme() {
  return [
    'webform_composite' => [
      'render element' => 'element',
    ],
  ];
}

and a template in its templates folder at

modules/contrib/webform_composite/templates/webform-composite.html.twig

Changes made to the template print on the webform. I don't want to make changes to the module's template and I don't want to make a sub-theme of the admin theme. I want to override the contrib module's template in my custom module.

In my custom module, hook_theme() is like this:

/**
 * Implements hook_theme().
 */
function my_module_theme($existing, $type, $theme, $path) {
  return [       
    'webform_composite__custom' => [
      'base hook' => 'webform_composite',
    ],
  ];
}

And hook_theme_suggestions_alter() is like this:

/**
 * Implements hook_theme_suggestions_alter().
 */
function my_module_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
  if ($hook === 'webform_composite') {
    $suggestions[] = $hook . '--' . 'custom';
  }
}

as well as adding a template in modules/custom/my_module/templates/webform-composite--custom.html.twig

This adds my template but not as an override since webform-composite.html.twig still has the x.

<!-- THEME DEBUG -->
<!-- THEME HOOK: 'webform_composite' -->
<!-- FILE NAME SUGGESTIONS:
   * webform-composite--my-award.html.twig
   * webform-composite--custom.html.twig
   x webform-composite.html.twig
-->
<!-- BEGIN OUTPUT from 'modules/contrib/webform_composite/templates/webform-composite.html.twig' -->

How do I override a contrib module's template for an admin module?

4uk4 avatar
cn flag
Does this answer your question? [How do you override a module's template from another module?](https://drupal.stackexchange.com/questions/313816/how-do-you-override-a-modules-template-from-another-module)
id flag
Might they also need to adjust the hook invocation order?
us flag
I updated my question with what I think are the right changes from your suggestions. But it's still reading the `webform-composite.html.twig` instead of my `webform-composite--custom.html.twig`.
apaderno avatar
us flag
Theme suggestions use underscore characters as delimiters; they are changed to hyphens only when Drupal core is searching for the template file.
us flag
Thanks @apaderno. Was just uncovering the problem.
Score:0
us flag

Changing '--' to __ in the $suggestions array fixed the problem per this core issue: hook_theme_suggestions_alter does not work for hyphens

Here's the working code:

/**
 * Implements hook_theme().
 */
function my_module_theme($existing, $type, $theme, $path) {
  return [
    'webform_composite__custom' => [
      'base hook' => 'webform_composite',
    ],
  ];
}

/**
 * Implements hook_theme_suggestions_alter().
 */
function my_module_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
  if ($variables['theme_hook_original'] == "webform_composite") {
    $suggestions[] = $hook . '__' . 'custom';
  }
}

Now the debug reports:

<!-- THEME DEBUG -->
<!-- THEME HOOK: 'webform_composite' -->
<!-- FILE NAME SUGGESTIONS:
   * webform-composite--my-award.html.twig
   x webform-composite--custom.html.twig
   * webform-composite.html.twig
-->
<!-- BEGIN OUTPUT from 'modules/custom/my_module/templates/webform-composite--custom.html.twig' -->

The first template suggestion is from webform_composite.module -> hook_theme_suggestions_HOOK_alter().
The second template suggestion is my custom module.
The third template suggestion is from webform_composite.module -> hook_theme().

My custom template has the x and is the template read in by Drupal.

I sit in a Tesla and translated this thread with Ai:

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.