Thanks for this! I wanted to clean up the answer a little bit. This is essentially the same with a couple changes:
- Instead of creating a new theme suggestion/hook for every hook already in the system, we create one only for the hook we want to create a new template for. This is a much less heavy handed approach.
- As 4uk4 pointed out, dashes have been replaced with underscores.
- As 4uk4 pointed out, we use
base hook
instead of template
in our hook_theme()
implementation.
- We add
path
in our hook_theme()
implementation, allowing us to sort templates within our module. In this case we put the template in templates/webform
.
// In web/modules/app/app.module
/**
* Implements hook_theme_suggestions_alter().
*/
function app_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
// Use switch so we can easily add another hook in the future.
switch ($hook) {
case 'webform_email_html':
$suggestions[] = $hook . '__' . 'custom';
break;
}
}
// In web/modules/app/app.module
/**
* Implements hook_theme().
*/
function app_theme($existing, $type, $theme, $path) {
return [
'webform_email_html__custom' => [
'base hook' => 'webform_email_html',
'path' => $path . '/templates/webform',
],
];
}