Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
This module looks like it could be very helpful for me. One other datetime thing that I'm missing is the ability to set the granularity of views filters so that, for example, I can select all entities starting on a particular day.
There is a patch for this against core, but it's not going to make it until at least 8.8:
#2868014: [PP-1] Views Date Filter Datetime Granularity Option
Would it be possible to add this functionality to this module?
Comment | File | Size | Author |
---|---|---|---|
#21 | smart_date-views_filter_granularity-3055994-21.patch | 9.46 KB | mandclu |
Comments
Comment #2
mandclu CreditAttribution: mandclu commentedThis sounds like a great idea, but I don't know that I'll have much time to look at it this week. If you want to take a stab at adapting the code from that patch, I would gladly review a patch here.
Comment #3
egfrith CreditAttribution: egfrith as a volunteer and commentedThat sounds good. I'll hope to see if I can do something later this week.
Comment #4
mandclu CreditAttribution: mandclu commentedOK I finally had some more time to look at this. Can you help me understand the use case here? I feel like you could accomplish the same thing with a pair of filters, e.g. start > 2019-08-31 12:00am and start < 2019-09-01 12:00am. I can see that it's less elegant for constructing the view, but that patch adds a lot of extra code.
Comment #5
egfrith CreditAttribution: egfrith as a volunteer and commentedSorry for the delay - I had missed the notification of your last comment.
The use case is as follows: I'm doing a website (on a voluntary basis) for a venue in the Edinburgh Fringe. On each of 21 days, we've various events, and we'd like to be able to produce a listing of the events on any one day. By default, users would be shown the listing for the current day on the "What's on" page, but there would also be links to get to the listings for tomorrow, yesterday or any of the other days. A good number of the events are repeats: for example a show has a run of 12 performances, though the performances might be at different times on different days. We need to indicate either the duration or end time of events.
I was thinking of implementing this by defining a "Show" content type with a "Times" field that uses the datetime or smart date range field, with an unlimited number of values. Then I would construct a view that would display events on a given date. This is the point I've got stuck at; I'm not even sure that views can deal with more than one value of any field.
Comment #6
mandclu CreditAttribution: mandclu commentedViews should be able to deal with multiple time values. You may end up with duplicates, but if you're building out a schedule that might be preferable anyway.
You're welcome to test it yourself anyway, but I suspect that if you build out that schedule view with core's datetime field, the queries will be really slow, since the values are stored as strings, so MySQL has to do a bunch of conversions on every row in order to make the required comparisons.
Having this granularity function would be amazing. The nice thing about having that patch as reference is that you have a decent sense of where the code would need to go, e.g. /src/Plugin/views/filter/SmartDate.php
Core also provides some handy methods for converting values between Datetime objects and timestamps. So, for example, if you want to match values for a particular day, get the timestamp for the start and end of the day, and add conditions to the query to get everything in between.
At the moment my top priority is getting the display configuration translatable, but if you're able to working on this I'm happy to provide any guidance or assistance I can.
Comment #7
egfrith CreditAttribution: egfrith as a volunteer and commentedI've looked at this a bit more. Another way of getting shows on a particular date might be to used contextual filters, which do allow years, months and days to be specified. I'd then have URLs of the form what_s_on/2019/08/23 , and would then have to somehow have a pager or date picker to get to the URL. I've tried using the contextual filter with smart_date, and it doesn't seem to work, though it does work for the standard datetime field.
I'm thinking that implementing the granularity filter might be the best way forward - I suppose the first thing is to get a version of core/modules/datetime/src/Plugin/views/filter/Date.php working in smart_date...
Comment #8
egfrith CreditAttribution: egfrith as a volunteer and commentedHmm... it's dawned on me that the multiple times are going to be a problem. Even though I'll be able to filter all of the events happening on a particular day, *all* of the times will be displayed, which is not ideal. It might be better to have a separate "Event" entity type, which has a reference to a "Show". The granularity option will still be useful, but I'll probably need to work out the entity structure before looking at that.
Comment #9
mandclu CreditAttribution: mandclu commentedI had a hunch that the ways Views handles multiple values would do the heavy lifting for you, so I did a quick test. There is indeed an option on the field (if multiple values are possible) to give you control over how to handle multiple values:
If you uncheck that, it will output a separate row for each value, which sounds like is what you need. For example:
You can then add your filters to show only the values within the range you need,
Comment #10
mandclu CreditAttribution: mandclu commentedComment #11
egfrith CreditAttribution: egfrith as a volunteer and commentedThanks very much for that info - I had missed that option as I'd being viewing content rather than fields. However, the field view is probably the right one for my listing anyway.
(It came just in time too - I worked out how to do the relational thing, but that does involve creating one entity per occurrence, which feels like overkill!)
Comment #12
MrPaulDriver CreditAttribution: MrPaulDriver commentedMay I add that I recently had a need for a views filter for day of the week.
What's happening on Monday
What's happening on Tuesday
etc
Especially useful where repeating dates are involved.
Comment #13
egfrith CreditAttribution: egfrith as a volunteer and commentedAnother way of achieving a per day listing of events might be to use contextual filters. At the moment it almost works: e.g. http://www.venue40.org.uk/programme2/2019-08-05 shows just the events on that day. However, the "Display a summary" default option doesn't group together the events on the same day: http://www.venue40.org.uk/programme2
I've been wondering whether the code in datetime_type_field_views_data_helper() in datetime.views.inc and the associated plugins (datetime/src/Plugin/views/argument/*) could be adapted for smart_date.
Comment #14
egfrith CreditAttribution: egfrith as a volunteer and commentedI'm almost there. I can used the grouped list with the rendered date used to group to get the view at:
http://www.venue40.org.uk/programme/day-by-day
I can use a contextual filter on the smart_data field to get a per-day view:
http://www.venue40.org.uk/programme/day-by-day/2019-08-15
(In both cases the list of links is hard-coded, but that's OK.)
The one thing I don't seem able to do is to give the current date as a default to the contextual filter. I get this error message when trying to do so:
Comment #15
egfrith CreditAttribution: egfrith as a volunteer and commentedThe patch at #125 here https://www.drupal.org/project/drupal/issues/2325899 fixes the above problem.
Comment #16
mandclu CreditAttribution: mandclu at Northern Commerce commentedAwesome! Could you post a patch with your changes?
Comment #17
mandclu CreditAttribution: mandclu at Northern Commerce commentedComment #18
amykhailova CreditAttribution: amykhailova at Kalamuna commentedIs it being worked on? We are looking for an exposed month filter and year filter. I am not sure if there is a way to achieve this with smart date at the moment?
Comment #19
mandclu CreditAttribution: mandclu commentedSome have reported success with the patch posted at #2868014: [PP-1] Views Date Filter Datetime Granularity Option. I will say that the time I tried it, it seemed to work. On the other hand, that was a while ago.
My plan is effectively to apply the latest patch from that thread, and then adapt the patched filter to Smart Date, potentially adapting to handle things like timezones if need be. Patches are always welcome, even more so on this issue :)
Comment #20
mandclu CreditAttribution: mandclu as a volunteer and at Acquia commentedLooking into this, the linked patch only updates the logic for datetime values (core datetime and datetime range fields, stored as strings) so implementing something custom for Smart Date
Comment #21
mandclu CreditAttribution: mandclu as a volunteer and at Acquia commentedHere's a patch that should add the option to set granularity in the views filter.
Comment #23
mandclu CreditAttribution: mandclu as a volunteer and at Acquia commentedSince the 3.6.x branch is going to be in beta for a little while anyway, decided to go ahead and merge this in. Marking as fixed, but feel free to reopen if additional work on this is needed.
Comment #25
tobiasbgranularity is always null in opSimple/opBetween.
Because it is stored in
$this->options['value']['granularity']
Comment #26
fallenturtle CreditAttribution: fallenturtle commentedIs this fix only for the regular filters? I can't find any granularity options under contextual filters. I'm trying to create a calendar that if given no arguments will show the current events that start or end in the current month and if provided in the URL with a month and year (ex: 2023-05) will show events that start or end in that month.