Revision: 64006
Updated Code
at June 26, 2013 21:11 by brownrl
Updated Code
<?php /** * @file sites/all/modules/MODULE/MODULE.views.inc */ /** * Implements hook_views_data_alter(). */ function MODULE_views_data_alter(&$data) { $data['node']['MODULE_has_xxxyyyzzz'] = array( 'title' => t('Module: has xxxyyyzzz'), 'help' => t('Check if fields has value.'), 'field' => array( 'handler' => 'MODULE_handler_field_has_xxxyyyzzz', /*'click sortable' => TRUE,*/ ), 'sort' => array( 'handler' => 'MODULE_handler_sort_has_xxxyyyyzzz' ), 'filter' => array( '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. $this->text_fields = array( 'some_alias' => array( 'table' => 'table_of_field', 'field' => 'value_column', ), 'some_other_alias' => array( '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 foreach( array_keys( $this->text_fields ) as $alias ) { // There is a value, return YES if( ! empty( $values->{$this->aliases[$alias]} ) ) { 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); $this->text_fields = array( 'some_alias' => array( 'table' => 'table_of_field', 'field' => 'column_of_field', ), 'some_other_alias' => array( '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() { if (isset($this->value) && $this->value !== FALSE) { $fields = array(); 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. $where = ' LENGTH( CONCAT_WS( "", '.implode(",",$fields).' ) ) != 0 '; $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); $this->text_fields = array( 'some_alias' => array( 'table' => 'field_table', 'field' => 'field_value_column', ), ); } // add the fields to the query function query() { $fields = array(); 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' ); } }
Revision: 64005
Updated Code
at June 26, 2013 21:09 by brownrl
Updated Code
<?php /** * @file sites/all/modules/MODULE/MODULE.views.inc */ /** * Implements hook_views_data_alter(). */ function MODULE_views_data_alter(&$data) { $data['node']['MODULE_has_xxxyyyzzz'] = array( 'title' => t('Module: has xxxyyyzzz'), 'help' => t('Check if fields has value.'), 'field' => array( 'handler' => 'MODULE_handler_field_has_xxxyyyzzz', /*'click sortable' => TRUE,*/ ), 'sort' => array( 'handler' => 'MODULE_handler_sort_has_xxxyyyyzzz' ), 'filter' => array( '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. $this->text_fields = array( 'some_alias' => array( 'table' => 'table_of_field', 'field' => 'value_column', ), 'some_other_alias' => array( '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 foreach( array_keys( $this->text_fields ) as $alias ) { // There is a value, return YES if( ! empty( $values->{$this->aliases[$alias]} ) ) { 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); $this->text_fields = array( 'some_alias' => array( 'table' => 'table_of_field', 'field' => 'column_of_field', ), 'some_other_alias' => array( '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() { if (isset($this->value) && $this->value !== FALSE) { $fields = array(); 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. $where = ' LENGTH( CONCAT_WS( "", '.implode(",",$fields).' ) ) != 0 '; $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); $this->text_fields = array( 'some_alias' => array( 'table' => 'field_table', 'field' => 'field_value_column', ), ); } // add the fields to the query function query() { $fields = array(); 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'], 'provol_review_has_quote_sort' ); } }
Revision: 64004
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at June 26, 2013 20:28 by brownrl
Initial Code
<?php /** * @file * Contains provol_review_handler_field_has_quote. */ 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. $this->text_fields = array( 'some_alias' => array( 'table' => 'table_of_field', 'field' => 'value_column', ), 'some_other_alias' => array( '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 foreach( array_keys( $this->text_fields ) as $alias ) { // There is a value, return YES if( ! empty( $values->{$this->aliases[$alias]} ) ) { return t("yes"); } } // If we go to here then it is no. return t("no"); } }
Initial URL
http://www.itsgotto.be
Initial Description
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.
Initial Title
Custom Drupal View Field
Initial Tags
drupal
Initial Language
PHP