Custom Drupal View Field


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. /**
  4.  * @file sites/all/modules/MODULE/MODULE.views.inc
  5.  */
  6.  
  7.  
  8. /**
  9.  * Implements hook_views_data_alter().
  10.  */
  11. function MODULE_views_data_alter(&$data) {
  12.  
  13. $data['node']['MODULE_has_xxxyyyzzz'] = array(
  14. 'title' => t('Module: has xxxyyyzzz'),
  15. 'help' => t('Check if fields has value.'),
  16. 'field' => array(
  17. 'handler' => 'MODULE_handler_field_has_xxxyyyzzz',
  18. /*'click sortable' => TRUE,*/
  19. ),
  20. 'sort' => array(
  21. 'handler' => 'MODULE_handler_sort_has_xxxyyyyzzz'
  22. ),
  23.  
  24. 'filter' => array(
  25. 'handler' => 'MODULE_handler_filter_has_xxxyyyzzz',
  26. 'type' => 'yes-no',
  27. ),
  28. );
  29. }
  30.  
  31.  
  32.  
  33.  
  34. /**
  35.  * @file sites/all/modules/MODULE/views/handlers/MODULE_handler_field_has_xxxyyyzzz.inc
  36.  */
  37. class MODULE_handler_field_has_xxxyyyzzz extends views_handler_field {
  38.  
  39.  
  40. /**
  41.   * Establish which fields to roll up and to check.
  42.   */
  43. function init(&$view, &$data) {
  44.  
  45. // Call parent init
  46. parent::init($view, $data);
  47.  
  48. // Establish which fields we are testing.
  49. $this->text_fields = array(
  50. 'some_alias' => array(
  51. 'table' => 'table_of_field',
  52. 'field' => 'value_column',
  53. ),
  54. 'some_other_alias' => array(
  55. 'table' => 'table_of_other_field',
  56. 'field' => 'other_value_column',
  57. ),
  58. );
  59.  
  60. }
  61.  
  62. /**
  63.   * Roll up the fields into the query
  64.   */
  65. function query() {
  66.  
  67. foreach ($this->text_fields as $name => $info) {
  68.  
  69. // Add the table to the query
  70. $table = $this->query->ensure_table($info['table'], $this->relationship);
  71.  
  72. // Get the alias that this table is going to be for later.
  73. $this->aliases[$name] = $this->query->add_field($table, $info['field']);
  74. }
  75.  
  76. }
  77.  
  78. // Goes through the fields and see if there is a text or value
  79. function render($values) {
  80.  
  81. // loop through each field and see if there is a value
  82. foreach( array_keys( $this->text_fields ) as $alias ) {
  83.  
  84. // There is a value, return YES
  85. if( ! empty( $values->{$this->aliases[$alias]} ) ) {
  86. return t("yes");
  87. }
  88. }
  89.  
  90. // If we got to here then it is no.
  91. return t("no");
  92. }
  93. }
  94.  
  95.  
  96.  
  97.  
  98.  
  99. /**
  100.  * @file sites/all/modules/MODULE/views/handlers/MODULE_handler_filter_has_xxxyyyzzz.inc
  101.  */
  102. class MODULE_handler_filter_has_xxxyyyzzz extends views_handler_filter_boolean_operator {
  103.  
  104. // Like the field itself here we says what fields we are going
  105. // to work on
  106. function init(&$view, &$data) {
  107.  
  108. // Call parent commit
  109. parent::init($view, $data);
  110.  
  111. $this->text_fields = array(
  112. 'some_alias' => array(
  113. 'table' => 'table_of_field',
  114. 'field' => 'column_of_field',
  115. ),
  116. 'some_other_alias' => array(
  117. 'table' => 'table_of_other_field',
  118. 'field' => 'column_of_other_field',
  119. ),
  120. );
  121. }
  122.  
  123. // Here we will roll up all the fields and make sure
  124. // That they are in the query.
  125. function query() {
  126.  
  127. if (isset($this->value) && $this->value !== FALSE) {
  128.  
  129. $fields = array();
  130. foreach ($this->text_fields as $name => $info) {
  131. $table = $this->query->ensure_table($info['table'], $this->relationship);
  132. $fields[] = $info['field']; // check this to make sure you doing the right fields.
  133. }
  134.  
  135. // CONCAT all the fields, and see the length.
  136. $where = ' LENGTH( CONCAT_WS( "", '.implode(",",$fields).' ) ) != 0 ';
  137. $this->query->add_where_expression($this->options['group'], $where);
  138. }
  139. }
  140. }
  141.  
  142.  
  143.  
  144. /**
  145.  * @file sites/all/modules/MODULE/views/handlers/MODULE_handler_sort_has_xxxyyyzzz.inc
  146.  */
  147.  
  148. class MODULE_handler_sort_has_xxxyyyzzz extends views_handler_sort {
  149.  
  150. function init(&$view, &$data) {
  151.  
  152. // call parent init
  153. parent::init($view, $data);
  154.  
  155. $this->text_fields = array(
  156. 'some_alias' => array(
  157. 'table' => 'field_table',
  158. 'field' => 'field_value_column',
  159. ),
  160. );
  161. }
  162.  
  163. // add the fields to the query
  164. function query() {
  165. $fields = array();
  166. foreach ($this->text_fields as $name => $info) {
  167. $table = $this->query->ensure_table($info['table'], $this->relationship);
  168. $this->query->add_field($table, $info['field']);
  169. $fields[] = $info['field'];
  170. }
  171.  
  172. // concat all the fields and see the length.
  173. $this->query->add_orderby( NULL, "LENGTH( CONCAT_WS( "" , ' . implode( "," , $fields ) . ' ) ) >= 1",
  174. $this->options['order'], 'MODULE_has_xxxyyyzzz_sort' );
  175. }
  176. }

URL: http://www.itsgotto.be

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.