2010-08-13 55 views
1

我正在使用帶有用於drupal的圖像庫的imagecache。當該字段設置爲鏈接到原始圖像的圖像緩存預設時。我希望它能夠在點擊預設圖像時將原始圖像加載到新選項卡中。如何將target =「_ blank」屬性添加到圖像緩存預設?Drupal - imagecache鏈接到圖像,將屬性設置爲目標=「_ blank」

+0

對於您的信息:嚴格的文檔類型不支持目標屬性。你可以寫: onclick =「window.open(this.href); return false;」 – Rimian 2010-08-14 06:18:52

回答

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"屬性添加到鏈接。

+0

甜!我是否必須爲每個預設定義一個新功能,或者是否有將它們組合的方法? – Toxid 2010-08-14 01:47:04

+0

截至目前,您必須爲每個預設定義一個新功能。應該可以直接用'mytheme_imagecache_formatter_imagelink()'重寫'theme_imagecache_formatter_imagelink()',但是Imagecache使用的是深度魔法,我不完全理解需要主題函數名稱擴展。當我知道這是爲什麼時,會更新我的答案。 – 2010-08-14 03:49:26

相關問題