- Issue created by @aurelianzaha
- @aurelianzaha opened merge request.
- Status changed to Needs review
over 1 year ago 3:04pm 20 March 2023 - Status changed to Needs work
over 1 year ago 7:00pm 20 March 2023 - 🇺🇸United States smustgrave
Thank you for reporting this issue.
Next step would be to write a test case for it.
- 🇮🇳India ranjith_kumar_k_u Kerala
Steps to reproduce
create a timestamp field with the default value as empty
Edit the field and check the default value(we can see a value in the default value section)If we are allowing the empty value then we need to change the description of the widget
- Status changed to Needs review
over 1 year ago 2:19pm 29 March 2023 - Status changed to Needs work
over 1 year ago 2:33pm 29 March 2023 - 🇺🇸United States smustgrave
The screenshot says leave blank to use time of form submission. So when the form is submitted it should still get a value and not be empty.
To me this is working by design.
What is the use case for wanting it to be empty?
- 🇦🇹Austria aurelianzaha
thanks for the feedback ranjith_kumar_k_u and smustgrave
in my case, the field is used to define the unpublishing date of a node (another use case would be a birthday field)
leaving empty means the node won't be unpublished (automatically)I understand that the widget was designed to work this way, so I would create a custom widget for my use case
- Status changed to Closed: works as designed
over 1 year ago 4:50am 11 May 2023 - 🇬🇧United Kingdom lind101
For those landing here and wondering what a custom widget to get around this might look like, this kind of thing seems to do the trick:
<?php namespace Drupal\my_module\Plugin\Field\FieldWidget; use Drupal\Core\Datetime\Plugin\Field\FieldWidget\TimestampDatetimeWidget; use Drupal\Core\Form\FormStateInterface; /** * Plugin implementation of the 'nullable datetime timestamp' widget. * * @FieldWidget( * id = "datetime_timestamp_nullable", * label = @Translation("Nullable Datetime Timestamp"), * field_types = { * "timestamp", * } * ) */ class NullableTimestampDatetimeWidget extends TimestampDatetimeWidget { public function massageFormValues(array $values, array $form, FormStateInterface $form_state) { // Record any NULL values $null_values = []; foreach ($values as $delta => $item) { if (is_null($item['value'])) { $null_values[$delta] = $delta; } } // Let the core widget do what it wants $values = parent::massageFormValues($values, $form, $form_state); // Restore the null values foreach ($null_values as $delta) { $values[$delta]['value'] = NULL; } return $values; } }