Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By dsteindl on
Hello,
i have the following hook_theme()
function mymodule_theme() {
return [
'event' => [
'render element' => 'elements',
]
];
}
I also have the following hook_theme_suggestions_HOOK()
function mymodule_theme_suggestions_event(array $variables) {
$event = $variables['elements']['#event'];
$sanitizedViewMode = $variables['elements']['#view_mode'];
$suggestions = [];
$suggestions[] = 'event__' . $sanitizedViewMode;
$suggestions[] = 'event__' . $event->id();
$suggestions[] = 'event__' . $sanitizedViewMode . '__' . $event->id();
return $suggestions;
}
My Controller-Function:
public function showEvent(Event $event, $view_mode = 'small') {
$build = ['#theme' => 'event'];
$build += \Drupal::entityTypeManager()->getViewBuilder('event')->view($event, $view_mode);
return $build;
}
When I now create a file modules/custom/mymodule/templates/event--small.html.twig it isn't used for rendering.
If I move the file "event--small.html.twig" to the theme-folder (e.g. core/themes/bartik/templates/event--small.html.twig), everything works well.
How can I achieve, that the files from the modules folder are used?
br
Daniel Steindl
Comments
The simple answer is that you
The simple answer is that you cannot. Theme overrides only work in themes.
Ah okay, good to know. Thanks
Ah okay, good to know. Thanks!