You are here

function media_gallery_field_formatter_view in Media Gallery 7

Same name and namespace in other branches
  1. 7.2 media_gallery.fields.inc \media_gallery_field_formatter_view()

Implements hook_field_formatter_view().

File

./media_gallery.fields.inc, line 64
Field API integration for media_gallery.module.

Code

function media_gallery_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  if (!$items) {
    return $element;
  }
  $file_view_mode = $display['settings']['file_view_mode'];

  // The Formatter Reference module allows per-entity, rather than
  // per-bundle, formatter assignment, but it only works when the field being
  // formatted and the reference field are part of the same entity. For media
  // galleries, we want to enable formatter reference fields on the gallery node
  // to be used as formatters for fields within the file entities, so we pass
  // along those field values to them. Here we just collect all formatter
  // reference field names for the entity.
  $formatter_reference_fields = array();
  if (module_exists('formatter_reference')) {
    $fields = field_info_fields();
    list(, , $bundle) = entity_extract_ids($entity_type, $entity);
    foreach (field_info_instances($entity_type, $bundle) as $field_name => $instance_info) {
      if ($fields[$field_name]['type'] == 'formatter_reference') {
        $formatter_reference_fields[] = $field_name;
      }
    }
  }

  // Prepare the referenced file entities for viewing.
  $files = array();
  foreach ($items as $delta => $item) {

    // Don't try to display files which no longer exist.
    if (!isset($item['file'])) {
      unset($items[$delta]);
      continue;
    }
    $file = $item['file'];

    // Pass along formatter reference field values from the gallery to the file.
    foreach ($formatter_reference_fields as $field_name) {
      if (!isset($file->{$field_name}) && isset($entity->{$field_name})) {
        $file->{$field_name} = $entity->{$field_name};
      }
    }

    // Set a default value for the file's media_title field.
    // @todo Eventually, fix this to take into account the possibility of a
    //   multilingual media title field.
    if (isset($file->media_title) && !isset($file->media_title[LANGUAGE_NONE][0]['value'])) {
      $file->media_title[LANGUAGE_NONE][0]['value'] = _media_gallery_get_media_title($file);
    }
    $files[$file->fid] = $file;
  }
  field_attach_prepare_view('file', $files, $file_view_mode);
  entity_prepare_view('file', $files);

  // View each file. We don't use file_view_multiple(), because we need the
  // render array indexed by $delta rather than by file id.
  foreach ($items as $delta => $item) {
    $element[$delta] = file_view($item['file'], $file_view_mode, $langcode);
    $element[$delta]['#media_gallery_entity_type'] = $entity_type;
    $element[$delta]['#media_gallery_entity'] = $entity;
    switch ($file_view_mode) {
      case 'media_gallery_thumbnail':
        $element[$delta]['#theme'] = 'media_gallery_media_item_thumbnail';
        break;
      case 'media_gallery_lightbox':
        $element[$delta]['#theme'] = 'media_gallery_media_item_lightbox';
        break;
      case 'media_gallery_detail':
        $element[$delta]['#theme'] = 'media_gallery_media_item_detail';
        break;
      case 'media_gallery_block_thumbnail':
        $element[$delta]['#theme'] = 'media_gallery_block_thumbnail';
        break;
      case 'media_gallery_collection_thumbnail':
        $element[$delta]['#theme'] = 'media_gallery_collection_thumbnail';
        break;
    }
  }
  return $element;
}