Image title as text description for colorbox image formatter (Drupal 7)

Table of Contents

When you have core "image" field on your node type and you want to output "title" (or "alt") of this image just under the image thumbnail as text - you can use imagecaptionformatter module for that.
But only in the case when you don't need another image formatter in place - e.g. Colorbox - because Colorbox provides its own image formatter, and imagecaptionformatter module also provides its own image formatter. You obviously can't use two formatters at the same time.

So, when I was asked to set up such image caption for Colorbox multiple image field, I had to spend a bit of time to find better way.

For Colorbox case, it's just the matter of one theme call override!

Colorbox module has themecolorboximagefield() function, and here is the modified version of the function (1 line change) that you need to put to your theme:

'hide') {     $image = '';     $class[] = 'js-hide';   }   else if (!empty($variables['image']['style_name'])) {     $image = theme('image_style', $variables['image']);       }   else {     $image = theme('image', $variables['image']);   }    // ##### Here are the lines I've added   $my_style = 'my_style_that_should_come_with_a_caption';   if ($variables['image']['style_name']  $my_style) {     $image .= '' . check_plain($variables['title']) . '';   }   // ##### end of modification    $options = array(     'html' => TRUE,     'attributes' => array(       'title' => $variables['title'],       'class' => implode(' ', $class),       'rel' => $variables['gid'],     )   );    return l($image, $variables['path'], $options); }  ?>

Super-easy! But I hope this post may save several minutes for somebody :)

Note that if you want to output "alt" of the image, not "title", you should change $variables['image']['title'] to $variables['image']['alt'].

UPD: some code improvements, from fietserwin