Annual, quarterly and monthly filters in Drupal 8 views (a workaround)


If you have a date field and you want to filter your exposed view with a date range, you can do this easily by using the extensive filters and impose a range between two dates.

So you can for the first quarter of 2019 provide a range between 01/01/2019 and 31/03/2019. One can do this for all quarters, months and years. The problem is that you have to do this manually for all quarters and years to come. There is no integrated system currently in Drupal 8 views wherein data can automatically be grouped to filter.

I have attempted to program this in views, but in the end I had to content myself with a (simple) workaround.

You use a computed field to convert the date to eg + year quarter.


With the following code:


$ $ date field = fields [ 'field_datum'] [0] [ 'value']; 
$ year = date ( 'Y', strtotime ($ date field)); 
$ month = date ( 'm', strtotime ($ date field)); 
$ value = $ year. "-". ceil ($ month / 3);

The first line retrieves the value of the date field. Adjust it with the name of your field. The second pulls out the year. The third line shows the month. Finally, the three months are divided by 3 and rounded to the whole number above it (= ceil in PHP). So you can obtain a quarter.

The computed field needs saving of the node and returns eg 2019-4 if the date is 01/10/2019.


In a view, it is now simple to use this field as a filter. 


Unfortunately you can not use 'views Selective Filters' on thid moment, in combination with computed fields.