Index: media_gallery.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/media_gallery/media_gallery.module,v
retrieving revision 1.7
diff -u -p -r1.7 media_gallery.module
--- media_gallery.module	18 Jan 2011 02:32:22 -0000	1.7
+++ media_gallery.module	18 Jan 2011 20:09:17 -0000
@@ -546,49 +546,15 @@ function media_gallery_block_view($delta
     $block['content'] = t('No content available.');
   }
   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]);
-        }
-      }
-    }
-    // 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'] = array(
+      '#node' => $node,
+      '#pre_render' => array('media_gallery_block_build', '_field_extra_fields_pre_render'),
+      '#cache' => array(
+        'cid' => 'media_gallery_block:' . $node->nid,
+        'bin' => 'cache_block',
+        'expire' => 86400,
+      ),
+    );
     $block['content']['more_link'] = array(
       '#theme' => 'more_link',
       '#url' => 'node/' . $node->nid,
@@ -596,11 +562,62 @@ function media_gallery_block_view($delta
       '#weight' => 1000,
     );
   }
-
   return $block;
 }
 
 /**
+ * Build a renderable array for the media gallery block content.
+ */
+function media_gallery_block_build($elements) {
+
+  // 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 $elements['#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.
+  $elements += node_view($node, 'media_gallery_block');
+
+  return $elements;
+}
+
+/**
  * Implements hook_block_configure().
  */
 function media_gallery_block_configure($delta = '') {
