I knew how to do that in D6, using an overide of an imagecache function.
But now, in D7, I just use the core image field (such as we have in Article content type).

In my template.php, I added

function bartik_image($variables) {
  $variables['attributes']['target'] = "_blank";
  $attributes = $variables['attributes'];
  $attributes['src'] = file_create_url($variables['path']);
  foreach (array('width', 'height', 'alt', 'title', 'target') as $key) {
    if (isset($variables[$key])) {
      $attributes[$key] = $variables[$key];
    }
  }
  return '<img' . drupal_attributes($attributes) . ' />';
}

I get the new attribute, but it does not change the link.
How to proceed?

Comments

bfroehle’s picture

The actual linking is done with the A tag surrounding the image.

The theme function which produces the image and link is
theme_image_formatter().

Could you accomplish this with a preprocessor function? Something like

function bartik_preprocess_image_formatter(&$variables) {
  $variables['path']['options']['attributes']['target'] = '_blank';
}
sahuni’s picture

It's exactly that, thanks a lot!

bfroehle’s picture

Status: Active » Fixed

Glad to hear it. :)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Peasoup’s picture

Hey :)
Sorry to reopen a closed issue, but this is exactly what i also want to do, but apparently I do something wrong. I added bfroehle's code (except the php tags of course) at the end of my template.php, but nothing changed. What am I doing wrong?
(I'm using a "subtheme" of bartik, basically just a clone in sites/all/themes, with minor changes, which do apply)
Thanks!

Jarvis Lukow’s picture

Hey there,

I was looking for something similar before, but didn't want to add the '_blank' target to all "a" tags, so I added the following code in my module. Hope this helps someone else.

Thanks,

$image = array(
                    'item' => $node->field_image['und'][0],
                    'image_style' => 'equipment_teaser',
                    'path' => array(
                                'path' => $path,
                                'options' => array(
                                    'attributes' => array(
                                        'target' => '_blank'
                                      )
                                    )
                                )
                );

$content = theme_image_formatter($image);

jond’s picture

jarvislukow, did you add this as a custom module, or in the template.php file?