1

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?

5
  • 1
    Does this answer your question? How do you override a module's template from another module?
    – 4uk4
    Aug 7, 2023 at 18:31
  • Might they also need to adjust the hook invocation order?
    – mona lisa
    Aug 7, 2023 at 18:45
  • 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.
    – esod
    Aug 7, 2023 at 20:38
  • Theme suggestions use underscore characters as delimiters; they are changed to hyphens only when Drupal core is searching for the template file.
    – apaderno
    Aug 8, 2023 at 18:09
  • Thanks @apaderno. Was just uncovering the problem.
    – esod
    Aug 8, 2023 at 18:53

1 Answer 1

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.