Created
December 2, 2015 15:52
-
-
Save xymox12/92b6f6df1ccc57a01508 to your computer and use it in GitHub Desktop.
Drupal Field Handler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Drupal does it the 'offical' sql way which can be a pain when you just want to do it the mysql way | |
// This is the workaround | |
// Tags are set in the view display Query setting | |
function mymodule_query_alter(QueryAlterableInterface $query) { | |
$view_name = 'project_isotope'; | |
if ($query->hasTag('views_' . $view_name)) { | |
$query->groupBy('node.title'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implements hook_views_data(). | |
*/ | |
function mymodule_views_data() { | |
$data = array(); | |
$data['field_data_field_colour']['table']['group'] = t('Test colours'); | |
$data['field_data_field_colour']['table']['join'] = array( | |
// #global is a special flag which let's a table appear all the time. | |
); | |
$data['field_data_field_colour']['get_colours'] = array( | |
'title' => t('Test Handler'), | |
'help' => t('Custom Handler.'), | |
'field' => array( | |
'handler' => 'mymodule_handler_field_colour_rgb', | |
), | |
); | |
return $data; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Definition of mymodule_handler_handlername. | |
*/ | |
/** | |
* Description of what my handler does. | |
*/ | |
class mymodule_handler_field_colour_rgb extends views_handler_field { | |
/** | |
* Add some required fields needed on render(). | |
*/ | |
function construct() { | |
parent::construct(); | |
$this->additional_fields['entity_id'] = array( | |
'table' => 'field_data_field_colour', | |
'field' => 'entity_id', | |
); | |
} | |
function query() { | |
$this->ensure_my_table(); | |
$this->add_additional_fields(); | |
$this->field_alias = $this->table . '_' . $this->field; | |
$this->query->add_field(NULL, "GROUP_CONCAT($this->table.field_colour_rgb)", $this->field_alias, array('aggregate' => TRUE)); | |
$table = $this->table; // i.e. how the table is represented in drupal database schema, without prefixes. | |
$table_alias = 'field_data_field_colour'; | |
$left_table = 'field_data_field_category';// the table to which I want to join it. Mostly I assume you'll want to join it to the table of your handler | |
$left_field = 'field_category_target_id'; // a column in $left_table on which you wanna join | |
$field = 'entity_id'; // a column in $table on which you wanna join | |
// We create a new join object and provide it with information about the join we wanna make. | |
$extra = array(); | |
$extra[] = array( | |
'table' => $table_alias, | |
'field' => 'deleted', | |
'value' => 0, | |
'operator' => '=', | |
); | |
$type = 'LEFT'; | |
$join = new views_join(); | |
$join->construct($table, $left_table, $left_field, $field, $extra, $type); | |
// Alias under which you wanna see $table once it has been JOINed with all the other tables. | |
//add the join the the view query | |
$this->query->add_relationship($table_alias, $join, $this->relationship); | |
// $this->query->add_groupby('node_title'); | |
} | |
function render($values) { | |
return format_string('color-' . $values->{$this->field_data_field_colour.entity_id},$values->{$this->field_alias}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment