Custom filter handler in Drupal 7 and Views 3 for data stored as date and datetime

I just needed to expose a table with legacy data for Drupal 7 with Views 3. However, some of the date in the legacy mysql database was stored as DATE and DATETIME. However, Views only supports timestamps for their datehandling, and I needed to use a filter to filter away older posts in the legacy database.

What I needed to do was build a view with "filter >= -10 day". First I googled and came across this article: Creating a DATETIME View handler for Views. And that article was a good starting point.

I needed to write my own views filter handler. Views 3 in Drupal 7 relies on the autoload feature, so first I needed to declare my handler in the .info-file:

 files[] = 

I also needed to state in my_module.module which views api I am using:

As you can see, I have a reference to my own filter here:

 'filter' => array('handler' => 'my_module_handler_filter_datetime'),

Then I wrote the handler based on the previous mentioned article. There a some smaller changes, but everything should pretty much be self-explanatory. Have a look through the code.


IMHO this should either be built into Views, or it should be easier to achieve e.g. via the date module (and maybe it is). If you have a better solution, do not hesitate to comment.


Just FYI - hook_views_handlers() doesn't do anything in D7.

Yeap, it's not being invoked from the views.module code so it's not needed anymore.

Yup! now with adding the handler file in the files[] array at .info file in the module is enough :D

Couldn't you just wrap the field in UNIX_TIMESTAMP()?  Would make a much simpler views handler.

Could you supply an example?


Thank you for excellent tutorial.

Can you provide example of code which can use add_groupby metod correctly. 

I try by myself but nothing happend



Sorry, never tried adding a groupby.

thanks. i hade some similar proplem and now have a good starting point for solving my views handler problem.


Thanks mate for providing custom filter handler in Drupal 7 and Views 3 for data stored as date and datetime. It works great with my drupal site. Kudos.

Tilføj kommentar