Skip to content

Instantly share code, notes, and snippets.

@schnippy
Last active October 8, 2019 03:23
Show Gist options
  • Save schnippy/3f53fe2ac5c847bed26e808d42210feb to your computer and use it in GitHub Desktop.
Save schnippy/3f53fe2ac5c847bed26e808d42210feb to your computer and use it in GitHub Desktop.
Drupal 8 Keyword list in Twig

Modified this helpful recipe from AgileAdam to be able to take a simple Drupal 8 taxonomy term reference field (field_ct_category), iterate through it and add links to the keyword pages, ex:

Topics: Energy, Immigration and Transportation

<dd class="keywords">
  Topics:&nbsp;{%- for item in content.field_ct_category['#items'] -%}
    {%- if loop.first -%}
      <a href="{{ path('entity.taxonomy_term.canonical', {'taxonomy_term': item.entity.tid.value}) }}">{{- item.entity.name.value -}}</a>
    {%- elseif loop.last -%}
      and&nbsp;<a href="{{ path('entity.taxonomy_term.canonical', {'taxonomy_term': item.entity.tid.value}) }}">{{- item.entity.name.value -}}</a>
    {%- else -%}
      <a href="{{ path('entity.taxonomy_term.canonical', {'taxonomy_term': item.entity.tid.value}) }}">{{- ', ' ~ item.entity.name.value -}}</a>&nbsp;
    {%- endif -%}
  {%- endfor -%}
</dd>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment