Problem/Motivation
The eva_display_entity_view theme does not have any template suggestion, so it is not easy to have a different template file for each view.
Steps to reproduce
Display an eva field.
The template suggestion is just this:
<!-- THEME DEBUG -->
<!-- THEME HOOK: 'eva_display_entity_view' -->
<!-- BEGIN OUTPUT from 'modules/contrib/eva/templates/eva-display-entity-view.html.twig' -->
Proposed resolution
We currently use something like this:
/**
* Implements hook_theme_suggestions_HOOK().
*/
function foo_theme_suggestions_eva_display_entity_view(array $variables): array {
/** @var \Drupal\views\ViewExecutable $view */
$view = $variables['view'];
return [
implode('__', [
$variables['theme_hook_original'],
$view->id(),
]),
implode('__', [
$variables['theme_hook_original'],
$view->id(),
$view->current_display,
]),
];
}
Comments
Comment #2
askibinski commentedAs commented here: #2862910: Add template suggestions to twig debug the normal views suggestions do work. However, it's true that twig debug does not actually show these in the html comments.
That might be related to this core issue though.
#2923634: [PP-1] Use hook_theme_suggestions in views
Comment #3
mlncn commentedEVA is also somehow not producing field template suggestions on the node it is attached to. Say there are multiple EVAs on a content type, these are reorderable at Manage display, "EVA Example One", "EVA Example Two", "EVA Example Three". When output however in the template there is no distinction.
Where a regular field would have a suggestion such as
field--node--example-field.html.twig, an EVA pseudo-field only hascontainer.html.twig, and then goes toeva-display-entity-view.html.twig. This latter one should have the hidden Views template suggestions, but having field-level template suggestions in the pseudo-field would be very helpful simply for rearranging and grouping fields in the template.If there were a template suggestion from the pseudo field of
example-eva-entity-view-1, even if nothing in it, even if not used, it{{ content.example_eva_entity_view_1 }}
The above does work by the way!! The name of the pseudofield comes from the view machine name (
example_evain our case above) plus the display machine name (entity_view_1in our example).Comment #4
pakmanlhJust in case someone is trying to make the @prudloff snippet work, here you have what I finally ended up using:
I think there are two different templates / issues, one coming from the Views module directly which as @askibinski rightly mentioned can be override as usual even though the suggestions are not showed up, but also there is another template coming from the EVA module which can be tweaked as the example from above shows.
Not sure if this should be part of the main module, I am changing the issue status so the maintainers can decide how to proceed.
Thanks!
Comment #5
niklp commentedSnippet in #4 is the only thing I could find anywhere that did what I wanted, thanks.
Comment #9
vitaliyb98 commentedI created a MR with hook_theme_suggestions_HOOK_alter, tested it locally, and it looks good.
Tested with Drupal core 11.2
Comment #10
anirudhsingh19 commentedreviewing this!
Comment #11
anirudhsingh19 commentedI confirmed that there were no suggestions for eva_display_entity_view before the MR. The MR applied cleanly and after the MR, there were suggestions visible which includes the view id and current display name.
Moving this to RTBC. Attaching screenshots for reference.
Comment #12
damienmckennaFWIW this is working well for us on a prod site, thank you.
Comment #14
ahebrank commented