You are here

function panopoly_widgets_field_formatter_view in Panopoly Widgets 7

Implements hook_field_formatter_view().

File

./panopoly_widgets.spotlight.inc, line 153
A specification for the custom spotlight entity that is part of Panopoly Widgets

Code

function panopoly_widgets_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {

  // Include the Spotlight Rotation time setting
  $duration_field = field_get_items('fieldable_panels_pane', $entity, 'field_basic_spotlight_duration');
  if ($duration_field) {
    $duration_field = reset($duration_field);
    $duration = $duration_field['value'];
  }
  if (empty($duration)) {
    $duration = variable_get('panopoly_widgets_spotlight_rotation_time', 5);
  }
  $pager_style = 'full';

  // Default to the Full pager if field isn't filled out (i.e., legacy data).
  $pager_style_field = field_get_items('fieldable_panels_pane', $entity, 'field_basic_spotlight_pager');
  if ($pager_style_field) {
    $pager_style_field = reset($pager_style_field);
    $pager_style = $pager_style_field['value'];
  }
  $element = array();
  $settings = $display['settings'];
  $formatter = $display['type'];

  // Load the images and filter out slides with missing images.
  foreach ($items as $delta => $item_data) {
    if (!empty($item_data['uuid']) && module_exists('uuid')) {
      $image_entity = entity_uuid_load('file', array(
        $item_data['uuid'],
      ));
      $image = file_load(array_pop($image_entity)->fid);
    }
    else {
      $image = (object) $item_data;
    }
    if (empty($image->uri) || !file_exists($image->uri)) {
      unset($items[$delta]);
    }
    else {
      $items[$delta]['image'] = $image;
    }
  }

  // Assemble the spotlight wrapper
  // $element[0] rather than $element because hook_field_formatter_view()
  // expects a renderable array for the $items, as an array of child elements
  // keyed by numeric indexes starting from 0.
  $element[0] = array(
    '#theme' => 'panopoly_spotlight_wrapper',
    '#items' => $items,
    '#settings' => $settings,
    '#formatter' => $formatter,
    '#duration' => $duration,
    '#pager_style' => $pager_style,
    '#entity_type' => $entity_type,
    '#entity' => $entity,
    'slides' => array(),
  );

  // Assemble the spotlight items (rendered in panopoly_spotlight_wrapper())
  foreach ($items as $delta => $item_data) {
    if (isset($item_data['field_title'])) {
      $item_data['title'] = $item_data['field_title'];
    }
    if (isset($item_data['field_alt'])) {
      $item_data['alt'] = $item_data['field_alt'];
    }
    $element[0]['slides'][] = array(
      '#theme' => 'panopoly_spotlight_view',
      '#items' => $item_data,
      '#delta' => $delta,
      '#settings' => $settings,
      '#entity_type' => $entity_type,
      '#entity' => $entity,
    );
  }
  return $element;
}