Last active
February 16, 2021 18:01
-
-
Save frankyonnetti/39ef8984acfca879d16d8a4890095bb9 to your computer and use it in GitHub Desktop.
Drupal 8 - loop entity reference fields #drupal #d8 #loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
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
Thanks Frank, your snippet helps me solve a problem with reference entity field.