deckerdev.com

A collection of resources for Drupal 8 and Twig.

General Twig

Drupal Twig

Drupal Modules that extend Twig

Snippets

Get the value from a List (text) field:

{{ content.field_text_list['#items'].getString() }}

Traverse a multi-valued field:

{# yes, the following syntax sucks! see https://www.drupal.org/node/2776307 #}
{% set links = [] %}
{% for key, link in content.field_links if key|first != '#' %}
  {% set links = links|merge([ link ]) %}
{% endfor %}

Get the value from a text field:

{{ node.field_text.value }}
{{ node.field_text.value|striptags }}

Create a link to an entity reference:

{% set ref = node.field_entity_reference.entity %}
- or -
{% set ref = node.field_entity_reference.0.entity %}

<a href="{{ path('entity.node.canonical', {'node': ref.id}) }}">{{ ref.label }}</a>

Counting the items in multivalue fields:

{% set count = node.field_entity_reference.getvalue|length %}