Skip to content

Instantly share code, notes, and snippets.

@juampynr
Last active August 31, 2023 05:48
Show Gist options
  • Save juampynr/27df316e7dab89f02a6d18bbff252bc2 to your computer and use it in GitHub Desktop.
Save juampynr/27df316e7dab89f02a6d18bbff252bc2 to your computer and use it in GitHub Desktop.
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]);
}
}
@juampynr
Copy link
Author

juampynr commented Oct 8, 2019

Glad to see that you figured out, @nezam4u!

@marklabrecque
Copy link

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

@marklabrecque
Copy link

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