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.
| Comment | File | Size | Author |
|---|---|---|---|
| #8 | thickbox.module.imagefield_0_0.patch | 2.06 KB | yojoe |
| #1 | imagefield.module.thickbox_1.patch | 1.86 KB | yojoe |
| imagefield.module.thickbox_0.patch | 1.85 KB | yojoe |
Comments
Comment #1
yojoe commentedJust added a check_url() in the theme function.
Comment #2
FiReaNGeL commentedVery 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.
Comment #3
yojoe commentedI 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.
Comment #4
yched commentedYou 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.
Comment #5
yched commentedEr, 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.
Comment #6
yojoe commentedHm, 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?
Comment #7
quicksketchyojoe, 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.
Comment #8
yojoe commentedOK, 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! ;)
Comment #9
yojoe commentedChanged the status to fixed.
Comment #10
(not verified) commented