function datetime_datelist_widget_validate

Validation callback for the datelist 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_datelist_widget_validate'
DateTimeDatelistWidget::formElement in drupal/core/modules/datetime/lib/Drupal/datetime/Plugin/field/widget/DatetimeDatelistWidget.php
Implements \Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement().

File

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

Code

function datetime_datelist_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 (!in_array('hour', $element['value']['#date_part_order'])) {
            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);
        }
      }
    }
  }
}