You are here

public static function DateRecurModularAlphaWidget::validateModularWidget in Recurring Date Field Modular Widgets 8

Same name and namespace in other branches
  1. 3.x src/Plugin/Field/FieldWidget/DateRecurModularAlphaWidget.php \Drupal\date_recur_modular\Plugin\Field\FieldWidget\DateRecurModularAlphaWidget::validateModularWidget()
  2. 2.x src/Plugin/Field/FieldWidget/DateRecurModularAlphaWidget.php \Drupal\date_recur_modular\Plugin\Field\FieldWidget\DateRecurModularAlphaWidget::validateModularWidget()

Validates the widget.

Parameters

array $element: The element.

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

array $complete_form: The complete form structure.

File

src/Plugin/Field/FieldWidget/DateRecurModularAlphaWidget.php, line 274

Class

DateRecurModularAlphaWidget
Date recur alpha widget.

Namespace

Drupal\date_recur_modular\Plugin\Field\FieldWidget

Code

public static function validateModularWidget(array &$element, FormStateInterface $form_state, array &$complete_form) : void {

  // Each of these values can be array if input was invalid. E.g date or time
  // not provided.

  /** @var \Drupal\Core\Datetime\DrupalDateTime|array|null $start */
  $start = $form_state
    ->getValue(array_merge($element['#parents'], [
    'start',
  ]));

  /** @var \Drupal\Core\Datetime\DrupalDateTime|array|null $end */
  $end = $form_state
    ->getValue(array_merge($element['#parents'], [
    'end',
  ]));

  /** @var string|null $timeZone */
  $timeZone = $form_state
    ->getValue(array_merge($element['#parents'], [
    'time_zone',
  ]));
  if ($start && !$timeZone) {
    $form_state
      ->setError($element['start'], \t('Time zone must be set if start date is set.'));
  }
  if ($end && !$timeZone) {
    $form_state
      ->setError($element['end'], \t('Time zone must be set if end date is set.'));
  }
  if (($start instanceof DrupalDateTime || $end instanceof DrupalDateTime) && (!$start instanceof DrupalDateTime || !$end instanceof DrupalDateTime)) {
    $form_state
      ->setError($element, \t('Start date and end date must be provided.'));
  }

  // Recreate datetime object with exactly the same date and time but
  // different timezone.
  $zoneLess = 'Y-m-d H:i:s';
  $timeZoneObj = new \DateTimeZone($timeZone);
  if ($start instanceof DrupalDateTime && $timeZone) {
    $start = DrupalDateTime::createFromFormat($zoneLess, $start
      ->format($zoneLess), $timeZoneObj);
    $form_state
      ->setValueForElement($element['start'], $start);
  }
  if ($end instanceof DrupalDateTime && $timeZone) {
    $end = DrupalDateTime::createFromFormat($zoneLess, $end
      ->format($zoneLess), $timeZoneObj);
    $form_state
      ->setValueForElement($element['end'], $end);
  }

  /** @var \Drupal\Core\Datetime\DrupalDateTime|array|null $endsDate */
  $endsDate = $form_state
    ->getValue(array_merge($element['#parents'], [
    'ends_date',
  ]));
  if ($endsDate instanceof DrupalDateTime && $timeZone) {
    $endsDate = DrupalDateTime::createFromFormat($zoneLess, $endsDate
      ->format($zoneLess), $timeZoneObj);
    $form_state
      ->setValueForElement($element['ends_date'], $endsDate);
  }
}