为了解决某些Views性能问题并尊重最佳实践,我想用自己的自定义处理程序替换我之前配置的一些Views PHP。
例如,我有一个Views PHP字段,该显示设置除外,该字段从display排除:
值码:
if( $row->sticky ==1 ) {
return 100;
} else {
if ( isset($row->product_id) && $row->product_id != "" ){
$query = "SELECT COUNT(statut.entity_id) FROM field_data_field_statut_depart statut"
. " INNER JOIN field_data_field_product product ON statut.entity_id= product.field_product_product_id"
. " INNER JOIN field_data_field_date_depart depart ON statut.entity_id = depart.entity_id"
. " WHERE product.entity_id = ". $row->nid." AND field_statut_depart_value IN (2,3) AND field_date_depart_value > NOW(); ";
$select = db_query($query);
$count = $select->fetchField();
return $count;
}
else {
return -1;
}
}
输出代码 :
<?php print $value ; ?>`
然后,在Global PHP排序条件中,将该字段用作第一排序条件(ascending):
if ($row1->php> $row2->php) return -1; else return 1;
如果您能采取正确的方法,我将不胜感激:我应该在哪个函数中构建相同的代码以在数据库中以PHP结尾?
总结:
经过搜索并取得进展,再加上@Renrahf帮助,大多数实现似乎还可以,下面详细介绍。但是我仍然在争论一点:我添加了一个自定义字段处理程序来计算值,但是如何按该处理程序排序?
编辑:
到目前为止,我做了什么:
.info文件
files[] = views_handler_vts_products_sort.inc
files[] = includes/views_handler_vts_count_depconf_field.inc
模块文件
/**
* Implements hook_views_data().
*/
function vts_views_handler_views_data() {
$data['custom']['table']['group'] = t('Custom');
$data['custom']['table']['join'] = array(
// #global is a special flag which let's a table appear all the time.
'#global' => array(),
);
$data['custom']['custom_handler'] = array(
'title' => t('Vts custom Sort Handler'),
'help' => 'Sorts products by sticky first then by custom statut field',
'sort' => array(
'handler' => 'views_handler_vts_products_sort',
),
);
$data['custom']['count_depconf_field'] = array(
'title' => t('Sum of products with status confirmed '),
'help' => t('Calculate Sum of products with status confirmed, to order lists".'),
'field' => array(
'handler' => 'views_handler_vts_count_depconf_field',
'click sortable'=> TRUE,
),
/*'sort' => array(
'handler' => 'views_handler_sort',
), */
);
return $data;
}
function vts_views_handler_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'vts_views_handler'),
);
}
views_handler_vts_products_sort
文件
/**
* Base sort handler that has no options and performs a simple sort.
*
* @ingroup views_sort_handlers
*/
class views_handler_vts_products_sort extends views_handler_sort {
function query() {
$this->ensure_my_table();
// Add the field.
$this->query->add_orderby('node', 'sticky', 'DESC');
}
}
views_handler_vts_count_depconf_field
文件
/*
* A simple field to calculate the value I wish to order by.
*/
class views_handler_vts_count_depconf_field extends views_handler_field {
function query() {
//do nothing
}
function render($values) {
$count = 0;
$product_id = isset($values-> commerce_product_field_data_field_product_product_id)? $values-> commerce_product_field_data_field_product_product_id: NULL;
if(!is_null($product_id)){
$query = "SELECT COUNT(statut.entity_id) FROM field_data_field_statut_depart statut"
. " INNER JOIN field_data_field_product product ON statut.entity_id= product.field_product_product_id"
. " INNER JOIN field_data_field_date_depart depart ON statut.entity_id = depart.entity_id"
. " WHERE product.entity_id = " . $values->nid . " AND field_statut_depart_value IN (2,3) AND field_date_depart_value > NOW(); ";
$select = db_query($query);
$count = $select->fetchField();
}
return $count;
}
}
剩余的问题:
如何通过自定义字段处理程序订购?我尝试将
'click sortable'=> TRUE,
OR'sort' => array('handler' => 'views_handler_sort',),
OR 添加$this->query->add_orderby('custom', 'count_depconf_field', 'DESC');
到自定义排序处理程序中。没有任何工作,但在“ order子句”中返回Unknown列DONE:我怎样才能
$row->product_id
和$row->nid
里面query()
?我需要它来构建子查询。:添加了一个视图处理程序字段,并在render($ values)中找到了行值...- 完成:我必须对示例处理程序的哪一部分进行编辑?仅查询功能?我是否需要保留整个示例代码或仅保留定制部分?
谢谢