function DateTimeWidgetBase::massageFormValues

Same name in other branches
  1. 9 core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeWidgetBase.php \Drupal\datetime\Plugin\Field\FieldWidget\DateTimeWidgetBase::massageFormValues()
  2. 8.9.x core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeWidgetBase.php \Drupal\datetime\Plugin\Field\FieldWidget\DateTimeWidgetBase::massageFormValues()
  3. 10 core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeWidgetBase.php \Drupal\datetime\Plugin\Field\FieldWidget\DateTimeWidgetBase::massageFormValues()

Overrides WidgetBase::massageFormValues

1 method overrides DateTimeWidgetBase::massageFormValues()
DateRangeWidgetBase::massageFormValues in core/modules/datetime_range/src/Plugin/Field/FieldWidget/DateRangeWidgetBase.php

File

core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeWidgetBase.php, line 45

Class

DateTimeWidgetBase
Base class for the 'datetime_*' widgets.

Namespace

Drupal\datetime\Plugin\Field\FieldWidget

Code

public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
    // The widget form element type has transformed the value to a
    // DrupalDateTime object at this point. We need to convert it back to the
    // storage timezone and format.
    $datetime_type = $this->getFieldSetting('datetime_type');
    if ($datetime_type === DateTimeItem::DATETIME_TYPE_DATE) {
        $storage_format = DateTimeItemInterface::DATE_STORAGE_FORMAT;
    }
    else {
        $storage_format = DateTimeItemInterface::DATETIME_STORAGE_FORMAT;
    }
    $storage_timezone = new \DateTimezone(DateTimeItemInterface::STORAGE_TIMEZONE);
    foreach ($values as &$item) {
        if (!empty($item['value']) && $item['value'] instanceof DrupalDateTime) {
            
            /** @var \Drupal\Core\Datetime\DrupalDateTime $date */
            $date = $item['value'];
            // Adjust the date for storage.
            $item['value'] = $date->setTimezone($storage_timezone)
                ->format($storage_format);
        }
    }
    return $values;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.