开发者

Drupal - imagecache link to image, set attribute to target="_blank"

I'm using imagecache with an image gall开发者_如何学运维ery for drupal. When the field is set to an imagecache preset that links to the original image. I want it so that when the preset image is clicked, the original image loads in a new tab. How can I add the target="_blank" attribute to an imagecache preset?


You need to override the image linked to image field formatter in your theme, which is created by theme_imagecache_formatter_imagelink(). Add this to your theme's template.php:

function mytheme_imagecache_formatter_mypreset_imagelink($element) {
  // Inside a view $element may contain NULL data. In that case, just return.
  if (empty($element['#item']['fid'])) {
    return '';
  }

  // Extract the preset name from the formatter name.
  $presetname = substr($element['#formatter'], 0, strrpos($element['#formatter'], '_'));
  $style = 'imagelink';

  $item = $element['#item'];
  $item['data']['alt'] = isset($item['data']['alt']) ? $item['data']['alt'] : '';
  $item['data']['title'] = isset($item['data']['title']) ? $item['data']['title'] : NULL;

  $imagetag = theme('imagecache', $presetname, $item['filepath'], $item['data']['alt'], $item['data']['title']);
  $path = file_create_url($item['filepath']);
  $class = "imagecache imagecache-$presetname imagecache-$style imagecache-{$element['#formatter']}";
  return l($imagetag, $path, array('attributes' => array('class' => $class, 'target' => '_blank'), 'html' => TRUE));
}

Replace mytheme and mypreset with the short name of your theme and the short name of your preset, respectively. This function is identical to theme_imagecache_formatter_imagelink() except for the last line, which adds the target="_blank" attribute to the link.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜