On a node page I have a term reference field. This term contains 3 fields

For the term I've created a few view modes:

  1. view_mode_first contains: field_1
  2. view_mode_second contains: field_2, field_3

hook theme suggestions:

  function theme_theme_suggestions_taxonomy_term_alter(array &$suggestions, array $variables) {
    $term = $variables['elements']['#taxonomy_term'];
    array_splice($suggestions, 2, 0, 'taxonomy_term__' . $term->bundle() . '__' . $variables['elements']['#view_mode']);

node field template, where I'm displaying referenced terms:

  {% for item in items %}
      {% set first = item.content|merge({'#view_mode': 'view_mode_first'}) %}
      {{ first }}
  {% endfor %}

  {% for item in items %}
      {% set second = item.content|merge({'#view_mode': 'view_mode_second'}) %}
      {{ second }}
  {% endfor %}

But as a result I'm getting the same templates everywhere: taxonomy-term--name--view-mode-first.html.twig and not a taxonomy-term--name--view-mode-second.html.twig

I found that if I disable render cache in a settings.local file, both templates work:

$settings['cache']['bins']['render'] = 'cache.backend.null';

Is there possibility to disable render cache for specific node and/or fields?


1 Answer 1


The problem is that the view mode is also part of the cache keys and you would need to update them as well to avoid that both view modes are cached under the same cache id.

Is there possibility to disable render cache for specific node and/or fields?

Yes, this is another possibility, you can disable caching of specific entities, in this case taxonomy terms, by removing the cache keys, which are in the #cache property. It's easier to manipulate this array in PHP, but this should also be possible in Twig. You can try to get #cache with the filter |without('keys') and merge it later with first and second.

You can also try to remove #cache completely from the second rendering

{{ second|without('#cache') }}

because all relevant cache metadata should bubble up already from the first rendering of the taxonomy term.

  • Wow! without('#cache') did the trick. Thanks a lot for your help and explanation!
    – Ian
    Dec 10, 2018 at 7:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.