2

We have a site finally patched all the way up to 8.5.3. Our old site (8.4.0) was using a Views patch (just for reference) that allowed for a content date field to be contextually filtered by month and year using a URL like https://example.com/events/042018, which returned all events for April, 2018.

This patch is no longer applicable to the Views module 8.5.3. It seems to be integrated for Created Date and Updated Date, but I don't need these fields. I am trying to access a content date field that I made.

Does anyone know how to do this?
Can you help me get started on creating a custom views contextual filter handler that can access this custom date field?

Example of view: Show all events filtering by date, but instead of an exact date, only grab month and year parts.

views content date month year

2
  • 1
    views patch is probably not related to your issue. If you go to /admin/structure/views/ then find the view you are talking about, then click on edit. Take a screenshot of the view, or just the contextual filter area and add it here would be helpful. We need to know your view configuration
    – awm
    May 8, 2018 at 16:06
  • True was just using the patch as reference to the functionality we were using. I will add a screen shot of my view, but this is a very basic setup. Content, fields, one of the fields is a date, contextually filter view output by the month and year of this date field. May 8, 2018 at 18:31

2 Answers 2

4
  1. Remove your date contextual filter, by clicking remove.
  2. click Add a contextual filter, and search for the date field and select the "Year+month" granularity. Then click add.

enter image description here

once you are done you should have your contextual filter look like this:

enter image description here

Now you can go events/201807 and it would give you the same results.

update: this granularity is available for all date fields under the contextual filter option (node.field_somedatefield (year_month) provided the field has been added normally using content types manage fields section:

enter image description here

5
  • As was stated in the origin post and comments, this granularity is needed for a Content: Date field (a regular date field on the content type). NOT the Updated or Created date of the content, which is clearly supported. Thank you though. May 15, 2018 at 19:13
  • This is available for all date fields provided you added them in the content type admin page as date field see updated section of my answer.
    – awm
    May 15, 2018 at 19:30
  • Sorry you are correct, but only datetime fields, not daterange fields. Which is still in dev. I should have been more specific about daterange field types in my original post. May 15, 2018 at 19:45
  • 2
    @MattCampbell I see but in your question you did not even mention range fields. My answer gives you exactly what you asked for.
    – awm
    May 15, 2018 at 19:52
  • Worked for me today with a date field.
    – simesy
    Nov 6, 2019 at 21:39
1

Views 8 is still developing the use of granular date filters for content daterange fields. This is needed for making month calendars, year calendars, etc.

SOLUTION

For Drupal 8.5.x Views, anyone trying to select content by MONTH & YEAR on a daterange field, install the following patch until it's integrated with core:

2786577-270.patch

https://www.drupal.org/project/drupal/issues/2786577?page=1

Comment #318. For earlier versions of Drupal 8, simply scroll back in that thread and look for the patch applicable to your version.

After installing the patch:

  1. Edit your view
  2. Under Advanced > Contextual Filters > Add
  3. Filter by "date"
  4. You will now see all of your content date fields with date parts available as filters. Choose year_month or any other date part you want
  5. You can now filter or contextually filter by date part
  6. Important: Keep your eye on core updates as newer versions of this patch may need to be applied again

contextual filter month year views drupal 8 contextual filter month year views drupal 8 calendar

Displays all content where the custom date field is anytime in April 2018. e.g. Display all events on calendar in April.

2
  • That patch is not tested in 8.5.x, especially the update path.
    – mpdonadio
    May 15, 2018 at 20:09
  • True, but tested on 8.6 dev and confirmed on 8.5 by multiple including me. No update path, but still this patch is the solution to this problem. May 15, 2018 at 21:12

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.