This is a small but very useful patch to used imagecache.module and thickbox.module together in Drupal 5. If imagecache.module AND thickbox.module (http://drupal.org/project/thickbox) are available, then for every imagecache preset an additional imagache+thickbox formatter is available. Let's say you have defined an imagecache preset named 'thumbnail', then you'll automatically get an additonal formatter named 'Thickbox: thumbnail'. If you choose this formatter e.g. for the body, then the imagecache 'thumbnail'-preset is used to show a clickable preview image of the full-size image. If you click the preview, Thickbox is used to nicely overlay the fullsize image over the current page. If the imagefield has multiple images, they are treated like a gallery and thickbox displays 'Next' and 'Prev' buttons.

I hope you like it and integrate it into the official release of imagefield.module. The patch I post here is done relative to the 5.x-1.x-dev version. Please tell me if I should provide a patch for the HEAD version. Anyway, the changes to imagefield.module are not very much. I had to add some code to imagefield_field_formatter_info(), imagefield_field_formatter() and added a new theme-function:

new code in imagefield_field_formatter_info():

  if ( module_exists('imagecache') && module_exists('thickbox') ) {
    $rules = _imagecache_get_presets();
    foreach ($rules as $ruleid => $rulename) {
      $formatters['thickbox]['.$rulename] = array(
        'label' => 'Thickbox: '.$rulename,
        'field types' => array('image'),
        );
    }
  }

new code in imagefield_field_formatter():

  if ( module_exists('imagecache') && module_exists('thickbox') ) {
    if (strpos($formatter, 'thickbox][') !== false) {
      list($null, $namespace) = explode('][', $formatter, 2);
      $rules = _imagecache_get_presets();
      if (in_array($namespace, (array) $rules)) {
        return theme('imagefield_image_imagecache_thickbox', $namespace, $field, $file['filepath'], $item['alt'], $item['title']);
      }
    }
  }

new theme function :

function theme_imagefield_image_imagecache_thickbox($namespace, $field, $path, $alt = '', $title = '', $attributes = NULL) {
  $attributes = drupal_attributes($attributes);
  $imagecache_path =  file_create_url(file_directory_path() .'/imagecache/'. $namespace .'/'. $path);
  return '<a href="'. check_url(file_create_url($path)) .'" class="thickbox" rel="'. $field['type_name'] .'"><img src="'. $imagecache_path .'" alt="'. check_plain($alt) .'" title="'. check_plain($title) .'" '. $attributes .' /></a>';
}

That's it! ;)

Note: There is no drupal 5 release of thickbox.module, yet. You have to install the HEAD-version of thickbox.module to test my patch.

Comments

yojoe’s picture

StatusFileSize
new1.86 KB

Just added a check_url() in the theme function.

FiReaNGeL’s picture

Very interesting! But does it belong in imagecache, thickbox, or its own module? I don't know. I just know that I'll find it very useful! Thanks for sharing.

yojoe’s picture

I think it belongs to imagefield, because it just adds special formatters for rendering the imagefield. I don't know if there are special cck-hooks, that allow modules to add formatters to a certain cck field??? If so this could be outsourced into an extra module, but since it doesn't require imagecache.module or thickbox.module I think it's fine, that this code is placed in imagecache.module.

yched’s picture

You can add custom formatters for a cck field from the outside.
Thickbox module would just have to implement hook_field_formatter_info and hook_field_formatter.
You can check cck's text.module or number.module (more advanced) for examples.

I don't think this feature has been actually used yet, so it would be a great opportunity to test. It should work, if it does not then we have a bug in cck.

yched’s picture

Er, sorry, you probably don't need examples given the code you already provide :-)
Well just to point out that this could (and probably should IMO) stay in thickbox.

yojoe’s picture

Hm, but then the imagecache formatters wouldn't belong to imagefield.module, too!? They should be provided by imagecache.module, right?

I just modified imagefield.module, because the imagecache formatters were already there. So do you recommend, that I should create this patch against the thickbox.module?

quicksketch’s picture

yojoe, you're probably right about imagecache providing the formatters for imagefield. I hadn't ever thought of that before. Imagecache really should be providing the additional formatters.

yojoe’s picture

StatusFileSize
new2.06 KB

OK, I created a patch against thickbox.module and posted it at the thickbox project(http://drupal.org/project/thickbox). See http://drupal.org/node/112850 for further progress on that. I hope they integrate it soon into CVS. I also appended the patch to this post. Well, you could add a note about this functionality on the imagefield project main site, so other users get informed that they can install thickbox.module to get additional imagefield formatters.

Hope you like it! ;)

yojoe’s picture

Status: Needs review » Fixed

Changed the status to fixed.

Anonymous’s picture

Status: Fixed » Closed (fixed)