Remove Link from Image Attach in Drupal

Mar 1 2010

UPDATE: Note, the code below may be out of date or not work with the newest version of image attach.

If you are using the Image Attach module in Drupal, you may not always want to have the image linked. Drupal links the image automatically to its own image node but sometimes you don't want that.

You can add an overide to your theme's template.php file. Note where it says 'your_theme' on line one, replace that with the name if your actual theme. You should also clear your Drupal cache after adding this code.

  1. function your_theme_image_attach_body($node, $iid) {
  2.   $img_size = variable_get('image_attach_size_body_'. $node->type, IMAGE_THUMBNAIL);
  3.  
  4.   if ($img_size != IMAGE_ATTACH_HIDDEN) {
  5.     drupal_add_css(drupal_get_path('module', 'image_attach') .'/image_attach.css');
  6.  
  7.     $image = node_load($iid);
  8.     if (!node_access('view', $image)) {
  9.       // If the image is restricted, don't show it as an attachment.
  10.       return NULL;
  11.     }
  12.     $class = 'image-attach-body' . ($image->status ? '' : ' image-unpublished');
  13.     $info = image_get_info(file_create_path($image->images[$img_size]));
  14.  
  15.     $output = '
  16. </p><div class="' . $class . '">';
  17.     $output .= image_display($image, $img_size);
  18.     $output .= '</div><p>'."\n";
  19.  
  20.     return $output;
  21.   }
  22. }


Great contribution

Thanks a lot, just what I was looking for.
This should be in drupal.org

Great contribution

Thanks a lot

Regards
W.Wallace

Fine

Thanks a lot! You are great.

This is the right one i`ve be searching a long time on the web.

It`s easy to implement and works fine.

Best regards from munich!
Axel

Thanks a lot, just what I was

Thanks a lot, just what I was looking for.
This should be in drupal.org

Thanks for pointing out that function

That helped a lot. I had not found that function before but needed to get rid of the css that this function is adding to the HTML code:

  1.     $output = '<div style="width: ' . $info['width'] . 'px" class="' . $class . '">';
  2.     $output .= l(image_display($image, $img_size), "node/$iid", array('html' => TRUE));
  3.     $output .= '</div>'."\n";

Now, it's easy. And I agree with the previous commentator, that this should be somehow explained on drupal.org as part of the image attach functionality.

That was cool hack. I am not

That was cool hack. I am not a good programmer specially can't play with Drupal but give my most time to Wordpress. This image attach module was giving a lot pain in my a** :). Every image were being attached which should have been disabled to save. Anyway, now I can move to search for new hack to disable saving image from my website.
Facebook Application Analytics

On the Theming side...

This script works unless you're using the latest version of image-attach. I believe they updated some things. Correct me if I'm wrong.

Here's a way to get just the image source using regex on the theme side:

  1. $image = $node->content["image_attach"]["#value"];
  2. preg_match('/(?<=src=")[^"]*/', $image, $img_url);
  3. print('<img src="'.$img_url[0].'"/>');

This will print the image url instead of all that other slop.

Thanks for the code and

Thanks for the code and comment, I now favor using CCK fields for images in combo with imagecache, I find that combo works better and it tends not clutter things up as image nodes are no longer being created. CCK images also work better if you ever want to render that stuff in any kind of view. cheers!

Duly Noted

Interesting. Thanks for the reply.

This works with the latest version of image_attach in D6

  1. function your_theme_image_attach_attached_images($nid, $image_nodes = array(), $options = array()) {
  2.   // Merge in defaults.
  3.   $options += array(
  4.     'size' => IMAGE_THUMBNAIL,
  5.     'link' => 'image',
  6.     'attributes' => array(),
  7.   );
  8.  
  9.   $img_size = $options['size'];
  10.   $link_destination = $options['link'];
  11.  
  12.   // Link images to the attaching node.
  13.   if ($link_destination == 'node') {
  14.     $link_path = "node/$nid";
  15.   }
  16.  
  17.   $output = '';
  18.   foreach ($image_nodes as $image) {
  19.     if (!node_access('view', $image)) {
  20.       // If the image is restricted, don't show it as an attachment.
  21.       continue;
  22.     }
  23.  
  24.     // Link images to the image node.
  25.     if ($link_destination == 'image') {
  26.       $link_path = NULL;
  27.     }
  28.  
  29.     // Get a fresh copy of the attributes for each image node.
  30.     $div_attributes = $options['attributes'];
  31.  
  32.     // Create CSS classes, beginning with those passed in to the function.
  33.     $classes = array();
  34.     if (isset($div_attributes['class'])) {
  35.       $classes[] = $div_attributes['class'];
  36.     }
  37.     // replace with base class in DIV
  38.     //$classes[] = 'image-attach-' . $teaser_or_body;
  39.     $classes[] = 'image-attach-node-' . $image->nid;
  40.     if (!$image->status) {
  41.       $classes[] = 'image-unpublished';
  42.     }
  43.     $div_attributes['class'] = implode(' ', $classes);
  44.  
  45.     // Add the width as inline CSS.
  46.     $info = image_get_info(file_create_path($image->images[$img_size]));
  47.     if (!isset($div_attributes['style'])) {
  48.       $div_attributes['style'] = '';
  49.     }
  50.     $div_attributes['style'] .= 'width: ' . $info['width'] . 'px;';
  51.  
  52.     $output .= '<div' . drupal_attributes($div_attributes) . '>';
  53.     $image_img = image_display($image, $img_size);
  54.     if (isset($link_path)) {
  55.       $output .= l($image_img, $link_path, array('html' => TRUE));
  56.     }
  57.     else {
  58.       $output .= $image_img;
  59.     }
  60.     $output .= "</div>\n";
  61.   }
  62.  
  63.   return $output;
  64. }