0

I have an array of 5 objects. I would like to loop throught the 5 objects and display fields from within.

enter image description here

I have a for loop in place that is returning the appropriate fields, but it is displaying the same object 5 times.

{% for key, object in featured_resource_nodes %}

  {% if node.field_featured_resources[0].entity.field_resource_type.value == 'whitepaper' %}
    <span class="icon-icons_whitepaper"></span>
  {% elseif node.field_featured_resources[0].entity.field_resource_type.value == 'Datasheet' %}
    <span class="icon-icons_datasheet"></span>
  {% elseif node.field_featured_resources[0].entity.field_resource_type.value == 'blog' %}
    <span class="icon-icons_blog"></span>
  {% elseif node.field_featured_resources[0].entity.field_resource_type.value == 'video' %}
    <span class="icon-icons_video"></span>
  {% elseif node.field_featured_resources[0].entity.field_resource_type.value == 'analystreport' %}
    <span class="icon-icons_analyst-report"></span>     
  {% endif %} 

  {% if node.field_featured_resources[0].entity.field_resource_type.value == 'video' %}    
    <a href="{{ featured_resource_nodes[0].field_on_demand_webinar[0].data[0] }}"><h3 class="text-center">{{ node.field_featured_resources.entity.title.value }}</h3></a> 
  {% else %}
    <a href="{{ file_url(node.field_featured_resources.entity.field_resource_file[0].entity.uri.value) }}"><h3 class="text-center">{{ node.field_featured_resources.entity.title.value }}</h3></a> 
  {% endif %}

{% endfor %}

But this is what is being returned

enter image description here

How do I loop through the objects and have it display the 5 individual results?

1

It is displaying the same object 5 times because node.field_featured_resources[0] is referenced 5 times. Use the iterator variable object or use node.field_featured_resources[key] instead.

It also may be better to do this without all of the logic in the template. Off of the top of my head,

  • In template preprocess for the field (PHP), set the class in Attribute.
  • In module layer (PHP), add a Field Formatter.
    • Or if Field Formatter are already available, configure Manage Display to display the fields in the desired format.
  • In template (Twig), print out the field value in the span directly.

Here's some Twig:

{% for key, object in featured_resource_nodes %}
  <span class="icon-icons_{{ object.entity.field_resource_type.value }}"></span>

  {% if object.entity.field_resource_type.value == 'video' %}
    {# @todo: Configure Field Display for On-Demand Webinar instead... #}
    <a href="{{ object.field_on_demand_webinar[0].data[0] }}">
      {# @todo: consider object.entity.title instead of .value #}
      <h3 class="text-center">{{ object.entity.title.value }}</h3>
    </a>
  {% else %}
    {# @todo: Configure Field Display for Resource File instead... #}
    <a href="{{ file_url(object.entity.field_resource_file[0].entity.uri.value) }}">
      {# @todo: consider object.entity.title instead of .value #}
      <h3 class="text-center">{{ object.entity.title.value }}</h3>
    </a> 
  {% endif %}
{% endfor %}
  • some notes: a) seems like you don't need key right now b) I'd recommend item as variable instead of object. the thing you're looping on is a field item. c) you seem to try and access some fields on the field item, you either need to follow the reference or use the node, not sure where those actually are. – Berdir Mar 29 '16 at 23:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.