In order to address some Views performance issues and respect best practices, I'd like to replace some Views PHP I configured some time ago by my own custom handlers.
For instance I've got a Views PHP field, excluded from display, with that setup :
Value code :
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;
}
}
Output code :
<?php print $value ; ?>`
Then I use that field as first sort criteria (ascending), in a Global PHP sort criteria:
if ($row1->php> $row2->php) return -1; else return 1;
I'd be really thankful if you could put me on the right way: in which function(s) shall I build that same code to end up with PHP in database ?
Summary :
After search and progress, plus @Renrahf help, most of the implementation seems ok, detailed below. But I'm still fighting with one point : I added a custom field handler to calculate a value, but how can I order by that handler ?
Edits :
What I did so far :
.info file
files[] = views_handler_vts_products_sort.inc
files[] = includes/views_handler_vts_count_depconf_field.inc
Module file
/**
* 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
file
/**
* 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
file
/*
* 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;
}
}
Remaining question :
how to order by the custom field handler ? I tried adding
'click sortable'=> TRUE,
OR'sort' => array('handler' => 'views_handler_sort',),
OR$this->query->add_orderby('custom', 'count_depconf_field', 'DESC');
into custom sort handler. None works but return Unknown column in 'order clause'DONE : How can I get
$row->product_id
and$row->nid
insidequery()
? I need it to build the subquery. : Added a views handler field and found the row values in render($values)...- DONE : Which part of the example handler do I have to edit ? The query function only ? Do I need to keep the whole example code or just the custom parts ?
Thank you