- Issue created by @dan_metille
- Assigned to nidhish
- 🇮🇳India rakesh.regar Rajasthan, India
rakesh.regar → made their first commit to this issue’s fork.
- Merge request !4Issue #3502904 :Fixes Non-required field_time fields save the current time as default. → (Open) created by rakesh.regar
- 🇩🇪Germany jox
We're also affected by this issue. I tried both patches from #3 and #6, but unfortunately, neither of them fixes the mentioned behavior.
- 🇩🇪Germany jox
I created a patch that fixes the main issue by making two changes:
- Leave time fields empty when they are not required, instead of setting the current time.
- Leave time range fields empty when they are not required, and avoid the error "The end time cannot be earlier or equal to the start time".Known limitation:
There is still an open issue: if only one value of a time range is entered (e.g., only "Start time" is filled and "End time" is left empty), the entered value will be silently discarded, and both fields will be saved as empty.I am willing to invest some more time to fix this properly (e.g., by adding a validation error if only one side of the range is filled).
Testing steps:
- Create a non-required time or time range field.
- Submit the form without entering any time: the field stays empty (✅).
- Submit the form with both "Start" and "End" filled in: the time range saves correctly (✅).
- Submit only one of "Start" or "End": currently still silently ignored (⚠️ to be fixed). - Status changed to Needs review
7 days ago 2:07pm 5 June 2025 - 🇺🇸United States agentrickard Georgia (US)
It fails to save because both fields are registered as REQUIRED in Drupal\field_time\Plugin\Field\FieldType::propertyDefinitions()
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) { $properties['from'] = DataDefinition::create('string') ->setLabel(new TranslatableMarkup('Start time')) ->setDescription(new TranslatableMarkup('Format HH:MM:SS')) ->setSetting('maxlength', 8) ->setRequired(TRUE); $properties['to'] = DataDefinition::create('string') ->setLabel(new TranslatableMarkup('End time')) ->setDescription(new TranslatableMarkup('Format HH:MM:SS')) ->setSetting('maxlength', 8) ->setRequired(TRUE); return $properties; }
- 🇺🇸United States agentrickard Georgia (US)
It looks like setting NULL fails, but empty string works.
$values[$delta]['value'] = !empty($value['value']) ? (new \DateTimeImmutable($value['value']))->format('H:i:s') : '';
- 🇺🇸United States agentrickard Georgia (US)
I have updated the MR. Here is the patch version.