You are here

public function DateRecurBasicWidget::dateValueCallback in Recurring Dates Field 8.2

Same name and namespace in other branches
  1. 3.x src/Plugin/Field/FieldWidget/DateRecurBasicWidget.php \Drupal\date_recur\Plugin\Field\FieldWidget\DateRecurBasicWidget::dateValueCallback()
  2. 3.0.x src/Plugin/Field/FieldWidget/DateRecurBasicWidget.php \Drupal\date_recur\Plugin\Field\FieldWidget\DateRecurBasicWidget::dateValueCallback()
  3. 3.1.x src/Plugin/Field/FieldWidget/DateRecurBasicWidget.php \Drupal\date_recur\Plugin\Field\FieldWidget\DateRecurBasicWidget::dateValueCallback()

Validator for start and end elements.

Sets the time zone before datetime element processes values.

Parameters

array $element: An associative array containing the properties and children of the generic form element.

array|false $input: Input, if any.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The value to assign to the element.

File

src/Plugin/Field/FieldWidget/DateRecurBasicWidget.php, line 109

Class

DateRecurBasicWidget
Basic RRULE widget.

Namespace

Drupal\date_recur\Plugin\Field\FieldWidget

Code

public function dateValueCallback(array $element, $input, FormStateInterface $form_state) : array {
  if ($input !== FALSE) {
    $timeZonePath = array_slice($element['#parents'], 0, -1);
    $timeZonePath[] = 'timezone';

    // Warning: The time zone is not yet validated, make sure it is valid
    // before using.
    $submittedTimeZone = NestedArray::getValue($form_state
      ->getUserInput(), $timeZonePath);
    if (!isset($submittedTimeZone)) {

      // If no time zone was submitted, such as when the 'timezone' field is
      // set to #access => FALSE, its necessary to fall back to the fields
      // default value.
      $timeZoneFieldPath = array_slice($element['#array_parents'], 0, -1);
      $timeZoneFieldPath[] = 'timezone';
      $timeZoneField = NestedArray::getValue($form_state
        ->getCompleteForm(), $timeZoneFieldPath);
      $submittedTimeZone = isset($timeZoneField["#value"]) ? $timeZoneField["#value"] : $timeZoneField["#default_value"] ?? NULL;
    }
    $allTimeZones = \DateTimeZone::listIdentifiers();

    // @todo Add test for invalid submitted time zone.
    if (!in_array($submittedTimeZone, $allTimeZones)) {

      // A date is invalid if the time zone is invalid.
      // Need to kill inputs otherwise
      // \Drupal\Core\Datetime\Element\Datetime::validateDatetime thinks there
      // is valid input.
      return [
        'date' => '',
        'time' => '',
        'object' => NULL,
      ];
    }
    $element['#date_timezone'] = $submittedTimeZone;
  }

  // Setting a callback overrides default value callback in the element,
  // call original now.
  return Datetime::valueCallback($element, $input, $form_state);
}