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?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

egfrith created an issue. See original summary.

mandclu’s picture

This 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.

egfrith’s picture

That sounds good. I'll hope to see if I can do something later this week.

mandclu’s picture

OK 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.

egfrith’s picture

Sorry 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.

mandclu’s picture

Views 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.

egfrith’s picture

I'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...

egfrith’s picture

Hmm... 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.

mandclu’s picture

I 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,

mandclu’s picture

egfrith’s picture

Thanks 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!)

MrPaulDriver’s picture

May 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.

egfrith’s picture

Another 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.

egfrith’s picture

I'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:

[Sun Jul 28 17:46:51.790295 2019] [:error] [pid 10835] [client 77.100.0.240:43308] Uncaught PHP Exception Drupal\\Component\\Plugin\\Exception\\PluginNotFoundException: "The "date" plugin does not exist. Valid plugin IDs for Drupal\\views\\Plugin\\ViewsPluginManager are: entity_browser_widget_context, node, taxonomy_tid, user, current_user, raw, query_parameter, fixed" at /var/web/drupal/8.7/web/core/lib/Drupal/Component/Plugin/Discovery/DiscoveryTrait.php line 53, referer: http://www.venue40.org.uk/admin/structure/views/view/what_s_on/edit/day_by_day_block
egfrith’s picture

The patch at #125 here https://www.drupal.org/project/drupal/issues/2325899 fixes the above problem.

mandclu’s picture

Awesome! Could you post a patch with your changes?

amykhailova’s picture

Is 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?

mandclu’s picture

Some 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 :)

mandclu’s picture

Version: 8.x-1.0-beta4 » 3.6.x-dev
Assigned: Unassigned » mandclu

Looking 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

mandclu’s picture

Assigned: mandclu » Unassigned
Status: Active » Needs review
FileSize
9.46 KB

Here's a patch that should add the option to set granularity in the views filter.

  • mandclu committed 874067f on 3.6.x
    Issue #3055994 by mandclu: Views Date Filter Datetime Granularity Option
    
mandclu’s picture

Status: Needs review » Fixed

Since 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.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

tobiasb’s picture

granularity is always null in opSimple/opBetween.

Because it is stored in $this->options['value']['granularity']

fallenturtle’s picture

Is 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.