1

We have a dev 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:

https://domain.com/events/042018

returned all events with month 04 and year 2018.

Now on views 8.5.3 this patch is no longer applicable and seems to be integrated for Created Date and Updated Date. But I don't need these date fields. I am trying to access a content date field that I made.

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

Thanks so much!

Example of View: Show all events. Contextually Filter by Date. But instead of an exact date, only grab month and year parts, i.e. show all events for July 2018 /events/201807 views content date month year

  • 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 '18 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. – Matt Campbell May 8 '18 at 18:31
  • Have you tried this module, drupal.org/project/contextual_range_filter – Jdrupal May 11 '18 at 21:45
2
  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

  • 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. – Matt Campbell May 15 '18 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 '18 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. – Matt Campbell May 15 '18 at 19:45
  • 1
    @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 '18 at 19:52
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.

  • That patch is not tested in 8.5.x, especially the update path. – mpdonadio May 15 '18 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. – Matt Campbell May 15 '18 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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