You are here

function media_gallery_item_view in Media Gallery 7

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

Constructs a drupal_render() array for a media entity displayed within a gallery.

During normal gallery node display, the Field Attach API ensures that all of its fields get rendered. Sometimes, however, we need to display just a single media entity (one item from the multi-valued "media_gallery_media" field) without displaying any other media entities within that field, and without displaying any other gallery node fields, but via a field formatter.

See also

media_gallery_detail_page()

media_gallery_lightbox_page()

2 calls to media_gallery_item_view()
media_gallery_detail_page in ./media_gallery.pages.inc
Menu callback; view a single gallery media entity as its own page.
media_gallery_lightbox_page in ./media_gallery.pages.inc
Menu callback; view a single gallery media item as the content of a lightbox.

File

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

Code

function media_gallery_item_view($gallery_node, $file, $file_view_mode) {
  $display = array(
    'type' => 'media_gallery',
    'settings' => array(
      'file_view_mode' => $file_view_mode,
    ),
  );
  $id = $gallery_node->nid;
  $field_name = 'media_gallery_media';
  $field = field_info_field($field_name);
  $instance = field_info_instance('node', $field_name, 'media_gallery');
  $langcode = key($gallery_node->{$field_name});
  $items = array(
    0 => array(
      'fid' => $file->fid,
      'file' => $file,
    ),
  );
  $items_multi = array(
    $id => &$items,
  );

  // Taken by reference, so can't inline.
  $formatter = field_info_formatter_types($display['type']);

  // "prepare_view" function is optional.
  $function = $formatter['module'] . '_field_formatter_prepare_view';
  if (function_exists($function)) {
    $function('node', array(
      $id => $gallery_node,
    ), $field, array(
      $id => $instance,
    ), $langcode, $items_multi, array(
      $id => $display,
    ));
  }

  // "view" function is mandatory, so deliberately break if it doesn't exist.
  $function = $formatter['module'] . '_field_formatter_view';
  $content = $function('node', $gallery_node, $field, $instance, $langcode, $items, $display);
  _media_gallery_attach_css_resources($content);
  return $content;
}