New views filter operators for date range containing, starting or ending at giving value

Created on 22 December 2023, over 1 year ago
Updated 8 May 2024, 11 months ago

Problem/Motivation

Use case: a hotel booking system, where people usually stay for multiple days, two views are often required. The first view provides a list of all guests being present today. The second view provides a list, who is either arriving or leaving today. In both cases, "today" is the default value, but the day needs to be selectable. That means we need an exposed filter.

The first use case needs this filter: startdate <= selecteddate AND enddate >= selecteddate

The second use case needs this filter: startdate == selecteddate OR enddate == selecteddate

In both cases, the exposed filter form should only ask for one date input, but the SQL query needs to add multiple conditions to the SQL query.

Note: without the need for an exposed filter, the lists can already be produced with existing filter plugins. It requires multiple filters, which is fine. But with the filter being exposed, we can't operate with multiple input values, as they should always be the same.

Proposed resolution

Provide additional operators for Smart Date's date filter to allow for additional use cases.

Feature request
Status

Fixed

Version

4.1

Component

Code

Created by

🇩🇪Germany jurgenhaas Gottmadingen

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @jurgenhaas
  • 🇩🇪Germany jurgenhaas Gottmadingen

    The MR contains the first use case. The views data for the daterange field needs to be defined, of course:

        $data['apbs_allocation']['daterange']['filter'] = [
          'field_name' => 'daterange_contains',
          'id' => 'date_range_contains',
          'title' => 'Date range contains',
        ];
    

    I'll work on the second case later, but wanted to leave the WIP for review.

  • Status changed to Needs review over 1 year ago
  • 🇩🇪Germany jurgenhaas Gottmadingen

    The second use case is now implemented as well. In addition to that, I've also implemented min and max values as well, so that it is similar to the parent date filter. Ready for review now.

  • 🇩🇪Germany jurgenhaas Gottmadingen

    An additional thought: with the 2 filter plugins, the existing date filter and the new one from the MR, we now have quite a bit of redundant code, which could be refactored. That could make maintenance a lot easier eventually. Please let me know if you're up for the idea, I could then come up with a proposal.

  • 🇨🇦Canada mandclu

    @jurgenhaas I can see what you mean about the duplication. I'm guessing you have in mind something like a base class that both filters could extend.

    Whatever it is, I'm open to suggestions on how to make it more maintainable.

  • 🇩🇪Germany jurgenhaas Gottmadingen

    @mandclu we don't even need a separate base class since the Date plugin is already the base class for the new DateRangeContains plugin.

    I've now updated the MR by introducing the \Drupal\smart_date\Plugin\views\filter\Date::getMinAndMax callback to do all the heavy lifting for the op* callbacks that prepare the values for the query conditions.

  • 🇨🇦Canada mandclu

    @jurgenhaas overall this looks great. I did raise a couple of questions. If we can get those resolved I believe this should be ready to merge in.

  • 🇩🇪Germany jurgenhaas Gottmadingen

    Thanks @mandclu for your review. I've responded to your comments and I believe the threads could be resolved, and did so. Please re-open if you believe otherwise.

  • 🇨🇦Canada mandclu

    @jurgenhaas it seems I needed more coffee before my review. I do have one additional comment, please let me know your thoughts.

  • 🇩🇪Germany jurgenhaas Gottmadingen

    @mandclu good catch. I also found 2 other left-overs and fixed them as well. Should be good to go now, except for one question I left in that last thread in the MR.

  • 🇩🇪Germany jurgenhaas Gottmadingen

    @mandclu it looks like there is only one thread left open, any chance we could get this addressed at some point?

  • Pipeline finished with Success
    12 months ago
    Total: 282s
    #151908
  • Pipeline finished with Success
    12 months ago
    Total: 288s
    #151909
  • Pipeline finished with Success
    12 months ago
    Total: 286s
    #151910
  • Pipeline finished with Success
    12 months ago
    Total: 288s
    #151911
  • 🇨🇦Canada mandclu

    Thanks for the reminder! Let me get my head around this again and I'm sure we can get this into the next (hopefully stable) release.

  • 🇨🇦Canada mandclu

    I added a comment to the existing thread, and also added one new comment. Very close now.

  • 🇩🇪Germany jurgenhaas Gottmadingen

    Thanks @mandclu, I've closed the older thread and commented to the new one but left it open in case I got it wrong.

  • 🇨🇦Canada mandclu

    All the threads are resolved, and I cleaned up a couple of other things I found. I'm going to do some testing and then merge this in as long as everything works as expected.

  • Pipeline finished with Success
    12 months ago
    Total: 154s
    #153458
  • Pipeline finished with Skipped
    12 months ago
    #155230
  • Status changed to Fixed 12 months ago
  • 🇨🇦Canada mandclu

    Merged in. Thanks for all your work on this @jurgenhaas!

  • 🇩🇪Germany jurgenhaas Gottmadingen

    This is exciting, thanks @mandclu for helping me and even considering this enhancement.

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

Production build 0.71.5 2024