Hello,

Context: A Page View that filters events by date, depending on the date they were assigned with a CCK Date Field. Like so: /events/%year/%month/%day
The Problem: I Haven't been able to use the date filter like this. Is it possible, or should I look for another route, and if so, which do you think it's best?

Thanks for your help! :-)

Comments

chromix’s picture

I've run into this as well. The most obvious way to do this would be calculated field that just stores the year, month, and day value, but this seems a little silly considering you can do this kind of filtering on created and updated dates in Drupal but not on date fields. Did you come up with another solution?

devuo’s picture

Hello Chromix, sorry for the late answer, but the solution I've came up with was by overriding the specified View Query "Where" parameters through hook_views_query_alter() -> http://drupalcontrib.org/api/drupal/contributions--views--docs--docs.php...

Basically like this:


function YOUR_MODULE_views_query_alter(&$view, &$query) {

	if($view->name == "YOUR_VIEW_NAME" && $view->current_display == 'YOUR_VIEW_DISPLAY_ID')
	{
		
		// Sets the default date as a Fallback (Year and Month)
		$year_month = date("Y-m");		
	
		// Checks if there are arguments present
		if($view->args)
		{
                        // Year
			$arg1 = check_plain($view->args[0]);
                        // Checks if arg1 is a number and if it's a valid year, otherwise get the current year
			$year = is_numeric($arg1) && checkdate(1, 1, $arg1) ? $arg1 : date("Y");
			
                        // Month
			$arg2 =check_plain($view->args[1]); // Month
                        // Checks if arg2 is a number and if it's a valid month, otherwise get the current month
			$month = is_numeric($arg2) && checkdate($arg2, 1, $year) ? $arg2 : date("m");
			
                        // Overrides the previously set Year and Month
			$year_month = date("Y-m", mktime(0,0,0,$month,1,$year));
		}
		
		// Redefine the WHERE query
		$query->where['date']['clauses'][0] = "DATE_FORMAT(CONVERT_TZ(STR_TO_DATE(node_data_field_YOUR_DATE_FIELD_NAME.field_YOUR_DATE_FIELD_NAME_value, '%Y-%m-%%dT%T'), 'UTC', 'Europe/Paris'), '%Y-%m') <= '$year_month' AND DATE_FORMAT(CONVERT_TZ(STR_TO_DATE(node_data_field_YOUR_DATE_FIELD_NAME.field_field_YOUR_DATE_FIELD_NAME_value, '%Y-%m-%%dT%T'), 'UTC', 'Europe/Paris'), '%Y-%m') >= '$year_month'";
	}
}

And that's basically it :)

jamesfk’s picture

You can do this in views 2, by choosing node date as the argument, then you get to choose a cck date after that for some odd reason....

jkingsnorth’s picture

Issue summary: View changes
Status: Active » Closed (fixed)

Solution offered.

(Closing old issue).