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

  1. Create and enable a new module with the name "websitename"
  2. Create and enable a new theme with the name "websitename"
  3. Add a hook_theme() to your websitename.module See code example below
  4. Create the file websitename.theme in the websitename theme folder.
  5. Use the template you declared in the module in the websitename theme. For example in the hook_preproccess_node(). See code example below.
  6. 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

Wemagine created an issue. See original summary.

sander wemagine’s picture

Issue summary: View changes
cilefen’s picture

Project: Drupal core ideas » Drupal core
Version: » 9.2.x-dev
Component: Idea » system.module
Category: Feature request » Bug report
Status: Active » Postponed (maintainer needs more info)
Related issues: +#371375: Do not allow a module and theme to use the same name.

I 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.

sander wemagine’s picture

I agree, that issue is exactly the same.

cilefen’s picture

Status: Postponed (maintainer needs more info) » Closed (duplicate)