Index: media_gallery.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/media_gallery/media_gallery.module,v
retrieving revision 1.5
diff -u -p -r1.5 media_gallery.module
--- media_gallery.module	16 Nov 2010 22:19:14 -0000	1.5
+++ media_gallery.module	1 Dec 2010 11:35:01 -0000
@@ -537,65 +537,98 @@ function media_gallery_block_info() {
  * Implements hook_block_view().
  */
 function media_gallery_block_view($delta = '') {
-  $node = node_load($delta);
-  $block['subject'] = check_plain($node->title);
-  if (empty($node->media_gallery_media_original)) {
-    // Bail out now if there won't be any media items to show.
-    $block['content'] = t('No content available.');
+  if ($delta && $node = node_load($delta)) {
+    $node = node_load($delta);
+    $block['subject'] = check_plain($node->title);
+    if (empty($node->media_gallery_media_original)) {
+      // Bail out now if there won't be any media items to show.
+      $block['content'] = t('No content available.');
+    }
+    else {
+      $block += array(
+        '#pre_render' => '_media_gallery_block_render',
+        '#node' => $node,
+        '#cache' => array(
+          'cid' => 'media_gallery_block:' . $node->nid,
+          'expire' => 3600,
+          'bin' => 'cache_block',
+        ),
+      );
+    }
   }
-  else {
-    // 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_media'
-    // 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_media = $node->media_gallery_media_original;
-    $files = &$node->media_gallery_media[LANGUAGE_NONE];
-    $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]);
-        }
+}
+
+/**
+ * Pre-render callback for media_gallery_block_view().
+ */
+function _media_gallery_block_render(&$elements) {
+  $node = clone $elements['#node'];
+
+  // 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_media'
+  // 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_media = $node->media_gallery_media_original;
+  $files = &$node->media_gallery_media[LANGUAGE_NONE];
+  $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['content'] = node_view($node, 'media_gallery_block');
-    $block['content']['more_link'] = array(
-      '#theme' => 'more_link',
-      '#url' => 'node/' . $node->nid,
-      '#title' => t('Show the complete gallery'),
-      '#weight' => 1000,
-    );
   }
+  // 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.
+  $elements['content'] = node_view($node, 'media_gallery_block');
+  $elements['content']['more_link'] = array(
+    '#theme' => 'more_link',
+    '#url' => 'node/' . $node->nid,
+    '#title' => t('Show the complete gallery'),
+    '#weight' => 1000,
+  );
+
+  return $elements;
+}
 
-  return $block;
+/**
+ * Implements hook_node_update().
+ */
+function media_gallery_node_update($node) {
+  cache_clear_all('media_gallery_block:' . $node->nid, 'cache_block');
+}
+
+/**
+ *  Implements hook_node_delete().
+ */
+function media_gallery_node_delete($node) {
+  cache_clear_all('media_gallery_block:' . $node->nid, 'cache_block');
 }
 
 /**
