0

In a custom module I'm adding a custom page template suggestion per multi-site like following which would then search for page--MYSITE.html.twig in the current theme's /templates folder.

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function MYMODULE_theme_suggestions_page_alter(array &$suggestions, array $variables) {

  $site_path = \Drupal::service('site.path');
  $site_path = explode('/', $site_path);
  $site_name = $site_path[1];

  $suggestions[] = 'page__' . $site_name;
}

Question: Is it possible to also specify a new location for this template. Like let's say inside my custom module's /templates folder?

Why: It would let me use a core theme but with my custom template (without the need to add something custom to core or to create a new sub-theme, lazy as I am).

There are 3 suggestion hooks that come into my mind but with none if them it seems possible to also specify a custom location. Is this right? Or is there a way? Is it bad practice to do so?

0

1 Answer 1

1

Is it possible with one of the template suggestion hooks to also specify a new template location?

You can specify a new template location in hook_theme():

path: Override the path of the file to be used. Ordinarily the module or theme path will be used, but if the file will not be in the default path, include it here. This path should be relative to the Drupal root directory.


Like let's say inside my custom module's /templates folder?

This means when using hook_theme() instead of hook__theme_suggestions_* the module's template folder is already default and you don't need to specify path.

So you can use this answer hook_theme_suggestions_page() provide default template file inside module folder.

2
  • Ah I see, per base hook override. Thanks for the linked answer! I guess that makes my question a duplicate then :)
    – leymannx
    Jun 6, 2018 at 12:08
  • Your question is broader for how to specify a new template location (which could be any path), so I answered this part and then referenced the existing question.
    – 4uk4
    Jun 6, 2018 at 12:22

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