Friday, April 29, 2011

Use SQL to Filter Drupal Views

I have a content type with a CCK integer field which takes a unix timestamp. I would like to create a view with a filter that takes the MySQL function "UNIX_TIMESTAMP":

alt text

If there is no way of doing this, how can I alter the SQL generated for the view?

From stackoverflow
  • I found a temporary solution which involves implementing the views_pre_execute hook inside the views module.

    I've documented it here in the comment labeled "Use SQL value for filter value on CCK integer field".

    I'm not very happy with this solution, though, because it seems a bit of a hack.

    Henrik Opel : Sidenote: The comment 'label' is actually a link to the comment itself, so you can link directly to it: http://drupal.org/node/409808#comment-1492672
  • You really ought to be using a date field, then you'd have "Now" in all of your filter options.

    If Views and CCK know your field is a date, they give you a lot more flexibility in how they filter and display the field.

0 comments:

Post a Comment