You are here

function backstretch_field_formatter_view in Backstretch 7.2

Implements hook_field_formatter_view().

File

./backstretch.module, line 311
Main file for Backstretch Formatter module.

Code

function backstretch_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  if (!count($items)) {
    return '';
  }

  // This is where the output will be.
  $element = array();

  // Store the field formatter settings.
  $settings = $display['settings'];

  // We store the default values from Backstretch formatter here.
  $formatters = backstretch_field_formatter_info();
  $default = $formatters['backstretch']['settings'];

  // We need the js variable name.
  $options_info = backstretch_formatter_options();

  // Here we store all options later.
  $options = array();
  foreach ($settings as $name => $value) {
    if (array_key_exists($name, $options_info)) {
      $option = $options_info[$name];
      $js = isset($option['js']) ? $option['js'] : '';

      // We need some special handling with the element settings.
      if ($name == 'element' || $name == 'element_other' && $settings['element'] == '') {
        continue;
      }

      // Replace tokens in the "Other element" field.
      if ($name == 'element_other') {
        $value = token_replace($value, array(
          $entity_type => $entity,
        ));
      }

      // We only put the setting into $options when it is
      // not the default value.
      if ($value != $default[$name] && $js) {
        $options[$js] = $value;
      }

      // The fade option has to be an integer otherwise it doesn't work.
      if ($name == 'fade' && $value != $default[$name] && is_numeric($value) && intval($value) == $value) {
        $options[$name] = (int) $options[$name];
      }
    }
  }

  // We need the entity id here because there could be multiple images with
  // Backstretch.
  $ids = entity_extract_ids($entity_type, $entity);
  $id = drupal_clean_css_identifier($entity_type . '-' . $ids[0]);
  if ($settings['random']) {
    $random_key = array_rand($items);
    $items = array(
      $items[$random_key],
    );
  }

  // Special handling for entity reference fields.
  if ($field['type'] == 'entityreference') {

    // Store the set entity type.
    $target_entity_type = $field['settings']['target_type'];

    // We store all images from referenced entities here temporary.
    $new_items = array();

    // Iterate all entity references.
    foreach ($items as $item) {
      $target_id = $item['target_id'];
      $target_entity = entity_load($target_entity_type, array(
        $target_id,
      ));
      $target_entity = $target_entity[$target_id];

      // The image field values are here.
      $target_items = field_get_items($target_entity_type, $target_entity, $settings['field']);

      // Iterate and put them into temporary array.
      foreach ($target_items as $target_item) {
        $new_items[] = $target_item;
      }
    }

    // Finally put collected items in real items array.
    $items = $new_items;
  }

  // If a delta was set we only use that one image.
  if ($settings['delta']) {
    $key = $settings['delta'] - 1;
    if (array_key_exists($key, $items)) {
      $items = array(
        $items[$key],
      );
    }
  }

  // Iterate all items and store the absolute url to it.
  foreach ($items as &$item) {
    $uri = $item['uri'];

    // Get url to image.
    if ($settings['image_style']) {
      $url = image_style_url($settings['image_style'], $uri);
    }
    else {
      $url = file_create_url($uri);
    }
    $options['items'][] = $url;
  }

  // Prepare a renderable array.
  $element[0] = array(
    '#theme' => 'backstretch',
    '#id' => $id,
    '#options' => $options,
  );
  return $element;
}