[PP-1] Use hook_theme_suggestions in views

Created on 15 November 2017, over 6 years ago
Updated 11 February 2024, 4 months ago

Problem/Motivation

Views currently does not show theme suggestions, due to a bug in core: 🐛 Twig debug output does not display all suggestions when an array of theme hooks is passed to #theme Needs work
Mark Carver though pointed out in #2118743-167: Twig debug output does not display all suggestions when an array of theme hooks is passed to #theme that views could also use hook_theme_suggestions

Proposed resolution

Use hook_theme_suggestions() instead of arrayed theme hooks.

Remaining tasks

Postponed till Add a "context" array variable to all theme hooks and "#context" array property to all elements to provide optional contextual data Needs work is finished.

User interface changes

None

API changes

Deprecates ViewsPluginInterface::themeFunctions and ViewExecutable::buildThemeFunctions and replaces them with equivilent themeSuggestions methods.

Data model changes

None

📌 Task
Status

Postponed

Version

11.0 🔥

Component
Views 

Last updated about 2 hours ago

Created by

🇩🇪Germany dawehner

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.

  • 🇩🇪Germany captain hindsight

    Just a quick question on this, I couldn't find a more appropriate issue. I wondered why ViewExecutable::buildThemeFunctions prefers (or even involves) the display_id as first argument over the view id.

    $themes[] = $hook . '__' . $display['id'];
    ...
    $themes[] = $hook . '__' . $id;
    

    Is this intended? For me this looks a little bit like gambling, to even involve display_id as first argument. I stumbled on this after using a "views-view--something.html.twig" and wondered why the display of a view matched another html.twig, because its display id matched it, which I never expected...

  • 🇫🇷France aiphes

    Hi,

    So @captain hindsight , do it be necessary to patch to get views template suggestions with display id or just adding code into .theme file ? Something like:
    $suggestions[] = 'views_view__' . $variables['view']->id(). '__' . $display['id'];

  • 🇧🇷Brazil julio_retkwa

    #83 it's working fine for me. Thanks !

  • last update about 1 year ago
    Patch Failed to Apply
  • 🇨🇭Switzerland tcrawford

    I am currently upgrading a project to Drupal 10 and so I have re-rolled #84 for D10.

    The patch (2923634-92.patch) is identical as #84, except for removal of the changes to /core/modules/aggregator/src/Plugin/views/row/Rss.php, due to the removal of the aggregator module from core. I have not created an interdiff as the first patch no longer applies on D10.

    I hope this assists if you are currently upgrading to D10. As the issue is postponed I have not created a patch against D11.

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MariaDB 10.3.22
    last update about 1 year ago
    Patch Failed to Apply
  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update 12 months ago
    Patch Failed to Apply
  • 🇮🇳India manikandank03 Tamil Nadu

    @tcrawford, thanks for the D10 patch.
    #92 patch cleanly applied Drupal 10.1.1.

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MariaDB 10.3.22
    last update 9 months ago
    Patch Failed to Apply
  • miiimooo Europe

    With the patch in #70 against 9.x I got this for example for an exposed views block:

    <!-- THEME HOOK: 'form' -->
    <!-- FILE NAME SUGGESTIONS:
       * form--audio-series-listing--block-1.html.twig
       * form--block-1.html.twig
       x form--audio-series-listing--block.html.twig
       * form--block.html.twig
       * form--audio-series-listing.html.twig
       * form.html.twig
       * form.html.twig
    -->
    

    With the latest patches here against 10.x I only get

    <!-- THEME DEBUG -->
    <!-- THEME HOOK: 'form' -->
    <!-- BEGIN OUTPUT from 'themes/contrib/stable/templates/form/form.html.twig' -->
    

    Going back to #83 against 10.x brings back the theme suggestions (form---VIEW_ID--DISPLAY_ID.html.twig,..) (though it doesn't apply cleanly due to the patch for core/modules/aggregator/src/Plugin/views/row/Rss.php)

    Something went missing in #92

  • last update 9 months ago
    Custom Commands Failed
  • @miiimooo opened merge request.
  • miiimooo Europe

    Re-rolled patch from #83 to apply cleanly against 10.1.x

  • 🇺🇸United States ben.hamelin Adirondack Mountains, NY

    I did not dig in to this much, but just noting that during an recent upgrade to 10.1 (from 9.5) this patch was breaking suggestions for views - existing template overrides (e.g. views-view-list--[machine_name]) were not being used. Removing the patch resolved for me.

  • Status changed to Needs work 8 months ago
  • 🇺🇦Ukraine Abyss

    This doesn't work quite right for Drupal 10.

    Example:

    1. We have one content type that is displayed in 2 views with the same view mode.
    2. The views are placed on different pages.
    3. Create templates for the content from these views.
    4. Open page 1 and we have the expected result.
    5. Open page 2 and the content that has already been displayed on page 1 is displayed with the template for the first view.
  • Status changed to Postponed 8 months ago
  • miiimooo Europe

    @ben.hamelin Have you tested wit the re-rolled patch from #96? I did that re-roll because previous patches were broken after upgrading to 10.1

  • 🇺🇸United States webdrips

    #92 works for me (Drupal 10.1.5), thanks.

  • 🇩🇰Denmark ressa Copenhagen

    The patch applies with som difficulties against 10.2.2, but I don't get file name suggestions for Views table:

    <!-- THEME DEBUG -->
    <!-- THEME HOOK: 'views_view_table' -->
    <!-- BEGIN OUTPUT from 'core/themes/olivero/templates/views/views-view-table.html.twig' -->
    <table class="views-table cols-1">
    

    Applying the patch:

    $ patch -p1                < 4947.diff 
    patching file core/modules/comment/src/Plugin/views/row/Rss.php
    patching file core/modules/node/src/Plugin/views/row/Rss.php
    patching file core/modules/search/src/Plugin/views/row/SearchRow.php
    patching file core/modules/views/src/Form/ViewsExposedForm.php
    Hunk #1 succeeded at 136 (offset -8 lines).   <<<<<< A few of these, as well as "succeeded at 63 with fuzz 1"
    [...]
Production build 0.69.0 2024