The integer 86401 should not be used to represent an "empty" time element

Created on 26 July 2024, about 1 year ago

Problem/Motivation

After upgrading from 2.1.0 to 2.1.1 and 2.1.2 I discovered that using a time element in a remote #states value='' condition no longer works.
For example:

  $form['field_test_string']['widget'][0]['value']['#states'] = [
    'invisible' => [
      ':input[name="field_test_time[0][value]"]' => ['value' => ''],
    ],
  ];

On further investigation, I found the `TimeElement` now uses `86401` to represent an empty value, which was added in Support empty time range values Fixed . Therefore the value of the element is not an empty string anymore, so that makes sense why the #states code doesn't work.

I would argue using `86401` to represent empty is not the correct approach to this problem. It is an invalid value for this input type, leading to a browser console warning and could cause unexpected "gotchas".

I understand its "invalidness" makes it work, but I think there might be a better solution. I also understand and respect the original author of the solution knew it wasn't the best approach.

Proposed resolution

As an alternative solution couldn't we:

  1. Change the `TimeRangeType` schema to allow `to` to be `NULL` (this would also require an update hook)
  2. Implement a `\Drupal\time_field\Plugin\Field\FieldWidget\TimeRangeWidget::massageFormValues()` and set the empty `to` value to NULL to ensure it passes validation.
  3. Modify TimeRangeFormatter to handle a NULL `to` value.

Given I don't use the TimeRangeType widget I have only done preliminary testing of this solution, but from what I have done it seems to work. That said, I could be completely missing something!

🐛 Bug report
Status

Active

Version

2.0

Component

Code

Created by

🇨🇦Canada tame4tex

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024