If you are like me, you have avoided diving into coding for the black hole that is Views. Every time I started to look at the mass amount of documentation, I went blind. There is no shortage of documentation and examples. Besides looking for how other modules have implemented Views, the following documentation is available.
Even with all of this documentation, something didn't quite click. Sure, I could do the easy stuff, like expose a new table to views or manipulate an existing view, but when it came it creating a custom handler, I was lost. So today I'm sharing with you an example of a new custom handler. We had the need to create a modification of the taxonomy term filter that allowed for using multiple vocabularies. We also needed it to be in an exposed filter to be used in an administration screen. Let's start by first looking at what is different, then at the current filter.
- Change the selection element selection to a checkbox element.
- Keep all of the current functionality of the term selction
From the 10,000 foot view (no pun intented) that's it. Using the wonderful world of objects, we are going ot utilize the current handler, only overriding a few functions. Let's start at the beginning. The first thing you have to do is tell Views that your module is going to using the Views API. This is done using hook_views_api():
* Implementation of hook_views_api
*/
function MY_MODULE_views_api() {
return array(
'api' => 2,
);
}
* Implementation of hook_views_handlers() to register all of the basic handlers
* views uses.
*/
function mymodule_views_handlers() {
return array(
'info' => array(
'path' => drupal_get_path('module', 'MY_MODULE'),
),
'handlers' =>; array(
// The name of my handler
'my_module_handler_filter_term_node_tid' => array(
// The name of the handler we are extending.
'parent' => 'views_handler_filter_term_node_tid',
),
),
);
}
* Implementation of hook_views_data().
*/
function MY_MODULE_views_data() {
$data['term_node']['term_multiple'] = array(
'group' => t('Taxonomy'),
'title' => t('Term Name (multiple)'),
'help'=>; t('Term from multiple vocabularies'),
'real field' => 'tid',
'filter' => array(
'handler' => 'my_module_handler_filter_term_node_tid',
'hierarchy table' => 'term_hierarchy',
'numeric' => TRUE,
'skip base' => 'term_data',
'allow empty' => TRUE,
),
);
return $data;
}
extends views_handler_filter_term_node_tid{
}
// Get the parent's $form array.
parent::extra_options_form($form, $form_state);
// Change the radio to select
$form['vid']['#type'] = 'select';
$form['vid']['#multiple'] = TRUE;
// Remove what we don't want
unset($form['markup_start']);
unset($form['type']);
unset($form['hierarchy']);
unset($form['markup_end']);
return $form;
}
$vocabulary = array();
$options = array();
$default_values = array();
// Cycle through the vids entered in the extra options form
foreach ($this->options['vid'] as $vid) {
// Load the vocab.
$vocabulary = taxonomy_vocabulary_load($vid);
// Load the term tree.
$tree = taxonomy_get_tree($vid);
// Create the $options array what will be used in our select
if ($tree) {
$options[$vid .':0'] = '**'. $vocabulary->name .'**';
foreach ($tree as $term) {
$options[$vid .':'. $term->tid] =
str_repeat('-', $term->depth + 1) .' '.
$vocabulary->name .': '. $term->name;
if (in_array($term->tid, $this->options['value'])) {
$default_values[$vid .':'. $term->tid] =
$vid .':'. $term->tid;
}
}
}
}
// We are just using a select to keep things simple.
$form['value'] = array(
'#type' => 'select',
'#title' => t('Select the Terms'),
'#options' => $options,
'#default_value' => $default_values,
'#multiple' => TRUE,
'#size' => min(9, count($options)),
);
// If this is a exposed form then call the helper options form
if (empty($form_state['exposed'])) {
// Retain the helper option
$this->helper->options_form($form, $form_state);
}
}
if (!is_array($input)) {
$values[$input] = $input;
} else {
$values = $input;
}
$output = array();
// Just make sure no vocab items are selected
foreach ($values as $key => $value) {
list($vid, $tid) = explode(':', $value);
// We really only want to save the tid.
// The vid is used to make the select box
if ($tid) {
$output[$tid] = $tid;
}
}
return $output;
}
$values = $form_state['values']['options']['value'];
$form_state['values']['options']['value'] =
$this->format_tids($values);
}
if (empty($this->options['exposed'])) {
return;
}
$identifier = $this->options['expose']['identifier'];
$values = $form_state['values'][$identifier];
$tids = $this->format_tids($values);
if ($tids) {
$this->validated_exposed_input = $tids;
}
}
extends views_handler_filter_term_node_tid{
function option_definition() {
// Get the parents options
$options = parent::option_definition();
// Change the vid to an array.
$options['vid'] = array('default' => array(0));
// Remove what we don't need.
unset($options['type']);
return $options;
}
function extra_options_form(&$form, &$form_state) {
// Get the parent's $form array.
parent::extra_options_form($form, $form_state);
// Change the radio to select
$form['vid']['#type'] = 'select';
$form['vid']['#multiple'] = TRUE;
// Remove what we don't want
unset($form['markup_start']);
unset($form['type']);
unset($form['hierarchy']);
unset($form['markup_end']);
return $form;
}
function value_form(&$form, &$form_state) {
$vocabulary = array();
$options = array();
$default_values = array();
// Cycle through the vids entered in the extra options form
foreach ($this->options['vid'] as $vid) {
// Load the vocab.
$vocabulary = taxonomy_vocabulary_load($vid);
// Load the term tree.
$tree = taxonomy_get_tree($vid);
// Create the $options array what will be used in our select
if ($tree) {
$options[$vid .':0'] = '**'. $vocabulary->name .'**';
foreach ($tree as $term) {
$options[$vid .':'. $term->tid] =
str_repeat('-', $term->depth + 1) .' '.
$vocabulary->name .': '. $term->name;
if (in_array($term->tid, $this->options['value'])) {
$default_values[$vid .':'. $term->tid] =
$vid .':'. $term->tid;
}
}
}
}
// We are just using a select to keep things simple.
$form['value'] = array(
'#type' => 'select',
'#title' => t('Select the Terms'),
'#options' => $options,
'#default_value' => $default_values,
'#multiple' => TRUE,
'#size' => min(9, count($options)),
);
// If this is a exposed form then call the helper options form
if (empty($form_state['exposed'])) {
// Retain the helper option
$this->helper->options_form($form, $form_state);
}
}
function value_validate(&$form, &$form_state) {
$values = $form_state['values']['options']['value'];
$form_state['values']['options']['value'] =
$this->format_tids($values);
}
function exposed_validate(&$form, &$form_state) {
if (empty($this->options['exposed'])) {
return;
}
// Get the identifier vlaue
$identifier = $this->options['expose']['identifier'];
$values = $form_state['values'][$identifier];
$tids = $this->format_tids($values);
if ($tids) {
$this->validated_exposed_input = $tids;
}
}
function format_tids($input) {
if (!is_array($input)) {
$values[$input] = $input;
} else {
$values = $input;
}
$output = array();
// Just make sure no vocab items are selected
foreach ($values as $key => $value) {
list($vid, $tid) = explode(':', $value);
// We really only want to save the tid.
// The vid is used to make the select box
if ($tid) {
$output[$tid] = $tid;
}
}
return $output;
}
}




This is awesome, it would be WAY COOLER if the
>and&were literal, though. :)This article has helped me just so much!
Thanks, They have been updated. That was an issue with the migration to our new site!
Thanks a lot, very very good job man !
Just need some help from you regarding the following issue.
I have a created a view from a drupal database table using the data module with views and VBO.
The data module allows you to create a view from the adopted table.
But,
when I go to the views and edit the view added through the data module, i see no multi-checkbox list under selected operations.
I want to have all the operations that are available under the node view type to be available here, for my view created with data module. (views created with data module have a view type data table)
Any help greatly appreciated.
Please help me ...it's really urgent.
Thanks,
Nilesh Barve
Thank you. Thank you. Thank you.
This is a great article. Thanks!
This is a great article. Thanks!
nice walk thru! thank you.
Post new comment