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?