You are here

function picture_field_formatter_picture_view in Picture 7.2

Helper function.

See also

picture_field_formatter_view()

File

./picture.module, line 863
Picture formatter.

Code

function picture_field_formatter_picture_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $settings = $display['settings'];

  // Check if the formatter involves a link.
  $image_link = $display['settings']['image_link'];
  if ($image_link == 'content') {
    if (isset($entity->referencing_entity) && isset($entity->referencing_entity_type)) {
      $uri = entity_uri($entity->referencing_entity_type, $entity->referencing_entity);
    }
    else {
      $uri = entity_uri($entity_type, $entity);
    }
  }
  elseif ($image_link == 'file') {
    $link_file = TRUE;
  }
  elseif ($image_link) {
    if (isset($entity->{$image_link})) {

      // Support for field translations.
      $language = field_language($entity_type, $entity, $image_link, $langcode);
      $link_field = $entity->{$image_link};
      if (isset($link_field[$language])) {
        $link_values = $link_field[$language];
      }
    }
    elseif (image_style_load($image_link)) {
      $link_file = $image_link;
    }
  }
  $fallback_image_style = $settings['fallback_image_style'];

  // Support old display settings from 7.x-1.x.
  $mapping_name = isset($display['settings']['picture_mapping']) && !empty($display['settings']['picture_mapping']) ? $display['settings']['picture_mapping'] : $display['settings']['picture_group'];

  // If we haven't saved a picture mapping for this field previously and
  // this isn't a field formatter screen (i.e. panels),
  // load a default mapping.
  $picture_mapping = FALSE;
  if (isset($mapping_name) && !empty($mapping_name)) {
    $picture_mapping = picture_mapping_load($mapping_name);
    if (!$picture_mapping) {
      trigger_error(check_plain("Unable to load picture mapping {$mapping_name}."), E_USER_ERROR);
      return $element;
    }
  }
  else {
    $all_mappings = picture_mapping_load_all();
    $picture_mapping = reset($all_mappings);
    if (!$picture_mapping) {
      trigger_error("No picture mappings have been defined yet.", E_USER_ERROR);
      return $element;
    }
  }
  $breakpoint_styles = picture_get_mapping_breakpoints($picture_mapping, $fallback_image_style);

  // Assume regular display.
  $formatter = 'picture_formatter';
  $colorbox_breakpoints = array();
  $colorbox_fallback_image_style = '';

  // Check for colorbox link.
  if (module_exists('colorbox') && $display['settings']['image_link'] == 'colorbox') {
    $formatter = 'picture_formatter_colorbox';
    $mappings = picture_mapping_load($display['settings']['colorbox_settings']['colorbox_group']);
    if (!$mappings) {
      trigger_error(check_plain("Unable to load picture mapping {$display['settings']['colorbox_settings']['colorbox_group']}."), E_USER_ERROR);
      return $element;
    }
    $colorbox_breakpoints = picture_get_mapping_breakpoints($mappings, $colorbox_fallback_image_style);
  }
  foreach ($items as $delta => $item) {
    if (isset($link_file)) {
      $uri = array(
        'path' => $link_file === TRUE ? file_create_url($item['uri']) : image_style_url($link_file, $item['uri']),
        'options' => array(),
      );
    }

    // Handle multiple link with image values.
    if (isset($link_values)) {
      if (isset($link_values[$delta]['url'])) {
        $uri = array(
          'path' => $link_values[$delta]['url'],
          'options' => array(
            'attributes' => $link_values[$delta]['attributes'],
          ),
        );

        // Handle query fragment if there is any.
        if (!empty($link_values[$delta]['query'])) {
          $uri['options']['query'] = $link_values[$delta]['query'];
        }
      }
      else {
        unset($uri);
      }
    }
    $libraries = array(
      array(
        'picture',
        'picturefill_head',
      ),
      array(
        'picture',
        'picturefill',
      ),
      array(
        'picture',
        'picture.ajax',
      ),
    );
    if (!empty($settings['lazyload'])) {
      $libraries[] = array(
        'picture',
        'lazysizes',
      );
      if (!empty($display['settings']['lazyload_aspect_ratio'])) {
        $libraries[] = array(
          'picture',
          'lazysizes_aspect_ratio',
        );
      }
    }
    $element[$delta] = array(
      '#theme' => $formatter,
      '#attached' => array(
        'library' => $libraries,
      ),
      '#item' => $item,
      '#image_style' => $fallback_image_style,
      '#breakpoints' => $breakpoint_styles,
      '#path' => isset($uri) ? $uri : '',
      '#colorbox_group' => $colorbox_breakpoints,
      '#colorbox_image_style' => $colorbox_fallback_image_style,
      '#lazyload' => !empty($settings['lazyload']),
      '#lazyload_aspect_ratio' => !empty($settings['lazyload_aspect_ratio']),
    );

    // Add css and js for colorbox.
    if ($formatter == 'picture_formatter_colorbox') {
      $element[$delta]['#attached']['css'][drupal_get_path('module', 'picture') . '/picture_colorbox.css'] = array(
        'type' => 'file',
      );
      $element[$delta]['#attached']['js'][drupal_get_path('module', 'picture') . '/picture_colorbox.js'] = array(
        'type' => 'file',
      );
      if (!variable_get('colorbox_inline', 0)) {
        $element[$delta]['#attached']['js'][drupal_get_path('module', 'colorbox') . '/js/colorbox_inline.js'] = array(
          'type' => 'file',
        );
      }
      $colorbox_settings = $display['settings']['colorbox_settings'];

      // Add the group ID.
      list($id) = entity_extract_ids($entity_type, $entity);
      $entity_id = !empty($id) ? $entity_type . '-' . $id : 'entity-id';

      // If this is a file entity field check for the referencing entity to do
      // the proper grouping.
      if (isset($entity->referencing_entity) && isset($entity->referencing_field)) {

        // Because we don't have the entity type we use some "magic" to get a
        // unique entity identifier.
        $entity_id = spl_object_hash($entity->referencing_entity);
        $colorbox_group_field = $entity->referencing_field;
      }
      else {
        $colorbox_group_field = $field['field_name'];
      }
      switch ($colorbox_settings['colorbox_gallery']) {
        case 'post':
          $gallery_id = 'gallery-' . $entity_id;
          break;
        case 'page':
          $gallery_id = 'gallery-all';
          break;
        case 'field_post':
          $gallery_id = 'gallery-' . $entity_id . '-' . $colorbox_group_field;
          break;
        case 'field_page':
          $gallery_id = 'gallery-' . $colorbox_group_field;
          break;
        case 'custom':
          $gallery_id = $colorbox_settings['colorbox_gallery_custom'];
          break;
        default:
          $gallery_id = '';
      }
      $element[$delta]['#colorbox_group_id'] = $gallery_id;

      // Add the caption.
      $entity_title = entity_label($entity_type, $entity);
      switch ($colorbox_settings['colorbox_caption']) {
        case 'auto':

          // If the title is empty use alt or the entity title in that order.
          if (!empty($item['title'])) {
            $caption = $item['title'];
          }
          elseif (!empty($item['alt'])) {
            $caption = $item['alt'];
          }
          elseif (!empty($entity_title)) {
            $caption = $entity_title;
          }
          else {
            $caption = '';
          }
          break;
        case 'title':
          $caption = $item['title'];
          break;
        case 'alt':
          $caption = $item['alt'];
          break;
        case 'node_title':
          $caption = $entity_title;
          break;
        case 'custom':
          $caption = token_replace($colorbox_settings['colorbox_caption_custom'], array(
            $entity_type => $entity,
            'file' => (object) $item,
          ), array(
            'clear' => TRUE,
          ));
          break;
        default:
          $caption = '';
      }

      // Shorten the caption for the example styles or when caption shortening
      // is active.
      $colorbox_style = variable_get('colorbox_style', 'default');
      $trim_length = variable_get('colorbox_caption_trim_length', 75);
      if ((strpos($colorbox_style, 'colorbox/example') !== FALSE || variable_get('colorbox_caption_trim', 0)) && drupal_strlen($caption) > $trim_length) {
        $caption = drupal_substr($caption, 0, $trim_length - 5) . '...';
      }
      $element[$delta]['#colorbox_caption'] = $caption;
    }
  }
  return $element;
}