I have the usual theming solution for a custom entity:

function module_theme($existing, $type, $theme, $path) {
  return [
    'customentity' => [
      'render element' => 'elements',
      'template' => 'customentity',

function template_preprocess_customentity(array &$variables) {
  $variables['content'] = [];

function module_theme_suggestions_customentity_alter(array &$suggestions, array $variables) {
  $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
  $suggestions[] = 'customentity__' . $sanitized_view_mode;

The only problem is that when I want to use a twig specific to the view mode, customentity--viewmode.html.twig, it only works if the twig file is inside the theme. If it's in the module templates folder, it isn't picked up (carefully checked all cases with twig debugging).

So, in this regard, it's functioning. Still, I'd need the twig in the module itself because in this case, it's not a mere theming question, the view mode is both logically and programmatically a radically different way to present the contents. It's for internal use, not a contrib module, so normal user expectations don't apply.

1 Answer 1


You have to add the path to your theme hook like so:

'my_entity__mailteaser' => [
        'render element' => 'elements',
        'base hook' => 'my_entity',
        'template' => 'my-entity--mailteaser',
        'path' => drupal_get_path('module', 'asdentbase') . '/templates',
  • Unfortunately, if I do this, it will no longer find my theme.inc file because it will be expected in the same folder. There are no separate path settings for both...
    – Gábor
    Commented Jun 25, 2021 at 10:32
  • Anyway, I tried with putting the theme.inc there, too. No, it doesn't work. path changes where the system tries to find the attached file but doesn't change where it looks for the templates.
    – Gábor
    Commented Jun 25, 2021 at 10:39
  • base hook is the correct solution. Drupal is then able to locate the template in the module's templates folder, even without pointing at it via template or path. See drupal.stackexchange.com/questions/298844/…
    – 4uk4
    Commented Jun 25, 2021 at 11:25
  • Not by itself but only if I add an extra theme item with the view mode glued to it, yes. you're right. Would you care to put it into a short answer for me to accept? :-)
    – Gábor
    Commented Jun 25, 2021 at 13:45
  • @Gábor, see the linked topic. The code in question+answer is the complete solution.
    – 4uk4
    Commented Jun 25, 2021 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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