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[] = my_module_handler_filter_datetime.inc 

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.

Caveat

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.

Kommentarer

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?

Hello

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 http://drupal.org/node/1578808

 

TIA

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.

 

Have you ever considered creating an e-book or guest authoring on other blogs?
I have a blog based on the same ideas you discuss and would love to have you
share some stories/information. I know my subscribers would enjoy your work.
If you are even remotely interested, feel free to send me an email.

* A sitemap to navigate your website is helpful for visitors to access main pages.
In addition, the observing surgeons could transmit their comments to the operating surgeon, who could read them on the Google Glass monitor.
What Googlebot is looking to see in the Free Local Google Advertising Listings
is quite another.

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.

Hello, I am new to drupal and want to add date filter for view. I have created a custom table for custom module in which I have DATETIME field for submit date field. 

Please can you explain the steps on how your code can be used by me.

Thanks,

Anu

Tilføj kommentar