【问题标题】:Drupal - imagecache link to image, set attribute to target="_blank"Drupal - imagecache 链接到图像,将属性设置为 target="_blank"
【发布时间】:2011-06-19 23:00:44
【问题描述】:

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

【问题讨论】:

  • 供您参考:严格的文档类型不支持目标属性。你可以写: onclick="window.open(this.href); return false;"

标签: php image drupal


【解决方案1】:

您需要在您的主题中覆盖 与图像链接的图像 字段格式化程序,该格式化程序由 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" 属性添加到链接。

【讨论】:

  • 甜蜜!我必须为每个预设定义一个新功能还是有办法将它们组合起来?
  • 到目前为止,您必须为每个预设定义一个新功能。应该可以直接用mytheme_imagecache_formatter_imagelink() 覆盖theme_imagecache_formatter_imagelink(),但是Imagecache 正在使用我不完全理解的深层魔法需要主题函数名称扩展。当我知道这是为什么时会更新我的答案。
猜你喜欢
  • 1970-01-01
  • 2019-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多