At the moment we can create twig files for nodes (and I think other entities) based on their view mode.

I'd like to do the same for taxonomy terms.

The specific use case I had for this was that I had a term with an icon (image field) attached and wanted to show this icon only, but the present twig file shows the {{ label }} and I didn't want to use css display: none to hide it.

Theme suggestions for taxonomy view modes

Patch will be applied in the comments.

Comments

markconroy created an issue. See original summary.

markconroy’s picture

Adding patch.

markconroy’s picture

Status: Active » Needs review

Changing status to 'needs review'

markconroy’s picture

Adding author to commit message.

markconroy’s picture

Issue summary: View changes
StatusFileSize
new70.08 KB
zerolab’s picture

Attaching a new patch that is sanitising the view mode, the same way node_theme_suggestions_node() does.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

fabianx’s picture

Status: Needs review » Reviewed & tested by the community

RTBC, but core committers might ask for a test for this.

lauriii’s picture

Version: 8.2.x-dev » 8.3.x-dev

FYI:

  1. I'm moving this for 8.3.x and committers can decide if this could be included in 8.2.x also.
  2. This could possibly face similar problems as this #2229355: Field template suggestions are colliding why it might make sense to figure out that issue first.
catch’s picture

Status: Reviewed & tested by the community » Needs review

This is very similar to #2766379: Add User view mode template suggestions, is it worth adding these centrally somewhere? Also we should look at #9.2 here as well.

lauriii’s picture

Status: Needs review » Closed (duplicate)

Sorry folks, but I think this should be fixed here instead: #2270883: Automatically add theme hook suggestions for all entity types

markconroy’s picture

Hi Laurii,

Agree. Would be better to have an auto-template generator and not try to fix these on an entity-by-entity basis. Happy to keep this closed.

enrico.sato’s picture

Hi,
I reorder suggestions of #6 to have view_mode before and make suggestion more generic. I hope it will help!

enrico.sato’s picture

StatusFileSize
new861 bytes

Fixed #13, sorry

kbeck303’s picture

I have tested the patch in comment #14 on drupal version 8.5.1 and the taxonomy term theme suggestions by view mode are displaying as excepted.

23 = tid
audience = vocabulary machine name
teaser = view mode

<!-- FILE NAME SUGGESTIONS:
   * taxonomy-term--23--teaser.html.twig
   * taxonomy-term--23.html.twig
   x taxonomy-term--audience--teaser.html.twig
   * taxonomy-term--audience.html.twig
   * taxonomy-term--teaser.html.twig
   * taxonomy-term.html.twig
-->
bdanin’s picture

confirmed #14 works

kerby70’s picture

Here's a workaround. Since the other task isn't complete, and this patch will not be going in.

/**
 * Implements hook_theme_suggestions_taxonomy_term_alter().
 */
function YOURTHEME_theme_suggestions_taxonomy_term_alter(array &$suggestions, array $variables) {
  /** @var \Drupal\taxonomy\TermInterface $term */
  $term = $variables['elements']['#taxonomy_term'];
  $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
  // Add view mode theme suggestions.
  $suggestions[] = 'taxonomy_term__' . $sanitized_view_mode;
  $suggestions[] = 'taxonomy_term__' . $term->bundle() . '__' . $sanitized_view_mode;
  $suggestions[] = 'taxonomy_term__' . $term->id() . '__' . $sanitized_view_mode;
}
vadim.jin’s picture

#17 works for me. Thanks. Just add to the theme file and build any suggestion that you want.

shamsher_alam’s picture

#17 Works for me. Thanks

alexpertsi’s picture

A different approach that you could follow is to create
taxonomy-term--services.html.twig (services = vocabulary machine name) for your vocabulary and check (if view_mode == 'teaser') the machine name of your view modes

{% if view_mode == 'teaser' %}
     <div class="svglink">
         {{ content.field_svgimg }}
     </div>
{% endif %}


{% if view_mode == 'full' %}

  {%
    set classes = [ 'taxonomy-term',  'vocabulary-' ~ term.bundle|clean_class, ]
  %}

  <div{{ attributes.setAttribute('id', 'taxonomy-term-' ~ term.id).addClass(classes) }}>
    {{ title_prefix }}
    {% if not page %}
      <h2><a href="{{ url }}">{{ name }}</a></h2>
    {% endif %}
    {{ title_suffix }}
    <div class="content">
      {{ content }}
    </div>
  </div>
  
{% endif %}
stefan.korn’s picture

Assigned: markconroy » Unassigned

Hm, wondering why not getting #14 in. It's more than three years since the issue was opened and no more general solution seems to be in sight anytime soon. So why not having it the same way that node does it for taxonomy too, maybe with a ToDo hint to remove it if a more general solutions will finally be available.

Even taxonomy_theme_suggestions_taxonomy_term() is already there and only needs to be amended slightly, so the risk with this change seems to be very small.

And regarding tests: At this moment taxonomy module does no tests on the suggestions although suggestions are already created. But if only tests are hindering, than I could maybe provide them analogous to how node does it.

Kindly asking maintainers to maybe have a look on this issue again.

mlncn’s picture

Agreed with Stefan.

Contrib solution for now: Twig Template Suggester module

ptmkenny’s picture

For those of us who are digging through issues trying to find what the latest status is, some work going on here: https://www.drupal.org/project/drupal/issues/2808481

jedgar1mx’s picture

#17 works great 👍

amir jamshidi’s picture

#17 Works for me. Thanks

swirt’s picture

#14 works great.

It is nice that this has been closed as a duplicate of https://www.drupal.org/project/drupal/issues/2270883 BUT that issue has been bogged down since 2014. Similarly in https://www.drupal.org/project/drupal/issues/2808481 also stuck in a bog.

gaëlg’s picture

#14 doesn't seem to apply on latest core but #17 is the way to go for now.

alex.bukach’s picture

Re-rolled #14.