Skip to content

Instantly share code, notes, and snippets.

@acabouet
Last active February 27, 2018 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acabouet/d9a17849f7c5dbfedb88d7592c3017e7 to your computer and use it in GitHub Desktop.
Save acabouet/d9a17849f7c5dbfedb88d7592c3017e7 to your computer and use it in GitHub Desktop.
Drupal 8 - Get values as taxonomy term names from a taxonomy term reference field and print them out in a Twig template as classes. Especially handy for using with Isotope filters.
{# Create classes array. The 'node' class is required for contextual edit links. #}
{% set categories = [] %}
{% set classes = ['integration', 'node'] %}
{% for key, item in content.field_integration_cat if key|first != '#' %}
{% set categories = categories | merge([ item['#title'] | replace({' ':''}) ]) %}
{% endfor %}
{% for item in categories %}
{% set classes = classes | merge([item]) %}
{% endfor %}
<section {{ attributes.addClass(classes) }}>
</section>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment