Created
May 12, 2011 15:20
-
-
Save leylaso/968724 to your computer and use it in GitHub Desktop.
Drupal 6 theme functions to insert a jcarousel that uses lightbox into any node with the appropriate image field, for use in a template.php file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Node preprocess function | |
*/ | |
function mytheme_preprocess_node(&$vars, $hook) { | |
if ($vars['node']->field_image && !empty($vars['node']->field_image)) { | |
$vars['image_carousel'] = _mytheme_generate_carousel($vars['node']->field_image); | |
} | |
} | |
/** | |
* Helper function to get the appropriate imagecache variant of an image | |
* | |
* This makes sure that the imagecached version of the image exists, and if it does | |
* not creates it. | |
* | |
* @param $filepath | |
* The path to the original file | |
* @param $presetname | |
* The imagecache preset to use | |
* @return | |
* The path to the cached version of the image | |
*/ | |
function _mytheme_generate_imagecache(&$filepath, $presetname = 'teaser') { | |
// First, check that the preset actually exists | |
$preset = imagecache_preset_by_name($presetname); | |
if (!$preset) { | |
return FALSE; | |
} | |
// Try to get the existing cached image | |
$dst = imagecache_create_path($presetname, $filepath); | |
$cache_path = $dst; | |
if (!file_exists($dst) || is_dir($dst)) { | |
imagecache_build_derivative($preset['actions'], $filepath, $cache_path); | |
} | |
return $cache_path; | |
} | |
/** | |
* Helper function to generate a jcarousel from a cck image field | |
* | |
* @param $pics | |
* The cck image field | |
* @return | |
* The images contained in the array, in a carousel or item list | |
*/ | |
function _mytheme_generate_carousel ($pics) { | |
$carousel = array(); | |
$lnk_options = array( | |
'html' => TRUE, | |
'attributes' => array( | |
'class' => 'carousel-item', | |
), | |
); | |
// Use Lightbox if available | |
if (function_exists('lightbox2_menu')) { | |
$lnk_options['attributes']['rel'] = 'lightbox'; | |
} | |
foreach ($pics as $item) { | |
$thumb = _mytheme_generate_imagecache($item['filepath'], 'carousel'); | |
$image = _mytheme_generate_imagecache($item['filepath'], 'large'); | |
$carousel[] = l("<img src='/{$thumb}' alt='' title='{$item['filename']}'/>", $image, $lnk_options); | |
} | |
// Theme the feed as a carousel if the module jcarousel is available | |
if (function_exists('theme_jcarousel')) { | |
$options = array ( | |
'buttonNextEvent' => 'click', | |
'buttonPrevEvent' => 'click', | |
'visible' => 4, | |
'scroll' => 3, | |
); | |
return theme('jcarousel', $carousel, $options); | |
} | |
else { | |
return theme('item_list', $carousel); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for that snippet! Got me 90% there :)