diff --git a/image_field_caption.module b/image_field_caption.module index b1f2b29..6c097fd 100644 --- a/image_field_caption.module +++ b/image_field_caption.module @@ -9,18 +9,87 @@ * Implements hook_theme_registry_alter(). */ function image_field_caption_theme_registry_alter(&$theme_registry) { - // Override the image_formatter function and add caption as a variable to be - // assembled by theme(). - $theme_registry['image_formatter']['theme path'] = drupal_get_path('module', 'image_field_caption'); - $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'; } /** + * Implements hook_field_formatter_info(). + */ +function image_field_caption_field_formatter_info() { + return array( + 'image_field_caption' => array( + 'label' => t('Image with caption'), + 'field types' => array('image'), + 'settings' => array( + 'caption' => '', + 'text_format' => filter_default_format(), + 'image_style' => '', + 'image_link' => '', + ), + ), + ); +} + +/** + * Implements hook_field_formatter_view(). + */ +function image_field_caption_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) { + $element = array(); + switch ($display['type']) { + case 'image_field_caption': + $element = image_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display); + foreach ($items as $delta => $item) { + $element[$delta]['#theme'] = 'image_field_caption_formatter'; + } + break; + } + return $element; +} + +/** + * Implements hook_field_formatter_settings_form(). + */ +function image_field_caption_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) { + // Use the image field formatter settings form. + return image_field_formatter_settings_form($field, $instance, $view_mode, $form, $form_state); +} + +/** + * Implements hook_field_formatter_settings_summary(). + */ +function image_field_caption_field_formatter_settings_summary($field, $instance, $view_mode) { + // Use the image field formatter settings summary. + return image_field_formatter_settings_summary($field, $instance, $view_mode); +} +/** + * Implements hook_theme(). + */ +function image_field_caption_theme() { + return array( + 'image_field_caption' => array( + 'template' => 'image_field_caption', + 'variables' => array('image' => NULL, 'caption' => NULL), + ), + 'image_field_caption_formatter' => array( + 'variables' => array('item' => NULL, 'path' => NULL, 'image_style' => NULL), + 'theme path' => drupal_get_path('module', 'image_field_caption'), + 'function' => 'theme_image_field_caption_formatter', + 'type' => 'module', + ), + ); +} + +/** Returns HTML for an image field caption formatter. + * + * @param $variables + * An associative array containing: + * - image: HTML from theme_image_formatter + * - caption: + * * Override of theme_image_formatter(). */ -function image_field_caption_image_formatter($variables) { +function theme_image_field_caption_formatter($variables) { $image = theme_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 @@ -75,18 +144,6 @@ function image_field_caption_picture_sizes_formatter($variables) { } /** - * Implements hook_theme(). - */ -function image_field_caption_theme($existing, $type, $theme, $path) { - return array( - 'image_field_caption' => array( - 'template' => 'image_field_caption', - 'variables' => array('image' => NULL, 'caption' => NULL), - ), - ); -} - -/** * Given an entity type and bundle name, this will return an associative array * of image field info instances, keyed by image field machine names. Returns * null if no image fields are found.