I have set up a view that shows users a set of nodes for an athletic season that runs September through August. Each node has a field called Start Date. I would like to filter this start date using the year of the season, i.e. 2012 for the 2012-2013 season. I've looked into contextual filters but I don't think this is going to work for me, as I don't see a way to provide a range (say 2012-09-01 through 2013-08-31). Will this require a custom module and if so, where can I get some concise instructions on hooking that custom module in with the specific view. Most of the documentation I read gives a quick code snippet with no specifics on where that snippet is supposed to go.
2 Answers
You can try setting a flag before node save.Set the flag if start date is Aug and End date is Sep.this flag field will be hidden field and you can pro-grammatically check the start date and end date before node save in hook_node_presave and in the same hook if you find condition to be true set the flag but remember to compare the date considering the same year option.Now On the views first filter the results on the basis of hidden flag field(as 1 or whatever you set) then add a start date expose filter with granularity as year only and make it expose to users.
Only reason I prefer to filter the results while creating the nodes was to simplify the filters in views, as it may be tough to filter out results in views untill you figure out how you can set interval as single and same year.
Is the "Start Date" field a date field? Or is it a text (or some other type of field)?
If it's an actual date field, you can configure this behavior through the UI.
Add the field as a filter in the Filters section. Then you should see an Is between
operator radio button. Select that and enter your range in the Value section. Screenshot:
You need to activate the date_views module to see the filters appearing in the Views module interface.
-
It is a date field. How do I allow the user to pass a year and configure that to return a date between September 1st of that year and August 31st of the following year? The year is not static as I have about 10 years worth of results to display.– RobbertMay 6, 2013 at 17:57
-
Oh. I misunderstood part of your question. You could check the "expose" checkbox and then use hook_form_alter to turn the resulting form into a select list. Or hide the exposed form altogether and use jQuery to fill the hidden form with specific values based on a select list. May 6, 2013 at 18:28