Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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