Score:0

Using template variations from the modules folder

lc flag

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.

Score:2
in flag

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',
      ],
lc flag
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...
lc flag
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.
4uk4 avatar
cn flag
`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 https://drupal.stackexchange.com/questions/298844/use-custom-content-entity-theme-suggestions-in-own-module
lc flag
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? :-)
4uk4 avatar
cn flag
@Gábor, see the linked topic. The code in question+answer is the complete solution.
lc flag
Yes, I know, thanks, I utilized it. I just wanted to be able to accept it here.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.