views

Views3 的 templates 選項

Views 3.x 的介面雖然和 views 2.x 有點相似 但便使用了更多的 DHTML, AJAX 技術 之前有網友說找不到 views 3.x templates suggestion 的選項 我截了些圖給大家參考:

Views List

Views Edit

Views Edit Advanced

Views Templates Suggestions

Development Tips: strip html tags on Drupal like view's doing

Views 的 fields 輸出項目有一個很方便的功能
它可以指定輸出一大段文字中的首 x 個字符
輸出 body 的時候可以大約控制欄位的長闊
實作原來是使用了 Drupal api:
http://api.drupal.org/api/drupal/modules--filter--filter.module/function...

因為 body 會自動插入 <p>
使用一般的 trim() 的話便會有 invalidate HTML 的問題
在 IE 上的版位便會變得更難控制了..

#IEhell

Updating views fields changes field alias names

When views is using fields as output,
you can hidden the field, and echo them in other field templates
you will get an alias, but that alias is willing to change.
So you cannot hardcode that in tpl files.

In order to retrieve the alias and its value:

<?php
//within field tpl files
$value = $view->field['field_host_value']->render($row);
?>

http://drupal.org/node/361756#comment-4424090

當使用Views 輸出fields 的時候, 你可以隐藏 (hidden) 一個 field
但這個 field 的別名 (alias) 是會改變的
所以你不可以將這個別名寫到 code 內

要在 code 取得這個別名,

<?php
//within field tpl files
$value = $view->field['field_host_value']->render($row);
?>

http://drupal.org/node/361756#comment-4424090

Views 過濾列表預設不返回結果 Views exposed filter default show zero result

Show No Results by Default

原理是使用 Global argument
如果 filter 的返回值都是預設值的話, 用 argument 返回 false,
再輸出 empty text 或者不輸出值

arguments: Global Null
provide default argument
fixed entry
PHP Code:

<?php
foreach ($view->filter as $filter) {
  if (
$filter->options['exposed']) {
   
$value = $view->display[$view->current_display]->handler->handlers['filter'][$filter->options['id']]->value;   
    if(!empty(
$value)) {
      if(
is_array($value)) {
       
$val = array_pop($value);
        if(!empty(
$val)) {
          return
true;
        }
      }else{
        if(!empty(
$value)) {
          return
true;
        }
      }
    }
  }
}
return
false;
?>

用 PHP 來提取 views 的結果: $view->preview()

EDIT 2010-01-13 <?php $view->access() ?>

之前有提及過用 PHP 來提取 views 的結果: 從 fid 提取上傳檔案的資料

但發覺這個方法不可以拿出 Filefield 的資料, 原因未明
因為效能問題不想使用 node_load(), 花了些時間挖
Views 的 AJAX paging 就是使用 <?php $view->preview()?> (可以參考右欄 Recent content 的 paging)

實際使用:

<?php
    $view
= views_get_view($name);
    if (
is_object($view)) {
     
$output .= $view->preview($display_id,array($taxonomy->name)); //第二個 arg array() 是給views 用的 argments
   
}
?>

此方法可以完全使用 views 的 templates, display options 等, 真正方便

EDIT:
要檢查權限:<?php $view->access($display_id) ?>

再加一個方法:

<?php
views_embed_view
($name, $display_id, $arg1, $arg2);
?>

客製輸出 views 的多個值欄位 (theming view field with multiple values)

Theme developer 顯示, theme_content_view_multiple_field() 決定多個值的時候 views 的輸出, 
源碼:

<?php
function theme_content_view_multiple_field($items, $field, $values) {
 
$output = '';
  foreach (
$items as $item) {
    if (!empty(
$item) || $item == '0') {
     
$output .= '<div class="field-item">'. $item .'</div>';
    }
  }
  return
$output;
}
?>

自定成用逗號分隔, 例如 value1, value2, value3
theme 內的 template.php:

<?php
function theme_content_view_multiple_field($items, $field, $values) {
 
$output = '';
  foreach (
$items as $item) {
    if (!empty(
$item) || $item == '0') {
     
$output .= ($item==end($items))? $item:$item.', ';
    }
  }
  return
$output;
}
?>

從 fid 提取上傳檔案的資料 Get file info from fid: Drupal6

Get views result anywhere use:

<?php
views_get_view_result
($view_name,$display_name);
?>

EDIT 2011-01-10: or use $view->preview()

But it only return fid in imagefield

To get to filepath and other info, you have to:

<?php
$file
= field_file_load($node->website_logo_fid);
$output .= "<p><img src=\"".$file['filepath']."\" alt=\"\" /></p>";
?>

source: http://snipplr.com/view/26812/drupal--get-the-image-path-of-a-cck-field-...

result:

last, output image tag:

<?php
echo theme_image($path,$alt,$title);
?>

http://api.drupal.org/api/drupal/includes--theme.inc/function/theme_image/6

2010-11-01 features module - 一個 Drupal 內建的 views 版本管理系統

一直以來, Drupal 都沒有有效的方法可以管理 views 的設定參數
意思是說, 例如測試環境對 views 模組所做的變更,
沒有方便的方法可以將它轉移到真的對外伺服器之上
而只可以用 mysqldump, 或者複雜的使用 views 的 export
相似的問題出現在 cck 之上

最新的解決方法就是 features 模組
它的主體功能是將選取的設定輸出到一個全新的, 自定名稱的 "設定模組"
讓你下載 解壓 上傳 啟用 "設定模組"
然後只要 views cck 設定有改變
features 模組會知道
並給予:
輸出 (輸出新的 "設定模組"到真的對外環境)
還原 (還原到 "設定模組"的設定)

而你從此便可以對 views 和 cck 的設定參數作 SCM 了

edit: Part 2 (2010-11-22)

Multiple display::attachment to views

Display::attachment 是 views 2.x 新加的一個 display 類
用法類似block, 但它的特別之處是它的位置一定要在另一個 display::page 的前或後
所以才有 "attachment" 的名號

例如, "最近文章" (/recent_post) 前需要一個 "特選文章" 的block
當然可以建立一個 block, 然後使用 region 的方式放到 "最近文章" 頁面前
但也可以使用 attachment, attach 到 page 的前面就可以了

attachment 都有自己的 templates, 操作上和原理上都和一般的 display 沒有大分別
但比起 region+block 的方式就方便多, 也不需要自己增加 region 了

Drupal, Views date format

Drupal 內需要顯示日期的時間有很多,
node 的內頁, index page 的文章按日期排列等等

但 Drupal 只內建long, medium, short 三種預定了的日期格式
需要其他的格式當然可以自己在 theme 的層面使用 php 的 date()
但其實 Drupal 的 date module 可以使用 Drupal 設定的方式改變輸出的格式
Administer ›› Site configuration ›› Date and time ›› format ›› add (admin/settings/date-time/formats/add)
就可以將 "short" 改成 Aug 21, 2010 之類的格式了