Attached images in a block

Last modified: October 15, 2009 - 15:52

A patch is here http://drupal.org/node/113326
---

This snippet displays attached images (which requires the image module and image_attach) in a block. I'm no PHP expert, but I managed to combine the general block loading code (lines 1 & 2) with the theme_image_attach_body code from image_attach.module, which seems to work pretty well.

<?php
if (arg(0) == 'node' && is_numeric(arg(1))) {
   
$node = node_load(arg(1));
    if (
$node->iid) {
       
$img_size = variable_get('image_attach_size_body_'. $node->type, IMAGE_ORIGINAL);
        if (
$img_size != IMAGE_ATTACH_HIDDEN) {
           
$image = node_load($node->iid);
            if (!
node_access('view', $image)) {
               
// If the image is restricted, don't show it as an attachment.
               
return NU

LL
;
            }
           
$output = '<div class="image-attach">';
           
$output .= l(image_display($image, $img_size), "node/$node->iid", array(), NULL, NULL, FALSE, TRUE);
           
$output .= '</div>'."\n";

            print
$output;
        }
    }
}
?>

Change the IMAGE_ORIGINAL to 'thumbnail' or 'yoursize' to have it display in a custom size.

 
 

Drupal is a registered trademark of Dries Buytaert.