Index: galleria.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/galleria/galleria.module,v
retrieving revision 1.1.2.18
diff -u -p -r1.1.2.18 galleria.module
--- galleria.module	28 Apr 2009 09:08:40 -0000	1.1.2.18
+++ galleria.module	29 Apr 2009 01:32:38 -0000
@@ -106,6 +106,9 @@ function galleria_theme() {
     'galleria_lightbox_link' => array(
       'arguments' => array('nid' => NULL),
     ),
+    'galleria_formatter_imagefield_galleria' => array(
+      'arguments' => array('element' => NULL),
+    ),
   );
 }
 
@@ -232,22 +235,14 @@ function theme_galleria_lightbox_link($n
 }
 
 /**
- * Preprocess the galleria variables.
+ * Implementation of template_preprocess_hook().
  */
 function template_preprocess_galleria(&$vars) {
-  $allowed_extensions = explode(',', GALLERIA_ALLOWED_EXT);;
-  $node = $vars['node'];
-  $files = $node->files;
-  $imagecount = 0;
+  $allowed_extensions = explode(',', GALLERIA_ALLOWED_EXT);
   $images = array();
-  $i = 0;
-
-  $imagecache_exists = module_exists('imagecache');
-  $img_preset = variable_get('galleria_imagecache_preset', '');
-  $thumb_preset = variable_get('galleria_thumb_imagecache_preset', '');
 
-  foreach ($files as $file) {
-    // abort if this is not an image file
+  foreach ($vars['node']->files as $file) {
+    // filter out files that are not images
     $ext = strtolower(substr($file->filename, -4));
     if (substr($ext, 0, 1) == '.') {
       $ext = substr($ext, 1, 3);
@@ -255,40 +250,63 @@ function template_preprocess_galleria(&$
     if (!in_array($ext, $allowed_extensions)) {
       continue;
     }
+    $images[] = $file;
+  }
+
+  $output = _galleria_build($images);
+
+  $vars['next_prev_links'] = $output['next_prev_links'];
+  $vars['gallery'] = $output['gallery'];
+}
+
+/**
+ * Build a Galleria from an array of images
+ *
+ * @param $images
+ *   An array of file objects representing the images to be included in the Galleria
+ * @return $vars
+ *   An array of template variables and their values
+ */
+function _galleria_build($images) {
+  $image_list = array();
+  $i = 0;
+
+  $imagecache_exists = module_exists('imagecache');
+  $img_preset = variable_get('galleria_imagecache_preset', '');
+  $thumb_preset = variable_get('galleria_thumb_imagecache_preset', '');
 
-    // this is an image, so process it
-    ++$imagecount;
-    $caption = $file->description != $file->filename ? $file->description : '';
-    $url = $file->filepath;
+  foreach ($images as $image) {
+    $caption = ($image->description != $image->filename) ? $image->description : '';
+    $filepath = $image->filepath;
 
     if ($imagecache_exists && $thumb_preset) {
-      $thumb = theme('imagecache', $thumb_preset, $file->filepath, $caption, $caption);
+      $thumb = theme('imagecache', $thumb_preset, $filepath, $caption, $caption);
     }
 
     if ($imagecache_exists && $img_preset) {
       if ($thumb) {
         // Preset for both thumbnail and gallery image
-        $image = l($thumb, imagecache_create_url($img_preset, $file->filepath), array('html' => TRUE, 'attributes' => array('title' => $caption)));
+        $image = l($thumb, imagecache_create_url($img_preset, $filepath), array('html' => TRUE, 'attributes' => array('title' => $caption)));
       }
       else {
         // Preset for only the gallery image
-        $image = theme('imagecache', $img_preset, $file->filepath, $caption, $caption);
+        $image = theme('imagecache', $img_preset, $filepath, $caption, $caption);
       }
     }
     else {
       if ($thumb) {
         // Preset for only the thumbnail image
-        $image = l($thumb, $file->filepath, array('html' => TRUE, 'attributes' => array('title' => $caption)));
+        $image = l($thumb, $filepath, array('html' => TRUE, 'attributes' => array('title' => $caption)));
       }
       else {
         // No presets selected
-        $image = theme('image', $file->filepath, $caption, $caption);
+        $image = theme('image', $filepath, $caption, $caption);
       }
     }
 
-    $images[] = array(
+    $image_list[] = array(
       'data' => $image,
-      'class' => $i == 0 ? 'active' : '',
+      'class' => ($i == 0) ? 'active' : '',
     );
 
     $i++;
@@ -299,11 +317,55 @@ function template_preprocess_galleria(&$
   );
 
   if (variable_get('galleria_thumb_location', 'below') == 'above') {
-    $vars['gallery'] = theme('item_list', $images, NULL, 'ul', $attribs) . '<div id="main-image"></div>';
+    $vars['gallery'] = theme('item_list', $image_list, NULL, 'ul', $attribs) . '<div id="main-image"></div>';
   }
   else {
-    $vars['gallery'] = '<div id="main-image"></div>' . theme('item_list', $images, NULL, 'ul', $attribs);
+    $vars['gallery'] = '<div id="main-image"></div>' . theme('item_list', $image_list, NULL, 'ul', $attribs);
   }
 
-  $vars['next_prev_links'] = ($imagecount > 1) ? '<a onclick="$.galleria.prev(); return false;" href="#"><< ' . t('previous') . '</a> | <a onclick="$.galleria.next(); return false;" href="#">' . t('next') . '>></a>' : '';
+  $vars['next_prev_links'] = ($i > 1) ? '<a onclick="$.galleria.prev(); return false;" href="#"><< ' . t('previous') . '</a> | <a onclick="$.galleria.next(); return false;" href="#">' . t('next') . '>></a>' : '';
+  
+  return $vars;
 }
+
+/**
+ * Implementation of hook_field_formatter_info(),.
+ */
+function galleria_field_formatter_info() {
+  return array(
+    'imagefield_galleria' => array(
+      'label' => t('Styled by Galleria'),
+      'field types' => array('filefield'),
+      'multiple values' => CONTENT_HANDLE_MODULE,
+    ),
+  );
+}
+
+/**
+ * Theme CCK imagefield uploads in a Galleria
+ *
+ * @ingroup themeable
+ */
+function theme_galleria_formatter_imagefield_galleria($element) {
+  galleria_includes();
+
+  $item = $element;
+
+  foreach (element_properties($element) as $key) {
+      //kill properties
+      unset($item[$key]);
+  }
+
+  $images = array();
+  foreach ($item as $file) {
+    //build images array
+    $images[] = (object) array(
+      'filepath' => $file['#item']['filepath'],
+      'filename' => $file['#item']['filename'],
+      'description' => $file['#item']['data']['description'],
+    );
+  }
+
+  $output = _galleria_build($images);
+  return $output['gallery'] . $output['next_prev_links'];
+}
\ No newline at end of file
