You are here

public static function DateRecurFilter::validateValue in Recurring Dates Field 8.2

Same name and namespace in other branches
  1. 3.x src/Plugin/views/filter/DateRecurFilter.php \Drupal\date_recur\Plugin\views\filter\DateRecurFilter::validateValue()
  2. 3.0.x src/Plugin/views/filter/DateRecurFilter.php \Drupal\date_recur\Plugin\views\filter\DateRecurFilter::validateValue()
  3. 3.1.x src/Plugin/views/filter/DateRecurFilter.php \Drupal\date_recur\Plugin\views\filter\DateRecurFilter::validateValue()

Form field validator.

Parameters

array $element: The form element.

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

File

src/Plugin/views/filter/DateRecurFilter.php, line 247

Class

DateRecurFilter
Date range/occurrence filter.

Namespace

Drupal\date_recur\Plugin\views\filter

Code

public static function validateValue(array &$element, FormStateInterface $form_state) : void {
  $elementValue = $element['#value'];
  if ($element['#required'] === FALSE && empty($elementValue)) {
    return;
  }

  /* @var $pluginOptions array */
  $pluginOptions = $element['#filter_plugin_options'];
  $granularity = $pluginOptions['value_granularity'];
  if (empty($granularity)) {
    throw new \LogicException('Granularity not set.');
  }

  /* @var string|null $optionValueMin */
  $optionValueMin = $pluginOptions['value_min'];
  $valueMin = isset($optionValueMin) ? \DateTime::createFromFormat(\DATE_ISO8601, $optionValueMin) : NULL;

  /* @var string|null $optionValueMax */
  $optionValueMax = $pluginOptions['value_max'];
  $valueMax = isset($optionValueMax) ? \DateTime::createFromFormat(\DATE_ISO8601, $optionValueMax) : NULL;
  $granularityFormatsMap = DateRecurGranularityMap::GRANULARITY_DATE_FORMATS;
  $format = $granularityFormatsMap[$granularity];
  $granularityRegexMap = DateRecurGranularityMap::GRANULARITY_EXPRESSIONS;
  $regex = $granularityRegexMap[$granularity];
  $value = $element['#value'];

  // Use the current users timezone.
  $timezone = new \DateTimeZone($element['#filter_plugin_user_timezone']);
  if (preg_match($regex, $value)) {

    // Validate value against minimum and maximums.
    if ($valueMin) {
      $largest = DateRecurUtility::createLargestDateFromInput($granularity, $value, $timezone);
      if ($largest < $valueMin) {
        $form_state
          ->setError($element, (string) \t('Value is under minimum @minimum_as_granularity', [
          '@minimum_full' => $valueMin
            ->format('r'),
          '@minimum_as_granularity' => $valueMin
            ->format($format),
        ]));
      }
    }
    $smallest = DateRecurUtility::createSmallestDateFromInput($granularity, $value, $timezone);
    if ($valueMax) {
      if ($smallest > $valueMax) {
        $form_state
          ->setError($element, (string) \t('Value is over maximum @maximum_as_granularity', [
          '@minimum_full' => $valueMax
            ->format('r'),
          '@maximum_as_granularity' => $valueMax
            ->format($format),
        ]));
      }
    }
  }
  else {
    $now = new DrupalDateTime();
    $sample = $now
      ->format($format);
    $granularityExpectedFormatMessages = DateRecurGranularityMap::granularityExpectedFormatMessages($sample);
    $form_state
      ->setError($element, (string) \t('Value format is incorrect. Expected format: @example', [
      '@example' => $granularityExpectedFormatMessages[$granularity],
    ]));
  }
}