Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have a content type which has the following fields:

  • month: List (integer) [jan, feb, ..., dec] (non mandatory)
  • year: Date (only collects year - mandatory)

I can normally create two exposed views filters, one for the month and one for the year.

What I'd like to do though is have a single exposed filter which, internally, would operate on the two fields above at once.

For example if the operator is "Is Between" and the user selects Oct 2011 to Feb 2012 I'd like the filter to return only nodes which have:

  • 2011, Oct
  • 2011, Nov
  • 2011, Dec
  • 2012, NULL month
  • 2012, Jan
  • 2012, Feb

Note that I've used two fields (month and year) instead of one so I can have the possibility of a monthless node.

What approach should I take? I've thought of the following:

  • use a third field which, on hook_node_presave will store the calculated (filter specific) date on the node. I wouldn't like this field to be visible on the edit node screen though
  • somehow (using a hook) calculate and rewrite the view filtering query. My problem here is how to display an exposed filter for a field which does not exist
share|improve this question
add comment

1 Answer 1

The most obvious solution to me would be to get the month data into the date field, since you are treating the date to be 1/1 when no month is given, this would fit just fine; there is no reason to create a new field for this.

You should be able to do this in a form submission handler for the node edit form.

share|improve this answer
    
The reason why I need two fields is to have the ability for a NULL month. Then the presentation should display "2012" instead of "2012 Jan". On sorting and filtering though I'd treat NULL as Jan. –  cherouvim Jul 14 '12 at 8:09
    
@cherouvim You don't understand - since you only display the year in the date field, you can still store month and year. –  googletorp Jul 14 '12 at 10:23
    
Are you sure that this is possible? Can a date field which only collects a year store a month and a day? I tried within a hook_node_presave to do $node->field_year['und'][0]['value'] = "2019-02-03 00:00:00"; and when I got it back the date was 2019-01-01 00:00:00 which means that the month and day values where lost. –  cherouvim Jul 17 '12 at 17:33
    
Do you confirm that what you propose is doable? Because I don't. –  cherouvim Jul 24 '12 at 14:55
    
@cherouvim I can confirm that it works, something is wrong with your code. –  googletorp Jul 24 '12 at 15:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.