Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save frankyonnetti/39ef8984acfca879d16d8a4890095bb9 to your computer and use it in GitHub Desktop.
Save frankyonnetti/39ef8984acfca879d16d8a4890095bb9 to your computer and use it in GitHub Desktop.
Drupal 8 - loop entity reference fields #drupal #d8 #loop
Twig coding standards
https://www.drupal.org/node/1823416
Filters - Modifying Variables In Twig Templates
https://www.drupal.org/node/2357633
{% for i, value in content.field_app_prod_toggle_products %}
{% if node.field_app_prod_toggle_products[i].entity %}
{% if i == 0 %}
<div class="tabs-panel is-active" id="panel-0">
{% else %}
<div class="tabs-panel" id="panel-{{ i }}">
{% endif %}
<h2>{{ node.field_app_prod_toggle_products[i].entity.field_product_subhead.value }}</h2>
{{ node.field_app_prod_toggle_products[i].entity.body.value|t }}
<h3>{{ node.field_app_prod_toggle_products[i].entity.field_product_highlight_title.value }}</h3>
{{ node.field_app_prod_toggle_products[i].entity.field_product_highlight.value|t }}
<p class="explore-product-btn">
<a class="button" href="{{ path('entity.node.canonical', {'node': node.field_app_prod_toggle_products[i].entity.id}) }}">Explore <i class="fa fa-arrow-right" aria-hidden="true"></i></a>
</p>
</div>
{% endif %}
{% endfor %}
@phunguyen19
Copy link

Thanks Frank, your snippet helps me solve a problem with reference entity field.

@zhihongl
Copy link

zhihongl commented Dec 14, 2020

Also you can do this:

{% if content.field_app_prod_toggle_products['#items']|length > 0 %}
            <div class="row">
              {% for i, value in content.field_app_prod_toggle_products['#items'] %}
                {% if value.entity  %}
                  <div class="col col-3">
                    .......
                  </div>
                {% endif %}
              {% endfor %}
            </div>
{% endif %}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment