-
Drupal + Twig: Render Taxonomy Terms and Comma-Separated List
Here’s a snippet that renders the terms in a multi-value taxonomy reference field (in a node template) as plain text items separated by commas. Yes, you could get the string into the desired format using PHP, but I wanted to try to do it with only Twig. The result is moderately readable.
Twig Code:
For a taxonomy term reference field called field_pub_tr_res_committees
123456789101112<dt class="pub__rescomms">{% trans %}Research Committee(s){% endtrans %}</dt><dd class="pub__rescomms">{%- for item in content.field_pub_tr_res_committees['#items'] -%}{%- if loop.first -%}{{- item.entity.name.value -}}{%- elseif loop.last -%}{{- ' and ' ~ item.entity.name.value -}}{%- else -%}{{- ', ' ~ item.entity.name.value -}}{%- endif -%}{%- endfor -%}</dd>Output:
If 1 value: My first term
-
Maintaining Lightbox2 Templating
Lightbox2 is a very popular and powerful Drupal module. One of the helpful features is that you can render a Drupal node in a lightbox rather easily in either PHP or HTML. View detailed explanation. The issue is that when you click a link within that lightboxed content, you will lose the lightbox2 templating (e.g., if your lightbox template hides sidebars, they’ll only be hidden when the node loads. If you click a link within the lightbox, the new node will load and sidebars will appear). Read on for a quick fix.