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,
    ]),
  ];
}

Issue fork eva-3223411

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

prudloff created an issue. See original summary.

askibinski’s picture

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.
#2923634: [PP-1] Use hook_theme_suggestions in views

mlncn’s picture

Version: 8.x-2.1 » 3.0.x-dev

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

pakmanlh’s picture

Status: Active » Needs review

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

niklp’s picture

Snippet in #4 is the only thing I could find anywhere that did what I wanted, thanks.

vitaliyb98 made their first commit to this issue’s fork.

vitaliyb98 changed the visibility of the branch 3223411-add-template-suggestions to hidden.

vitaliyb98’s picture

I created a MR with hook_theme_suggestions_HOOK_alter, tested it locally, and it looks good.

Tested with Drupal core 11.2

anirudhsingh19’s picture

reviewing this!

anirudhsingh19’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new57.49 KB
new70.62 KB

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

damienmckenna’s picture

FWIW this is working well for us on a prod site, thank you.

  • e3928e81 committed on 3.1.x
    Issue #3223411 by vitaliyb98: Add template suggestions
    
ahebrank’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.