adrian-d of Rouge Devs
11/23/2016 - 8:55 AM

Loop through a field collection and return all of its fields

Loop through a field collection and return all of its fields

// Firstly, you will need to use field_get_items() to find the contents of the BEAN. This will allow you to loop through each one.

<?php $fb_fields = field_get_items('bean', $bean, '[field collection machine name]'); ?>

// This will allow you to reference a BEAN. You can change the relevant information to include a node.

<?php $fb_fields = field_get_items('node', $node, '[field collection machine name]'); ?>

// On its own, field_get_items doesn't do a great deal. You need to use the below to grab the identifiers that field_get_items gives you, and return actual content for them.

<?php
  $ids = array();
  foreach ($fb_fields as $fb_field) {
    $ids[] = $fb_field['value'];
  }
  $items = field_collection_item_load_multiple($ids);
?>

// Now loop through each one.

<?php
  foreach ($items as $item) {
    echo "<pre>";
    print_r($item);
    echo "</pre>";
  }
?>

This should give you everything you need to begin templating.