I've been looking for a way to display the body field or extra fields in the caption in addition to the title. I thought Lightbox trigger in views would be the answer but there is no auto resize feature which makes it impossible to use when displaying images of various shapes and sizes.

I'm using imagefield and imagecache so this may not apply to everyone, and in the lightbox2 settings under CCK display settings I have use node title as caption ticked.

From lightbox2.formatter.inc I copied the following into my template.php

function theme_imagefield_image_imagecache_lightbox2($view_preset, $field_name, $item, $node, $rel = 'lightbox', $args = array()) {
  if (!isset($args['lightbox_preset'])) {
    $args['lightbox_preset'] = 'original';
  }
  // Can't show current node page in a lightframe on the node page.
  // Switch instead to show it in a lightbox.
  $on_image_node = (arg(0) == 'node') && (arg(1) == $node->nid);
  if ($rel == 'lightframe' && $on_image_node) {
    $rel = 'lightbox';
  }
  $orig_rel = $rel;

  // Unserialize into original - if sourced by views.
  $item_data = $item['data'];
  if (is_string($item['data'])) {
    $item_data = unserialize($item['data']);
  }

  // Set up the title.
  $image_title = $item_data['description'];
  $image_title = (!empty($image_title) ? $image_title : $item_data['title']);
  $image_title = (!empty($image_title) ? $image_title : $item_data['alt']);
  if (empty($image_title) || variable_get('lightbox2_imagefield_use_node_title', FALSE)) {
    $node = node_load($node->nid);
    $image_title = $node->title;
  }

  $image_tag_title = '';
  if (!empty($item_data['title'])) {
    $image_tag_title = $item_data['title'];
  }


  // Enforce image alt.
  $image_tag_alt = '';
  if (!empty($item_data['alt'])) {
    $image_tag_alt = $item_data['alt'];
  }
  elseif (!empty($image_title)) {
    $image_tag_alt = $image_title;
  }

  // Set up the caption.
  $node_links = array();
  if (!empty($item['nid'])) {
    $attributes = array();
    $attributes['id'] = 'lightbox2-node-link-text';
    $target = variable_get('lightbox2_node_link_target', FALSE);
    if (!empty($target)) {
      $attributes['target'] = $target;
    }
    $node_link_text = variable_get('lightbox2_node_link_text', 'View Image Details');
    if (!$on_image_node && !empty($node_link_text)) {
      $node_links[] = l($node_link_text, 'node/'. $item['nid'], array('attributes' => $attributes));
    }
    $download_link_text = check_plain(variable_get('lightbox2_download_link_text', 'Download Original'));
    if (!empty($download_link_text) && user_access('download original image')) {
      $node_links[] = l($download_link_text, file_create_url($item['filepath']), array('attributes' => array('target' => '_blank', 'id' => 'lightbox2-download-link-text')));
    }
  }

  $caption = $image_title;
  if (count($node_links)) {
    $caption .= '<br /><br />'. implode(" - ", $node_links);
  }
  if (isset($args['caption'])) {
    $caption = $args['caption']; // Override caption.
  }

  if ($orig_rel == 'lightframe') {
    $frame_width = variable_get('lightbox2_default_frame_width', 600);
    $frame_height = variable_get('lightbox2_default_frame_height', 400);
    $frame_size = 'width:'. $frame_width .'px; height:'. $frame_height .'px;';
    $rel = preg_replace('/\]$/', "|$frame_size]", $rel);
  }

  // Set up the rel attribute.
  $full_rel = '';
  $imagefield_grouping = variable_get('lightbox2_imagefield_group_node_id', 1);
  if (isset($args['rel_grouping'])) {
    $full_rel = $rel .'['. $args['rel_grouping'] .']['. $caption .']';
  }
  elseif ($imagefield_grouping == 1) {
    $full_rel = $rel .'['. $field_name .']['. $caption .']';
  }
  elseif ($imagefield_grouping == 2 && !empty($item['nid'])) {
    $full_rel = $rel .'['. $item['nid'] .']['. $caption .']';
  }
  elseif ($imagefield_grouping == 3 && !empty($item['nid'])) {
    $full_rel = $rel .'['. $field_name .'_'. $item['nid'] .']['. $caption .']';
  }
  elseif ($imagefield_grouping == 4 ) {
    $full_rel = $rel .'[allnodes]['. $caption .']';
  }
  else {
    $full_rel = $rel .'[]['. $caption .']';
  }
  $class = '';
  if ($view_preset != 'original') {
    $class = 'imagecache imagecache-' . $field_name . ' imagecache-' . $view_preset . ' imagecache-' . $field_name . '-' . $view_preset;
  }
  $link_attributes = array(
    'rel' => $full_rel,
    'class' => 'imagefield imagefield-lightbox2 imagefield-lightbox2-' . $view_preset . ' imagefield-' . $field_name . ' ' . $class,
  );

  $attributes = array();
  if (isset($args['compact']) && $item['#delta']) {
    $image = '';
  }
  elseif ($view_preset == 'original') {
    $image = theme('lightbox2_image', $item['filepath'], $image_tag_alt, $image_tag_title, $attributes);
  }
  elseif ($view_preset == 'link') {
    // Not actually an image, just a text link.
    $image = variable_get('lightbox2_view_image_text', 'View image');
  }
  else {
    $image = theme('imagecache', $view_preset, $item['filepath'], $image_tag_alt, $image_tag_title, $attributes);
  }

  if ($args['lightbox_preset'] == 'node') {
    $output = l($image, 'node/'. $node->nid .'/lightbox2', array('attributes' => $link_attributes, 'html' => TRUE));
  }
  elseif ($args['lightbox_preset'] == 'original') {
    $output = l($image, file_create_url($item['filepath']), array('attributes' => $link_attributes, 'html' => TRUE));
  }
  else {
    $output = l($image, imagecache_create_url($args['lightbox_preset'], $item['filepath']), array('attributes' => $link_attributes, 'html' => TRUE));
  }

  return $output;
}

then changed

  // Set up the title.
  $image_title = $item_data['description'];
  $image_title = (!empty($image_title) ? $image_title : $item_data['title']);
  $image_title = (!empty($image_title) ? $image_title : $item_data['alt']);
  if (empty($image_title) || variable_get('lightbox2_imagefield_use_node_title', FALSE)) {
    $node = node_load($node->nid);
    $image_title = $node->title;
  }

to

  // Set up the title.
  $image_title = $item_data['description'];
  $image_title = (!empty($image_title) ? $image_title : $item_data['title']);
  $image_title = (!empty($image_title) ? $image_title : $item_data['alt']);
  if (empty($image_title) || variable_get('lightbox2_imagefield_use_node_title', FALSE)) {
    $node = node_load($node->nid);
   $image_title = $node->title . '<br />' . $node->body . '<br />' . $node->field_test[0]['value'];
  }

That's it.

Comments

elektrorl’s picture

Thanks!
I'm a noob in php.

My concerns:
$image_title = $node->title . '<br />' . $node->body . '<br />' . $node->field_test[0]['value'];

My CCK fields have multiple values (multigroup version, but it's a normal CCK use for this field).

In contemplate, my field: $node->field_pic_acteur_role[0]['value']
When I replace above, it prints only the first value.

In page.tpl, I can do this:

<?php foreach ($node->field_test as $test) {
  print content_format('field_test', $test);}?>

But how to print this in the function theme_imagefield_image_imagecache_lightbox2 ?

greenavus’s picture

@ marcus178 I tried your suggestion exactly and i always get this error.

Fatal error: Cannot redeclare theme_imagefield_image_imagecache_lightbox2() (previously declared in /home/develop2/clients/beares/sites/all/themes/main/template.php:172) in /home/develop2/clients/beares/sites/all/modules/lightbox2/lightbox2.formatter.inc

If i uncomment this line, it comes up with another error so it seem like there is a conflict of some sorts. But i dont know because i am a php nube.

greenavus’s picture

@marcus178

It worked. It was actually my fault. Just needed to change the theme_ to my theme name. Thanks a lot for the effort.

johnhanley’s picture

Thanks for this.

This had me stumped at first because my assumption was that adding the node body to the output was a simple configurable option within the module itself. Or at least it should be, based on the many requests and references in the issues queue.

In my case I simply wanted to replace the node title with node body and the above theme function (once added to template.php with the theme name replacing the "theme_" prefix) works nicely with only the following change:

$image_title = $node->body;

TechNikh’s picture

Thanks. this is awesome!

ambientdrup’s picture

These methods are great and are working for me as well. I have one question related. Here's my code first:


 // Set up the title.
  $image_title = $item_data['description'];
  $image_title = (!empty($image_title) ? $image_title : $item_data['title']);
  $image_title = (!empty($image_title) ? $image_title : $item_data['alt']);
  if (empty($image_title) || variable_get('lightbox2_imagefield_use_node_title', FALSE)) {
    $node = node_load($node->nid);
    $image_title = $node->title . '<br />' . $node->body . '<br />' . $node->field_new_gallery_image_caption[0]['value']. '<br />' . $node->field_publication_source_gallery[0]['nid'] . '<br />' . $node->field_video_caption_new_gallery[0]['value'] . '<br />Size:' . $node->field_new_gallery_image[0]['filesize']. 'kb';
  }

Do you know how to format the ['filesize'] value printed out above from the $node->field_new_gallery_image field array? I can get it to print out the actual # but it's not formatted with a decimal or any specific kb or mb notation. It's just the raw string.

Any suggestions? I added the Size: and the . 'kb'; but obviously that's just to test to make sure I can print stuff out there.

I want the end result to be like:

179.82 KB

for example.

-Trevor

Anonymous’s picture

Thanks - worked for me.