1

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

1 Answer 1

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.

5
  • 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, 2012 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, 2012 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, 2012 at 17:33
  • Do you confirm that what you propose is doable? Because I don't.
    – cherouvim
    Jul 24, 2012 at 14:55
  • @cherouvim I can confirm that it works, something is wrong with your code.
    – googletorp
    Jul 24, 2012 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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