diff --git a/image_field_caption.module b/image_field_caption.module index b1f2b29..c41d79e 100644 --- a/image_field_caption.module +++ b/image_field_caption.module @@ -15,6 +15,11 @@ function image_field_caption_theme_registry_alter(&$theme_registry) { $theme_registry['image_formatter']['function'] = 'image_field_caption_image_formatter'; $theme_registry['picture_formatter']['function'] = 'image_field_caption_picture_formatter'; $theme_registry['picture_sizes_formatter']['function'] = 'image_field_caption_picture_sizes_formatter'; + // Override the colorbox_image_formatter function to support caption. + if (module_exists('colorbox')) { + $theme_registry['colorbox_image_formatter']['theme path'] = drupal_get_path('module', 'image_field_caption'); + $theme_registry['colorbox_image_formatter']['function'] = 'image_field_caption_colorbox_formatter'; + } } /** @@ -37,6 +42,24 @@ function image_field_caption_image_formatter($variables) { } /** ++ * Override of colorbox_image_formatter(). ++ */ +function image_field_caption_colorbox_formatter($variables) { + $image = theme_colorbox_image_formatter($variables); + // Now that Drupal has rendered the image, if there was a caption let's + // render the image and the caption, otherwise just return the already + // rendered image. + if (isset($variables['item']['image_field_caption']) && !empty($variables['item']['image_field_caption']['value'])) { + return theme('image_field_caption', array( + 'image' => $image, + 'caption' => check_markup($variables['item']['image_field_caption']['value'], $variables['item']['image_field_caption']['format']), + )); + } else { + return $image; + } +} + +/** * Override of theme_picture_formatter(). */ function image_field_caption_picture_formatter($variables) { @@ -450,3 +473,29 @@ function image_field_caption_views_api() { 'path' => drupal_get_path('module', 'image_field_caption') . '/views', ); } + +/** + * Implements hook_token_info(). + */ +function image_field_caption_token_info() { + $info['tokens']['file']['image_field_caption'] = array( + 'name' => t('Image Field Caption'), + 'description' => t('The image caption, if enabled.'), + ); + return $info; +} + +/** + * Implements hook_tokens(). + */ +function image_field_caption_tokens($type, $tokens, $data = array(), $options = array()) { + if ($type != 'file' || empty($tokens['image_field_caption']) || $tokens['image_field_caption'] != '[file:image_field_caption]' || empty($data['file']) || empty($data['file']->image_field_caption['value'])) { + return; + } + $options = array_merge(array('language' => LANGUAGE_NONE, 'sanitize' => TRUE), $options); + $caption = check_markup($data['file']->image_field_caption['value'], $data['file']->image_field_caption['format'], $options['language']); + if ($options['sanitize']) { + $caption = filter_xss($caption); + } + return array('[file:image_field_caption]' => $caption); +}