Datepicker doesn't register relative dates

Created on 28 August 2024, 7 months ago

Problem/Motivation

In a view, I have a date filter with a default value of "today" to select content now and in the future. The filter is exposed to allow selection of different date ranges. This date filter is part of a set of filters to determine what content is displayed. The default date filter correctly filters the output, but the Better Exposed Filters Date Picker widget shows no default value, and if I change other filter parameters and submit the exposed filter form, no date filtering is done.

If I use a fixed default date (such as 2024-08-28) the Date Picker shows that date and everything works as expected.

Steps to reproduce

Create a view with an exposed date filter and a default value of "today" (offset) - or any other non-fixed date (+1 day, for example). Use the Date Picker widget. No default date appears on the exposed form. To see that the default value is not remembered on submitting the form, add another exposed filter; if you change the value of the other filter, the exposed date filter acts as though there is no default value.

Proposed resolution

The filter should accept any php-valid default date. Looking at DatePickers.php I see there's a function called convertOffsets, but that doesn't act on my filter because it seems to assume a wrapper; however, even when I modified it to convert the date for the default value to the format "2024-08-28", that conversion had no effect on the exposed filters form (or on the date picker itself). I'm not sure where the change should be made.

πŸ› Bug report
Status

Active

Version

7.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States peter pulsifer

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

Merge Requests

Comments & Activities

  • Issue created by @peter pulsifer
  • πŸ‡ΊπŸ‡ΈUnited States JCL324 Portland, OR

    I too ran into this issue. Tried various things to get DatePickers.php to show the initial date that was set in the filter, but no luck. What I ended up doing instead was to set the exposed date filter to a static value but actually left it blank - your mention that the picker does work without an offset date gave me this idea. Then I added a form alter to set the initial date and voila! Note it's important to have the view value initially empty so you can test for it on first load.

    function recurring_date_formatter_form_views_exposed_form_alter(&$form, FormStateInterface $form_state, $form_id) {
      // Set the default value for the date filter to the current date +2 days.
      if ($form['#id'] == 'views-exposed-form-event-promotion-page' ||
          $form['#id'] == 'views-exposed-form-event-promotion-page-1') {
        $default_date = new \DateTime('+2 days');
    
        // Set the default value only if the exposed filter is empty.
        $user_input = $form_state->getUserInput();
        $filter_value = $form_state->getUserInput()['field_dates_value'];
    
        if (empty($filter_value)) {
          $user_input['field_dates_value'] = $default_date->format('Y-m-d');
          $form_state->setUserInput($user_input);
        }
      }
    }
    

    If/when I get a chance, I think the solution for DatePickers.php might lie in the $user_input = $form_state->getUserInput(); pulling from $form_state.

  • πŸ‡ΊπŸ‡ΈUnited States peter pulsifer

    Hey, @jcl324 - that works. Very clever! Now we just need to figure out what's going on... Thanks!

  • πŸ‡ΊπŸ‡ΈUnited States liberatr Portland, OR

    I suppose it would also work to check for empty() if it is equal == to the default value. The setUserInput() seemed to be the key here.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Any potential solutions would be welcome

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Can someone try this?

  • πŸ‡¦πŸ‡ΊAustralia Agileware

    @smustgrave thanks for posting an MR.

    I've tried it out on with BEF 7.0.5 and it's working great on initial load, however after changing the filter it shows the default again after load – changed value is applied though.

    E.g. if I set the date to "today" on 10/02/2025, it correctly loads filtered from 10/02/2025 initially, however if I then change the filter to 01/01/2025 in the exposed form, it reloads the view from 01/01/2025 , but 10/02/2025 in the widget.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave
  • πŸ‡«πŸ‡·France MacSim

    Applied the MR on a 7.0.2 and a 7.0.5 with a 'now' value and '>' operator on a single date field.

    Before patching:
    On initial load, widget is empty and the offset condition is not applied on the view's results.
    If I change the date and submit the form, it's all good:
    the widget displays the date I picked and results are correctly filtered.

    After patching:
    On initial load, widget diplays the good date (today) but it's not applied on the view's results (I've got results from few days ago).
    If I change the date and submit the form, I've got the same behavior than @agileware #9:
    view's results are correctly filtered starting by the date I picked (tomorrow) but the widget displays the wrong date (today)

  • πŸ‡«πŸ‡·France MacSim

    Seems good for me now (only tried with a single field though).
    Review needed especially with a min/max exposed filter.

  • Pipeline finished with Success
    about 2 months ago
    Total: 266s
    #425799
  • Pipeline finished with Canceled
    about 2 months ago
    Total: 206s
    #425803
  • Pipeline finished with Success
    about 2 months ago
    Total: 8273s
    #425807
  • Pipeline finished with Success
    about 2 months ago
    Total: 371s
    #426247
  • Status changed to Needs review about 2 months ago
  • πŸ‡«πŸ‡·France vanessa.fayard

    Hi!

    I tested the patch on Drupal 10.4 & better_exposed_filters ^7.0

    It works on an single exposed filter (equal, less or greater)

    It doesn't work on a exposed filter β€œbetween” (min - max)

  • πŸ‡«πŸ‡·France MacSim

    I don't know why and if we have to open a new issue for that or not but when we set the field to use a "between" operator with min/max default values,
    $form_state->getUserInput() doesn't have values for this field on initial load (whereas it does after the form submission, and it also does if we set the field to use a '>', '<', '=' operator with single default value)

  • Pipeline finished with Success
    about 1 month ago
    Total: 240s
    #444191
  • πŸ‡«πŸ‡·France MacSim

    Seems good with single default value (equal / less / greater) and between min/max default values.

    Needs review with and without default values on each those use cases.

    Gotta also write tests.

  • Pipeline finished with Success
    about 1 month ago
    Total: 269s
    #444192
  • Pipeline finished with Failed
    30 days ago
    Total: 274s
    #444608
  • Pipeline finished with Failed
    30 days ago
    Total: 256s
    #444636
  • Pipeline finished with Success
    30 days ago
    Total: 267s
    #444643
  • πŸ‡«πŸ‡·France MacSim

    Tests are now fine with equal / less / greater and between operators on initial load.

    It might be nice to also add tests checking that everything is ok after a form submission with updated values.

  • Pipeline finished with Failed
    29 days ago
    Total: 264s
    #444704
  • Pipeline finished with Failed
    29 days ago
    Total: 255s
    #444720
  • Pipeline finished with Failed
    29 days ago
    Total: 255s
    #444725
  • Pipeline finished with Failed
    29 days ago
    Total: 265s
    #444737
  • πŸ‡«πŸ‡·France MacSim
  • Pipeline finished with Failed
    29 days ago
    Total: 518s
    #444887
  • Pipeline finished with Success
    29 days ago
    Total: 252s
    #444896
  • Pipeline finished with Success
    29 days ago
    Total: 253s
    #444898
Production build 0.71.5 2024