/ Published in: PHP
This is a custom field handler for a Drupal view. Sometimes you have two or more fields in a content type and you want to do a test on them to see if both are empty or both full or exclusive and return that as a field.
There are probably ways to do this in the admin/view configuration. However, this code also serves as a general skeleton for just any old custom field handler.
* Make sure that all files that you make/add to your module that you add to your .info file. If you forget this you will bash your head in later trying to figure WTF this is not working.
There are probably ways to do this in the admin/view configuration. However, this code also serves as a general skeleton for just any old custom field handler.
* Make sure that all files that you make/add to your module that you add to your .info file. If you forget this you will bash your head in later trying to figure WTF this is not working.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php /** * @file sites/all/modules/MODULE/MODULE.views.inc */ /** * Implements hook_views_data_alter(). */ function MODULE_views_data_alter(&$data) { 'title' => t('Module: has xxxyyyzzz'), 'help' => t('Check if fields has value.'), 'handler' => 'MODULE_handler_field_has_xxxyyyzzz', /*'click sortable' => TRUE,*/ ), 'handler' => 'MODULE_handler_sort_has_xxxyyyyzzz' ), 'handler' => 'MODULE_handler_filter_has_xxxyyyzzz', 'type' => 'yes-no', ), ); } /** * @file sites/all/modules/MODULE/views/handlers/MODULE_handler_field_has_xxxyyyzzz.inc */ class MODULE_handler_field_has_xxxyyyzzz extends views_handler_field { /** * Establish which fields to roll up and to check. */ function init(&$view, &$data) { // Call parent init parent::init($view, $data); // Establish which fields we are testing. 'table' => 'table_of_field', 'field' => 'value_column', ), 'table' => 'table_of_other_field', 'field' => 'other_value_column', ), ); } /** * Roll up the fields into the query */ function query() { foreach ($this->text_fields as $name => $info) { // Add the table to the query $table = $this->query->ensure_table($info['table'], $this->relationship); // Get the alias that this table is going to be for later. $this->aliases[$name] = $this->query->add_field($table, $info['field']); } } // Goes through the fields and see if there is a text or value function render($values) { // loop through each field and see if there is a value // There is a value, return YES return t("yes"); } } // If we got to here then it is no. return t("no"); } } /** * @file sites/all/modules/MODULE/views/handlers/MODULE_handler_filter_has_xxxyyyzzz.inc */ class MODULE_handler_filter_has_xxxyyyzzz extends views_handler_filter_boolean_operator { // Like the field itself here we says what fields we are going // to work on function init(&$view, &$data) { // Call parent commit parent::init($view, $data); 'table' => 'table_of_field', 'field' => 'column_of_field', ), 'table' => 'table_of_other_field', 'field' => 'column_of_other_field', ), ); } // Here we will roll up all the fields and make sure // That they are in the query. function query() { foreach ($this->text_fields as $name => $info) { $table = $this->query->ensure_table($info['table'], $this->relationship); $fields[] = $info['field']; // check this to make sure you doing the right fields. } // CONCAT all the fields, and see the length. $this->query->add_where_expression($this->options['group'], $where); } } } /** * @file sites/all/modules/MODULE/views/handlers/MODULE_handler_sort_has_xxxyyyzzz.inc */ class MODULE_handler_sort_has_xxxyyyzzz extends views_handler_sort { function init(&$view, &$data) { // call parent init parent::init($view, $data); 'table' => 'field_table', 'field' => 'field_value_column', ), ); } // add the fields to the query function query() { foreach ($this->text_fields as $name => $info) { $table = $this->query->ensure_table($info['table'], $this->relationship); $this->query->add_field($table, $info['field']); $fields[] = $info['field']; } // concat all the fields and see the length. $this->query->add_orderby( NULL, "LENGTH( CONCAT_WS( "" , ' . implode( "," , $fields ) . ' ) ) >= 1", $this->options['order'], 'MODULE_has_xxxyyyzzz_sort' ); } }