以前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中直接导入、使用数据库”的时候替代。
评论