You are here

public function DateRecurBasicWidget::validateRrule in Recurring Dates Field 3.0.x

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

Validates RRULE and first occurrence dates.

Parameters

array $element: An associative array containing the properties and children of the generic form 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/DateRecurBasicWidget.php, line 162

Class

DateRecurBasicWidget
Basic RRULE widget.

Namespace

Drupal\date_recur\Plugin\Field\FieldWidget

Code

public function validateRrule(array &$element, FormStateInterface $form_state, array &$complete_form) : void {
  $input = NestedArray::getValue($form_state
    ->getValues(), $element['#parents']);

  /** @var \Drupal\Core\Datetime\DrupalDateTime|array|null $startDate */
  $startDate = $input['value'];

  /** @var \Drupal\Core\Datetime\DrupalDateTime|array|null $startDateEnd */
  $startDateEnd = $input['end_value'];
  if (is_array($startDate) || is_array($startDateEnd)) {

    // Dates are an array if invalid input was submitted (e.g date:
    // 80616-02-01).
    return;
  }

  /** @var string $rrule */
  $rrule = $input['rrule'];
  if ($startDateEnd && !isset($startDate)) {
    $form_state
      ->setError($element['value'], (string) $this
      ->t('Start date must be set if end date is set.'));
  }

  // If end was empty, copy start date over.
  if (!isset($startDateEnd) && $startDate) {
    $form_state
      ->setValueForElement($element['end_value'], $startDate);
    $startDateEnd = $startDate;
  }

  // Validate RRULE.
  // Only ensure start date is set, as end date is optional.
  if (strlen($rrule) > 0 && $startDate) {
    try {
      DateRecurHelper::create($rrule, $startDate
        ->getPhpDateTime(), $startDateEnd ? $startDateEnd
        ->getPhpDateTime() : NULL);
    } catch (\Exception $e) {
      $form_state
        ->setError($element['rrule'], (string) $this
        ->t('Repeat rule is formatted incorrectly.'));
    }
  }
}