Drupal7 : How to add _blank to hyper link in views ?

如果在 Drupal7 裡面想要在系統產生出來的超連結上加入 _blank 另開視窗的標籤
該怎麼做呢?
一般都會用他內建的 rewrite 功能把
但你會發現某些東西例如檔案路徑不知道怎麼生出來
可能要裝某些 Token 才有辦法知道路徑
感覺很簡單的功能確距離好遙遠阿…

不過既然 Drupal7 是 OpenSource
就自然有熱心的朋友把這功能實做出來囉!
使用的方式也很簡單
這個 case 是當你的檔案是 pdf 或 txt 時
系統會自動在 hyper link 那加上 target="_blank" 的標籤

function THEMENAME_file_link($variables) {
  $file = $variables['file'];
  $icon_directory = $variables['icon_directory'];
  $url = file_create_url($file->uri);
  $icon = theme('file_icon', array('file' => $file, 'icon_directory' => $icon_directory));
  // Set options as per anchor format described at
  // http://microformats.org/wiki/file-format-examples
  $options = array(
    'attributes' => array(
      'type' => $file->filemime . '; length=' . $file->filesize,
    ),
  );
  // Use the description as the link text if available.
  if (empty($file->description)) {
    $link_text = $file->filename;
  }
  else {
    $link_text = $file->description;
    $options['attributes']['title'] = check_plain($file->filename);
  }
  //open files of particular mime types in new window
  $new_window_mimetypes = array('application/pdf','text/plain');
  if (in_array($file->filemime, $new_window_mimetypes)) {
    $options['attributes']['target'] = '_blank';
  }
  return '' . $icon . ' ' . l($link_text, $url, $options) . '';
}

上面這個 function 請放到你的 theme 中的 template.php 中
並把 function name 裡面的 THEMENAME 置換成你 theme 的名稱
儲存後執行 Clear all caches 就會實現囉!

Reference
Open File in New Window (target='_blank')


Comments

Popular posts from this blog

CentOS7 如何新增/移除 Rich Rule for firewalld

mac 安裝 mtr

Howto use Postman test Cors