0

In Drupal 8.4.4 I have a "Living Things" taxonomy vocabulary. One field on the terms in the vocabulary is "Most Common Name", with machine name field_most_common_name. On my "Animalia" term its value would be "Animal".

In Views, I have a "Taxon Media" view which lists media related to the taxonomy term. I display it in a block on the taxonomy term's pages themselves (not on nodes). In the view I group media by another field, "Primary Taxon", which is a reference to the term. It prints on the page as the grouping heading (e.g. "Animalia").

How can I print the value of my "Most Common Name" field next to that title? i.e. "Animalia Animal".

I have made a template, views-view-grouping--taxon-media.html.twig, cloned from the one in the Classy template, tested that works, then added the 4th line and tried a few other things such as content.field_most_common_name, but cannot get the field's value to display:

<div class="view-grouping">
  <div class="view-grouping-header">
    {{ title }}
    {{ field_most_common_name }}
  </div>
  <div class="view-grouping-content">{{ content }}</div>
</div>
  • You should create that title in your view (grouping). So instead of providing your template and the template variables, better provide details of your view settings. – Mario Steinitz Jan 11 '18 at 3:11
  • Thank you again @MarioSteinitz ! Your hint got me there. I will delete the "dump" part of my question, which is irrelevant, and accept my answer tomorrow when I'm allowed to. – Nick Hope Jan 11 '18 at 7:37
0

Thanks to Mario's helpful hint in the first comment, I was able to do this in the View and not in the twig template:

  1. In the FIELDS section of the View, add "Most Common Name" as a field.
  2. Select "Exclude from display", because I only want it in the title, not the results themselves. (3. Select "Link to the Taxonomy term", because I want it to be a link.)
  3. Next to "FIELDS", under the "Add" drop-down menu, choose "Rearrange".
  4. Drag "(field_primary_taxon: Taxonomy term) Taxonomy term: Most Common Name" above "Media: Primary Taxon" > Apply
  5. Click "Media: Primary Taxon [hidden]" field
  6. Check "Override the output of this field with custom text"
  7. REWRITE RESULTS > and copy the required tokens from the "REPLACEMENT PATTERNS" list, and paste into the "Text" area. I also inserted brackets outside of those tokens, so my Text area looks like this:

    {{ field_primary_taxon }} ( {{ field_most_common_name }} )

  8. Apply > Save > Clear all caches if necessary > Refresh the page.

  9. Title now in my example is "Animalia (Animal)". "Animalia" and "Animal" are links to the taxonomy page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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