How do I use 2 different taxonomy view modes on one node page?












3















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?










share|improve this question



























    3















    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?










    share|improve this question

























      3












      3








      3








      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?










      share|improve this question














      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?







      theming taxonomy-terms






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 9 '18 at 12:50









      IanIan

      487




      487






















          1 Answer
          1






          active

          oldest

          votes


















          4














          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.






          share|improve this answer


























          • Wow! without('#cache') did the trick. Thanks a lot for your help and explanation!

            – Ian
            Dec 10 '18 at 7:43











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "220"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdrupal.stackexchange.com%2fquestions%2f273748%2fhow-do-i-use-2-different-taxonomy-view-modes-on-one-node-page%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          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.






          share|improve this answer


























          • Wow! without('#cache') did the trick. Thanks a lot for your help and explanation!

            – Ian
            Dec 10 '18 at 7:43
















          4














          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.






          share|improve this answer


























          • Wow! without('#cache') did the trick. Thanks a lot for your help and explanation!

            – Ian
            Dec 10 '18 at 7:43














          4












          4








          4







          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.






          share|improve this answer















          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 9 '18 at 13:53

























          answered Dec 9 '18 at 13:26









          4k44k4

          50.5k55698




          50.5k55698













          • Wow! without('#cache') did the trick. Thanks a lot for your help and explanation!

            – Ian
            Dec 10 '18 at 7:43



















          • Wow! without('#cache') did the trick. Thanks a lot for your help and explanation!

            – Ian
            Dec 10 '18 at 7:43

















          Wow! without('#cache') did the trick. Thanks a lot for your help and explanation!

          – Ian
          Dec 10 '18 at 7:43





          Wow! without('#cache') did the trick. Thanks a lot for your help and explanation!

          – Ian
          Dec 10 '18 at 7:43


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Drupal Answers!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdrupal.stackexchange.com%2fquestions%2f273748%2fhow-do-i-use-2-different-taxonomy-view-modes-on-one-node-page%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Berounka

          Sphinx de Gizeh

          Different font size/position of beamer's navigation symbols template's content depending on regular/plain...