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

Jaypan’s picture

The simple answer is that you cannot. Theme overrides only work in themes.

dsteindl’s picture

Ah okay, good to know. Thanks!