TimestampDatetimeWidget::massageFormValues does not allow NULL value

Created on 20 March 2023, over 1 year ago
Updated 18 May 2023, over 1 year ago

Problem/Motivation

When using the field widget TimestampDatetimeWidget for a timestamp field on an entity form and the timestamp field is left empty, on form submit the field will be populated with the current time.

Steps to reproduce

Add a timestamp field to a content type, and use the TimestampDatetimeWidget to render the form widget for the new field. Create an new content and leave the timestamp field empty.

Proposed resolution

Allow NULL value on a timestamp field

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Closed: works as designed

Version

10.1

Component
Base 

Last updated about 6 hours ago

Created by

🇦🇹Austria aurelianzaha

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

  • Issue created by @aurelianzaha
  • @aurelianzaha opened merge request.
  • Status changed to Needs review over 1 year ago
  • Status changed to Needs work over 1 year ago
  • 🇺🇸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
  • Status changed to Needs work over 1 year ago
  • 🇺🇸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
  • 🇬🇧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;
      }
    
    }
    
    
Production build 0.71.5 2024