eternalistic
6/3/2015 - 10:37 PM

Iterate through all attached field_collections

Iterate through all attached field_collections

<?php

  // Iterate through all attached field_collections
  foreach ($node->field_section['und'] as $delta => $value) {
    $fc_entity_id = $value['value'];

    // If a background image exists, set it to be the background.
    if (isset($vars['content']['field_section']['' . $delta .'']['entity']['field_collection_item']['' . $fc_entity_id . '']['field_section_background']['0']['#item'])) {
      $bg_image = $vars['content']['field_section']['' . $delta .'']['entity']['field_collection_item']['' . $fc_entity_id . '']['field_section_background'][0]['#item']['uri'];
      $bg_image_path = file_create_url($bg_image);
      if (!empty($bg_image)) {
        if ($vars['content']['field_section']['' . $delta .'']) {
          $vars['content']['field_section']['' . $delta .'']['#prefix'] = '<div class="section-bg" style="background-image:url(' . $bg_image_path . ');">';
          $vars['content']['field_section']['' . $delta .'']['#suffix'] = '</div>';
        }
      }
      // Unset background image.
      unset ($vars['content']['field_section']['' . $delta .'']['entity']['field_collection_item']['' . $fc_entity_id . '']['field_section_background']);
    }
  }