function media_gallery_view in Media Gallery 7
Same name and namespace in other branches
- 7.2 media_gallery.module \media_gallery_view()
Implements hook_view().
File
- ./
media_gallery.module, line 263
Code
function media_gallery_view($node, $view_mode) {
// Add display elements and resources for users who can edit the gallery.
if (node_access('update', $node)) {
// Add the "Add images" link, themed as a local action. Note that this
// element is listed in hook_field_extra_fields(), so whether or not it
// will *actually* be displayed in the current view mode depends on the
// site's configuration for the corresponding pseudo-field.
$node->content['add_media_link'] = array(
'#theme' => 'menu_local_action',
'#link' => array(
'title' => t('Add media'),
'href' => 'media/browser',
'localized_options' => array(
'query' => array(
'render' => 'media-popup',
),
'attributes' => array(
'class' => array(
'media-gallery-add',
'launcher',
),
),
),
),
// @todo Drupal could really use a theme_menu_local_actions() function...
'#prefix' => '<ul class="field action-links">',
'#suffix' => '</ul>',
);
// Prevent the overlay module to open an additional dialog.
if (module_exists('overlay')) {
$node->content['add_media_link']['#link']['localized_options']['attributes']['class'][] = 'overlay-exclude';
}
// Enable the "Add media" link to launch the media browser.
module_load_include('inc', 'media', 'includes/media.browser');
media_attach_browser_js($node->content['add_media_link']);
$node->content['add_media_link']['#attached']['js'][] = drupal_get_path('module', 'media_gallery') . '/media_gallery.addimage.js';
// These JS settings are used by the "add media" link but some are also
// shared by the drag-and-drop code below.
$instance = field_info_instance('node', 'media_gallery_media', $node->type);
$token = drupal_get_token('media_gallery');
$gallery_js_settings = array(
'mediaGalleryAddImagesUrl' => url('media-gallery/add-images/' . $node->nid . '/' . $token),
'mediaGallerySortGalleryUrl' => url('media-gallery/sort/gallery/' . $node->nid . '/' . $token),
'mediaGalleryAllowedMediaTypes' => array_filter($instance['widget']['settings']['allowed_types']),
);
// When viewing the full node, add front-end resources for drag-and-drop
// sorting.
if ($view_mode == 'full') {
drupal_add_css(drupal_get_path('module', 'media_gallery') . '/media_gallery.dragdrop.css');
drupal_add_library('system', 'ui.sortable');
drupal_add_library('system', 'jquery.bbq');
drupal_add_js(drupal_get_path('module', 'media_gallery') . '/media_gallery.dragdrop.js');
drupal_add_js($gallery_js_settings, array(
'type' => 'setting',
));
}
else {
// Otherwise, attach the setting to the "add media" link, as per above.
$node->content['add_media_link']['#attached']['js'][] = array(
'type' => 'setting',
'data' => $gallery_js_settings,
);
}
}
// For the teaser, only the first thumbnail needs to be displayed, so remove the
// rest from the node's field data, so that the field formatter doesn't waste
// time building the render structure for items that won't be shown.
if ($view_mode == 'teaser') {
if (!empty($node->media_gallery_media[LANGUAGE_NONE])) {
if (media_access('view')) {
$first_item = array_shift($node->media_gallery_media[LANGUAGE_NONE]);
$node->media_gallery_media[LANGUAGE_NONE] = array(
$first_item,
);
}
else {
$node->media_gallery_media[LANGUAGE_NONE] = array();
}
}
}
elseif ($view_mode == 'full' || $view_mode == 'media_gallery_block') {
$full = $view_mode == 'full' ? TRUE : FALSE;
if (!empty($node->media_gallery_media) && media_access('view')) {
$media = $node->media_gallery_media[LANGUAGE_NONE];
}
else {
$media = array();
}
// Only display the requested number of media items per page.
$columns = $full ? $node->media_gallery_columns[LANGUAGE_NONE][0]['value'] : $node->media_gallery_block_columns[LANGUAGE_NONE][0]['value'];
$rows = $full ? $node->media_gallery_rows[LANGUAGE_NONE][0]['value'] : $node->media_gallery_block_rows[LANGUAGE_NONE][0]['value'];
$number_per_page = $columns * $rows;
$deltas = array_keys($media);
$total = count($deltas);
// Initialize the pager and find out what page we are viewing.
$page = $full ? pager_default_initialize($total, $number_per_page, MEDIA_GALLERY_PAGER_ELEMENT) : 0;
// Deny access to all media items that aren't on this page.
$min_on_page = $number_per_page * $page;
$max_on_page = $number_per_page * ($page + 1) - 1;
$pre_links = array();
$post_links = array();
foreach ($deltas as $key => $delta) {
$item = $media[$delta];
$fid = _media_gallery_get_media_fid($item);
if ($key < $min_on_page) {
$pre_links[$key] = array(
'title' => '',
'href' => 'media-gallery/detail/' . $node->nid . '/' . $fid,
'attributes' => array(
'class' => array(
'colorbox-supplemental-link pre',
),
),
);
unset($media[$delta]);
}
elseif ($key > $max_on_page) {
$post_links[$key] = array(
'title' => '',
'href' => 'media-gallery/detail/' . $node->nid . '/' . $fid,
'attributes' => array(
'class' => array(
'colorbox-supplemental-link post',
),
),
);
unset($media[$delta]);
}
}
// Field rendering requires sequential deltas, so re-key.
// @todo Open a Drupal core issue about this.
if ($media) {
$node->media_gallery_media[LANGUAGE_NONE] = array_values($media);
}
else {
$node->media_gallery_media[LANGUAGE_NONE] = array();
}
// Create a set of dummy links to media items that don't appear on this
// page, so colorbox can link to them in the slideshow.
// @todo If the gallery contains 1000 media, then rendering each link takes
// extra server-side time, extra network time to transfer the markup, and
// extra client-side time to initialize the DOM. Performance can likely be
// significantly improved if we only send the fids to Drupal.settings, and
// add JavaScript code to make that information usable by Colorbox.
$node->content['colorbox_links_pre'] = array(
'#theme' => 'links',
'#links' => $pre_links,
'#weight' => -20,
'#attributes' => array(
'class' => array(
'colorbox-supplemental-links',
),
),
);
$node->content['colorbox_links_post'] = array(
'#theme' => 'links',
'#links' => $post_links,
'#weight' => 20,
'#attributes' => array(
'class' => array(
'colorbox-supplemental-links',
),
),
);
// Finally, display the pager, with a high weight so it appears at the
// bottom.
if ($full) {
$node->content['media_gallery_pager'] = array(
'#theme' => 'pager',
'#element' => MEDIA_GALLERY_PAGER_ELEMENT,
'#weight' => 2000,
);
}
}
return $node;
}