While Image Assist lets me add some inline code to display an image and a caption, it would be very nice if the Image module's "Attached Image" would optionally automatically show a caption, perhaps generated from the Image node's body; a teaser caption is probably not required as the image is too small to display.

I've currently hacked a solution in Image 5.x-1.8 in the file image\contrib\image_attach/image_attach.module, at the end in function theme_image_attach_body($node) to which I've added just one line, $output .= '<div class="caption">' . $image->body . '</div>';

 function theme_image_attach_body($node) {
  $img_size = variable_get('image_attach_size_body_'. $node->type, IMAGE_THUMBNAIL);

  if ($img_size != IMAGE_ATTACH_HIDDEN) {
    drupal_add_css(drupal_get_path('module', 'image_attach') .'/image_attach.css');

    $image = node_load($node->iid);
    if (!node_access('view', $image)) {
      // If the image is restricted, don't show it as an attachment.
      return NULL;
    }
    $info = image_get_info(file_create_path($image->images[$img_size]));

    $output = '<div style="width: '. $info['width'] .'px" class="image-attach-body">';
    $output .= l(image_display($image, $img_size), "node/$node->iid", array(), NULL, NULL, FALSE, TRUE);
    $output .= '<div class="caption">' . $image->body . '</div>';
    $output .= '</div>'."\n";

    return $output;
  }
}

The caption is formatted using the css class .caption in my theme's style.css file,

I had tried adding this function to my template.php file as described in "Using Theme Override Functions"

Comments

iantresman’s picture

Title: Automatic caption from image body » Automatic caption from image body teaser

I've modified the code slightly so that the image body text uses the "teaser" text as the caption only. In this way, the image body can be much longer and descriptive, but nothing appearing after the <!--break--> will display in the image preview caption text.

function theme_image_attach_body($node) {
  $img_size = variable_get('image_attach_size_body_'. $node->type, IMAGE_THUMBNAIL);

  if ($img_size != IMAGE_ATTACH_HIDDEN) {
    drupal_add_css(drupal_get_path('module', 'image_attach') .'/image_attach.css');

    $image = node_load($node->iid);
    if (!node_access('view', $image)) {
      // If the image is restricted, don't show it as an attachment.
      return NULL;
    }
    $info = image_get_info(file_create_path($image->images[$img_size]));
    $caption = node_teaser($image->body);

    $output = '<div style="width: '. $info['width'] .'px" class="image-attach-body">';
    $output .= l(image_display($image, $img_size), "node/$node->iid", array(), NULL, NULL, FALSE, TRUE);
    $output .= '<div class="caption">' . $caption . '</div>';
    $output .= '</div>'."\n";

    return $output;
  }
}

Only two new lines have been added in total:

  1. $caption = node_teaser($image->body);, which creates the teaser. And few lines on...
  2. $output .= '<div class="caption">' . $caption . '</div>';, which displays the teaser as the image caption
Hetta’s picture

marked http://drupal.org/node/265961 as duplicate.

sun’s picture

Status: Active » Closed (won't fix)

Sorry, 5.x-1.x won't see any new features.