以前Drupal網站中遇到需要對Views的輸出進行一些變換的時候,一般是通過安裝Views PHP這個模塊,在Views中添加Global PHP字段,在其中設置讀取什麼值、進行什麼變換、輸出什麼内容,這個方式很早就開始用了,Drupal 6和Drupal 7都用過。但缺點是設置麻煩,還可能會引起負載的升高。
最近在調試一個新的網站中就遇到數據量大的時候,服務器報500錯誤,甚至導緻阿裡雲服務器死機的情況。檢查後發現都是與Views相關,先安裝了Views PagerLite來減少MySQL的Count指令,但依然有不少内存不足的報錯。
後來在Drupal官方網站上找到views-view-field.tpl.php的說明,按照這個來進行配置、修改,可以達到與Views PHP一樣的效果,但不會一起負載增高,真是一個好辦法!
詳細說明請看:views-view-field.tpl.php
2016年11月11日補充原始模闆代碼為:
<?php
/**
* @file
* This template is used to print a single field in a view.
*
* It is not actually used in default Views, as this is registered as a theme
* function which has better performance. For single overrides, the template is
* perfectly okay.
*
* Variables available:
* - $view: The view object
* - $field: The field handler object that can process the input
* - $row: The raw SQL result that can be used
* - $output: The processed output that will normally be used.
*
* When fetching output from the $row, this construct should be used:
* $data = $row->{$field->field_alias}
*
* The above will guarantee that you'll always get the correct data,
* regardless of any changes in the aliasing that might happen if
* the view is modified.
*/
?>
<?php print $output; ?>
修改後的views-view-field--country_code.tpl.php代碼示範:
<?php
if ($view->name == 'continent_english' && $view->current_display == 'page_1' && isset($row->{$field->field_alias})) {
$value = $row->{$field->field_alias};
$country_code = $value;
$country_name_english = country_code_name($country_code);
$country_name = t($country_name_english);
$flag = country_code_flag_16($country_code);
$output = "$flag $country_code ($country_name)";
print $output;
} else {
print $output;
}
?>
說明:先根據$view->name和$view->current_display确定view以及page,然後讀取country_code字段的值,變換成需要的格式再輸出。
更多$view、$field、$row的key如下:
$view
db_table
base_table
base_field
name (确定view用)
vid
description
tag
human_name
core
api_version
disabled
editing
args
use_ajax
result
current_page
items_per_page
offset
total_rows
exposed_raw_input
old_view
parent_views
is_attachment
current_display(确定page用)
query
display_handler
display
style_plugin
style_options
row_index
override_url
override_path
base_database
field
argument
sort
filter
relationship
header
footer
empty
table
type
export_type
executed
built
build_info
attachment_before
attachment_after
dom_id
relationships_fixed
localization_plugin
inited
build_sort
plugin_name
build_time
execute_time
$field
field_alias (就是字段名稱、模闆文件名中的country_code)
aliases
original_value
additional_fields
view
query
handler_type
table_alias
real_field
relationship
options
definition
is_handler
localization_keys
table
field
position
last_render
last_render_text
last_tokens
$row
ipshu_ip2location_lite_asn_asn ($data = $row->ipshu_ip2location_lite_asn_asn, 更簡單的辦法是$data = $row->{$field->field_alias})
ipshu_ip2location_lite_asn_as
2017年7月18日補充《Drupal視圖Views可用的顯示模闆文件》,特别是用到views-view-fields.tpl.php,例如:views-view-fields--basic_company_data--page_1.tpl.php這樣的模闆,與node--xxx.tpl.php很類似,甚至可以在“在Drupal中直接導入、使用數據庫”的時候替代。
评论