You are here

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

Same name and namespace in other branches
  1. 3.x src/Plugin/Field/FieldWidget/DateRecurModularSierraWidget.php \Drupal\date_recur_modular\Plugin\Field\FieldWidget\DateRecurModularSierraWidget::validateModularWidget()
  2. 2.x src/Plugin/Field/FieldWidget/DateRecurModularSierraWidget.php \Drupal\date_recur_modular\Plugin\Field\FieldWidget\DateRecurModularSierraWidget::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/DateRecurModularSierraWidget.php, line 592

Class

DateRecurModularSierraWidget
Date recur sierra widget.

Namespace

Drupal\date_recur_modular\Plugin\Field\FieldWidget

Code

public static function validateModularWidget(array &$element, FormStateInterface $form_state, array &$complete_form) : void {
  $valueParents = $element['#parents'];
  $formParents = $element['#array_parents'];

  // Dont start validation until at least the start date is not empty.

  /** @var string|null $start */
  $startDay = $form_state
    ->getValue(array_merge($valueParents, [
    'day_start',
  ]));
  if (empty($startDay)) {
    return;
  }

  /** @var string|null $timeZone */
  $timeZone = $form_state
    ->getValue(array_merge($valueParents, [
    'time_zone',
  ]));
  if (empty($startDay)) {
    $form_state
      ->setError($element, \t('Time zone must be set if start date is set.'));
  }
  $isAllDay = (bool) $form_state
    ->getValue(array_merge($valueParents, [
    'is_all_day',
  ]));
  if ($isAllDay) {
    $form_state
      ->setValue(array_merge($valueParents, [
      'time_start',
    ]), '00:00:00');
    $form_state
      ->setValue(array_merge($valueParents, [
      'time_end',
    ]), '23:59:59');
  }
  try {
    $startDate = static::buildDatesFromFields(array_merge($formParents, [
      'day_start',
    ]), array_merge($formParents, [
      'time_start',
    ]), $timeZone, $form_state);
    $form_state
      ->setValue(array_merge($valueParents, [
      'start',
    ]), $startDate);
  } catch (\Exception $e) {
    $message = \t('Start date and time invalid.');
    $form_state
      ->setError($element['day_start'], $message);
    $form_state
      ->setError($element['time_start'], $message);
  }
  try {
    $dateEnd = static::buildDatesFromFields(array_merge($formParents, [
      'day_end',
    ]), array_merge($formParents, [
      'time_end',
    ]), $timeZone, $form_state);
    $form_state
      ->setValue(array_merge($valueParents, [
      'end',
    ]), $dateEnd);
  } catch (\Exception $e) {
    $message = \t('End date and time invalid.');
    $form_state
      ->setError($element['day_end'], $message);
    $form_state
      ->setError($element['time_end'], $message);
  }
  if (isset($startDate) && isset($dateEnd) && $startDate > $dateEnd) {
    $form_state
      ->setError($element['day_end'], \t('End date cannot be before the start date.'));
  }
  elseif (isset($startDate) && !isset($dateEnd)) {
    $form_state
      ->setError($element['day_end'], \t('End date must be set if start date is set.'));
  }
  elseif (!isset($startDate) && isset($dateEnd)) {
    $form_state
      ->setError($element['day_start'], \t('Start date must be set if end date is set.'));
  }

  // Process RRULE.
  $rrule = '';
  if (isset($startDate)) {
    $recurrenceOption = $form_state
      ->getValue(array_merge($valueParents, [
      'recurrence_option',
    ]));
    if ($recurrenceOption === 'custom') {
      $rrule = $form_state
        ->get([
        static::FORM_STATE_RRULE_KEY,
        $element['field_path']['#value'],
      ]);

      // There wont be a value in form state if the modal wasn't interacted
      // with, so fall back to value in storage.
      if (!isset($rrule)) {
        $rrule = $form_state
          ->getValue(array_merge($valueParents, [
          'rrule_in_storage',
        ]));
      }
    }
    else {
      $rrule = static::buildRruleFromRecurrenceOption($startDate, $recurrenceOption);
    }
  }
  $form_state
    ->setValue(array_merge($valueParents, [
    'rrule',
  ]), $rrule);
}