Skip to content

Instantly share code, notes, and snippets.

@juampynr
Last active Aug 10, 2021
Embed
What would you like to do?
Drupal 7 Views 3 custom filter handler
dependencies[] = ctools
; Views Handlers
files[] = views/mymodule_handler_filter_myfiltername.inc
<?php
/**
* Implements hook_views_api().
*/
function mymodule_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'mymodule') . '/views',
);
}
<?php
// This file must be at mymodule/views directory.
/**
* @file
* Views definitions for mymodule module.
*/
/**
* Implements hook_views_data_alter().
*/
function mymodule_views_data_alter(&$name) {
$data['node']['myfiltername'] = array(
'group' => 'Content',
'title' => t('Title of the filter'),
'title short' => t('Title of the filter'),
'help' => t('Describe here what the filter does.'),
// The following must be a column in the table that the filter is attached to.
// The value of this column will be available in the filter.
'real field' => 'created',
'filter' => array(
'handler' => 'mymodule_handler_filter_myfiltername',
),
);
}
<?php
// This file must be at mymodule/views directory.
/**
* @file
* Definition of mymodule_handler_filter_myfiltername.
*/
/**
* One line description of the filter.
*
* This sample filter extends from the basic views_handler_filter. However, you can extend
* from all of the other filter handlers used for dates, strings, lists, etc. Search for
* *_handler_filter_* files to find them.
*/
class mymodule_handler_filter_myfiltername extends views_handler_filter {
/**
* The form field to show when this filter is exposed.
*/
function value_form(&$form, &$form_state) {
$form['value'] = array(
'#type' => 'select',
'#title' => 'Month',
'#options' => array(
'1' => 'January',
'2' => 'February',
'3' => 'March',
'4' => 'April',
'5' => 'May',
'6' => 'June',
'7' => 'July',
'8' => 'August',
'9' => 'September',
'10' => 'October',
'11' => 'November',
'12' => 'December',
),
'#default_value' => $this->value,
);
}
/**
* Runs the query for this filter.
*
* @see $this->query->add_where() for other types of conditions that you may want to write.
*/
public function query() {
// $this->value is not empty when the above form has been filled out.
if (empty($this->value)) {
return;
}
$this->ensure_my_table();
// Field will contain the value of the 'real field' setting defined at hook_views_data_alter().
$field = "$this->table_alias.$this->real_field";
// Adds a condition to filter for content created on a given month.
$this->query->add_where_expression($this->options['group'], 'MONTH(FROM_UNIXTIME(' . $field . ')) = ' . $this->value[0]);
}
}
@lathanbritz
Copy link

lathanbritz commented Feb 8, 2019

line 13 of mymodule.views.inc
should be function mymodule_views_data_alter(&$data) {

@nezam4u
Copy link

nezam4u commented Apr 19, 2019

It is very help full to create handler, I need some help to create handler to trim white space from exposed text filter. Can you please explain

@nezam4u
Copy link

nezam4u commented Oct 8, 2019

It is very help full to create handler, I need some help to create handler to trim white space from exposed text filter. Can you please explain
My problem is resolved, I got the way to create the handler

Thanks

@juampynr
Copy link
Author

juampynr commented Oct 8, 2019

Glad to see that you figured out, @nezam4u!

@marklabrecque
Copy link

marklabrecque commented Aug 10, 2021

I believe the &$name in the hook_views_data_alter implementation needs to be &$data ?

@marklabrecque
Copy link

marklabrecque commented Aug 10, 2021

Thanks for this code! It has been quite helpful to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment