function InOperator::validate

Same name in other branches
  1. 9 core/modules/views/src/Plugin/views/filter/InOperator.php \Drupal\views\Plugin\views\filter\InOperator::validate()
  2. 10 core/modules/views/src/Plugin/views/filter/InOperator.php \Drupal\views\Plugin\views\filter\InOperator::validate()
  3. 11.x core/modules/views/src/Plugin/views/filter/InOperator.php \Drupal\views\Plugin\views\filter\InOperator::validate()

Overrides FilterPluginBase::validate

File

core/modules/views/src/Plugin/views/filter/InOperator.php, line 417

Class

InOperator
Simple filter to handle matching of multiple options selectable via checkboxes

Namespace

Drupal\views\Plugin\views\filter

Code

public function validate() {
    $this->getValueOptions();
    $errors = parent::validate();
    // If the operator is an operator which doesn't require a value, there is
    // no need for additional validation.
    if (in_array($this->operator, $this->operatorValues(0))) {
        return [];
    }
    if (!in_array($this->operator, $this->operatorValues(1))) {
        $errors[] = $this->t('The operator is invalid on filter: @filter.', [
            '@filter' => $this->adminLabel(TRUE),
        ]);
    }
    if (is_array($this->value)) {
        if (!isset($this->valueOptions)) {
            // Don't validate if there are none value options provided, for example for special handlers.
            return $errors;
        }
        if ($this->options['exposed'] && !$this->options['expose']['required'] && empty($this->value)) {
            // Don't validate if the field is exposed and no default value is provided.
            return $errors;
        }
        // Some filter_in_operator usage uses optgroups forms, so flatten it.
        $flat_options = OptGroup::flattenOptions($this->valueOptions);
        // Remove every element which is not known.
        foreach ($this->value as $value) {
            if (!isset($flat_options[$value])) {
                unset($this->value[$value]);
            }
        }
        // Choose different kind of output for 0, a single and multiple values.
        if (count($this->value) == 0) {
            $errors[] = $this->t('No valid values found on filter: @filter.', [
                '@filter' => $this->adminLabel(TRUE),
            ]);
        }
    }
    elseif (!empty($this->value) && ($this->operator == 'in' || $this->operator == 'not in')) {
        $errors[] = $this->t('The value @value is not an array for @operator on filter: @filter', [
            '@value' => var_export($this->value, TRUE),
            '@operator' => $this->operator,
            '@filter' => $this->adminLabel(TRUE),
        ]);
    }
    return $errors;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.