function media_gallery_block_view in Media Gallery 7.2
Same name and namespace in other branches
- 7 media_gallery.module \media_gallery_block_view()
Implements hook_block_view().
File
- ./
media_gallery.module, line 626
Code
function media_gallery_block_view($delta = '') {
$node = node_load($delta);
if (empty($node->media_gallery_expose_block[LANGUAGE_NONE][0]['value'])) {
// Bail out now if the node doesn't exist or if it is configured not to
// display a block.
$block['subject'] = NULL;
$block['content'] = '';
return $block;
}
// Collect an array of file IDs associated with this gallery. For
// simplicity we will assume (here and below) that this is not a
// multilingual field. Also note that the node may have been loaded and
// viewed elsewhere on the page, in which case the 'media_gallery_file'
// field was modified and does not contain what we want, so we have to go
// back to the original field value set in hook_node_load() instead, and
// also clone the node before changing it so our modifications do not
// affect other places where it might be being viewed.
$node = clone $node;
$node->media_gallery_file = $node->media_gallery_file_original;
$files =& $node->media_gallery_file[LANGUAGE_NONE];
$files = media_gallery_filter_media_access($files, 'view');
if (empty($files)) {
// Bail out now if there won't be any media items to show.
$block['subject'] = check_plain($node->title);
$block['content'] = t('No content available.');
return $block;
}
$gallery_fids = array();
foreach ($files as $file) {
$gallery_fids[] = _media_gallery_get_media_fid($file);
}
// Construct a list of file IDs that is limited to the specified number of
// items and ordered by most recent; these are the files that will be
// displayed in the block.
$columns = !empty($node->media_gallery_block_columns[LANGUAGE_NONE][0]['value']) ? $node->media_gallery_block_columns[LANGUAGE_NONE][0]['value'] : 1;
$rows = !empty($node->media_gallery_block_rows[LANGUAGE_NONE][0]['value']) ? $node->media_gallery_block_rows[LANGUAGE_NONE][0]['value'] : 1;
$number_to_show = $columns * $rows;
$block_fids = db_select('file_managed', 'f')
->fields('f', array(
'fid',
))
->condition('fid', $gallery_fids, 'IN')
->orderBy('timestamp', 'DESC')
->range(0, $number_to_show)
->execute()
->fetchCol();
// Before sorting, remove any items that will not display in the block.
$fid_order = array_flip($block_fids);
if ($number_to_show < count($files)) {
foreach ($files as $key => $file) {
if (!isset($fid_order[_media_gallery_get_media_fid($file)])) {
unset($files[$key]);
}
}
}
// Prepare the sorting function with the list of file ID orders.
_media_gallery_sort_by_recent(NULL, NULL, $fid_order);
// Perform the sort.
usort($files, '_media_gallery_sort_by_recent');
// Display the block.
$block['subject'] = check_plain($node->title);
$block['content']['gallery'] = node_view($node, 'media_gallery_block');
// Move the node's contextual links so that they display on the block
// rather than the node (i.e., in the same dropdown as the "Configure
// block" link). This is also required in order to properly integrate with
// the code in media_gallery_contextual_links_view_alter().
if (isset($block['content']['gallery']['#contextual_links'])) {
$block['content']['#contextual_links'] = $block['content']['gallery']['#contextual_links'];
unset($block['content']['gallery']['#contextual_links']);
}
$block['content']['more_link'] = array(
'#theme' => 'more_link',
'#url' => 'node/' . $node->nid,
'#title' => t('Show the complete gallery'),
'#weight' => 1000,
);
return $block;
}