function datetime_datetime_widget_validate

Validation callback for the datetime widget element.

The date has already been validated by the datetime form type validator and transformed to an date object. We just need to convert the date back to a the storage timezone and format.

Parameters

array $element: The form element whose value is being validated.

array $form_state: The current state of the form.

1 string reference to 'datetime_datetime_widget_validate'
DateTimeDefaultWidget::formElement in drupal/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDefaultWidget.php
Implements \Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement().

File

drupal/core/modules/datetime/datetime.module, line 164
Field hooks to implement a simple datetime field.

Code

function datetime_datetime_widget_validate(&$element, &$form_state) {
  if (!form_get_errors()) {
    $input_exists = FALSE;
    $input = NestedArray::getValue($form_state['values'], $element['#parents'], $input_exists);
    if ($input_exists) {

      // The date should have been returned to a date object at this point by
      // datetime_validate(), which runs before this.
      if (!empty($input['value'])) {
        $date = $input['value'];
        if ($date instanceof DrupalDateTime && !$date
          ->hasErrors()) {

          // If this is a date-only field, set it to the default time so the
          // timezone conversion can be reversed.
          if ($element['value']['#date_time_element'] == 'none') {
            datetime_date_default_time($date);
          }

          // Adjust the date for storage.
          $date
            ->setTimezone(new \DateTimezone(DATETIME_STORAGE_TIMEZONE));
          $value = $date
            ->format($element['value']['#date_storage_format']);
          form_set_value($element['value'], $value, $form_state);
        }
      }
    }
  }
}