Views CCK filter + today value

shushu - July 19, 2007 - 08:44

Hi All,
I have a CCK type with field_day and field_month, which have nodes per day of the year.
I would like to have a view that shows only today's nodes, according to day of the month and month of year.

Unfortunately I couldn't find a way to make this. I thought viewsphpfilter.module will help, but coding it is more then I wanted.
Getting into Views API is something that I try not to do in this case...

Any idea ?

You could use View arguments

matt_harrold - July 19, 2007 - 10:54

You could use View arguments to pass the month and day into the view.

Then your problem becomes generating a link that supplies the right day and month:

<?php
 
echo "<a href=\"/yourview?field_day=".date("j")."&amp;field_month=".date("n")."\">Today's Nodes</a>";
?>

This is ONE way to work around your problem without extra modules.

Anyone know a better solution?

I Love Drupal's hooks - Dynamic views is simple !

shushu - July 26, 2007 - 20:56

I love Drupal's hooks !
I've found out View module implemented a hook named hook_views_query_substitutions, which allows me to make changes in the SQL query before it is being executed.
So the solution was to select some code word and preg_replace it. Amazingly easy, although with a little coding, but very elegant.

<?php
function hook_views_query_substitutions($view) {

    if (
$view->name == "my_view_name") {
       
$today = getdate();
        return array(
"UPPER('TODAY')" => $today[mday], "MONTH" => $today[month]);;
    }
}
?>

Thanks for the tip!

matt_harrold - July 27, 2007 - 08:05

I am grabbing this snippet and giving it a go. Much more Drupal-like than my suggested hack.

Where to put it?

timothymedia - June 24, 2008 - 18:09

So, where do you put all this code? I have the same problem you had.

 
 

Drupal is a registered trademark of Dries Buytaert.