Add template suggestions

Created on 13 July 2021, almost 3 years ago
Updated 14 September 2023, 10 months ago

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,
    ]),
  ];
}
✨ Feature request
Status

Needs review

Version

3.0

Component

Code

Created by

πŸ‡«πŸ‡·France prudloff Lille

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡³πŸ‡±Netherlands askibinski

    As 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.
    πŸ“Œ [PP-1] Use hook_theme_suggestions in views Postponed

  • πŸ‡ΊπŸ‡ΈUnited States mlncn Minneapolis, MN, USA

    EVA 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 has container.html.twig, and then goes to eva-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_eva in our case above) plus the display machine name (entity_view_1 in our example).

  • Status changed to Needs review 10 months ago
  • πŸ‡ͺπŸ‡ΈSpain pakmanlh Barcelona

    Just in case someone is trying to make the @prudloff snippet work, here you have what I finally ended up using:

    /**
     * Implements hook_theme_suggestions_HOOK_alter().
     */
    function foo_theme_suggestions_eva_display_entity_view_alter(array &$suggestions, array $variables) {
      /** @var \Drupal\views\ViewExecutable $view */
      $view = $variables['view'];
    
      $suggestions[] = implode('__', [
        $variables['theme_hook_original'],
        $view->id(),
      ]);
      $suggestions[] = implode('__', [
        $variables['theme_hook_original'],
        $view->id(),
        $view->current_display,
      ]);
    }
    

    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.
    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!

Production build 0.69.0 2024