Drupal - imagecache链接到图像,将属性设置为target =“_ blank”

时间:2010-08-13 22:42:57

标签: php image drupal

我正在使用带有drupal图像库的imagecache。当字段设置为链接到原始图像的图像缓存预设时。我想要它,以便在单击预设图像时,原始图像加载到新选项卡中。如何将target =“_ blank”属性添加到图像缓存预设?

1 个答案:

答案 0 :(得分:0)

您需要覆盖由theme_imagecache_formatter_imagelink()创建的主题中链接到图像字段格式化程序的图像。将其添加到主题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));
}

mythememypreset分别替换为主题的短名称和预设的简称。此函数与theme_imagecache_formatter_imagelink()相同,但最后一行除外,它将target="_blank"属性添加到链接。