Problem/Motivation
Creating a custom theme and module with a identical name causes unexpected behaviour when using hook_theme(). Drupal is trying to find the template file inside the theme instead of the module folder where the template has been declared.
Steps to reproduce
- Create and enable a new module with the name "websitename"
- Create and enable a new theme with the name "websitename"
- Add a
hook_theme()to your websitename.module See code example below - Create the file websitename.theme in the websitename theme folder.
- Use the template you declared in the module in the websitename theme. For example in the
hook_preproccess_node(). See code example below. - You now get a error:
Twig\Error\LoaderError: Template "themes/custom/websitename/templates/testimonials.html.twig" is not defined.because Drupal tries to find the file inside the theme folder instead of the module folder.
// File: /modules/custom/websitename/websitename.module
/**
* Implements hook_theme().
*/
function websitename_theme() {
return [
'testimonials' => [
'render element' => 'children',
],
];
}// File: /themes/custom/websitename/websitename.theme
/**
* Implements hook_preproccess_node();
*
* @param $variables
*/
function websitename_preprocess_node(&$variables) {
$variables['testimonials'] = [
'#theme' => 'testimonials',
];
}
Proposed resolution
I can think of two possible solutions:
- Build in a check when enabling a module or theme that checks if there is already a similar named theme/module. This check should prevent enabling the theme/module.
- Add a check on the "Status Report" page that displays a warning message when there is a module and theme with the same name.
Remaining tasks
Todo
User interface changes
Todo
API changes
Todo
Data model changes
Todo
Comments
Comment #2
sander wemagine commentedComment #3
cilefen commentedI believe this is a duplicate of this old issue: #371375: Do not allow a module and theme to use the same name.. Do you agree? Also, this is in the ideas queue, so I am moving it.
Comment #4
sander wemagine commentedI agree, that issue is exactly the same.
Comment #5
cilefen commented