function SortPluginBase::validateExposeForm

Same name and namespace in other branches
  1. 10 core/modules/views/src/Plugin/views/sort/SortPluginBase.php \Drupal\views\Plugin\views\sort\SortPluginBase::validateExposeForm()
  2. 11.x core/modules/views/src/Plugin/views/sort/SortPluginBase.php \Drupal\views\Plugin\views\sort\SortPluginBase::validateExposeForm()

Validate the options form.

Overrides HandlerBase::validateExposeForm

1 call to SortPluginBase::validateExposeForm()
SortPluginBase::validateOptionsForm in core/modules/views/src/Plugin/views/sort/SortPluginBase.php
Simple validate handler.

File

core/modules/views/src/Plugin/views/sort/SortPluginBase.php, line 227

Class

SortPluginBase
Base sort handler that has no options and performs a simple sort.

Namespace

Drupal\views\Plugin\views\sort

Code

public function validateExposeForm($form, FormStateInterface $form_state) {
    $field_identifier = $form_state->getValue([
        'options',
        'expose',
        'field_identifier',
    ]);
    if (!preg_match('/^[a-zA-z][a-zA-Z0-9_~.\\-]*$/', $field_identifier)) {
        $form_state->setErrorByName('expose][field_identifier', $this->t('This identifier has illegal characters.'));
        return;
    }
    // Validate that the sort field identifier is unique within the sort
    // handlers. Note that the sort field identifier is different that other
    // identifiers because it is used as a query string value of the 'sort_by'
    // parameter, while the others are used as query string parameter keys.
    // Therefore we can have a sort field identifier be the same as an exposed
    // filter identifier. This prevents us from using
    // DisplayPluginInterface::isIdentifierUnique() to test for uniqueness.
    // @see \Drupal\views\Plugin\views\display\DisplayPluginInterface::isIdentifierUnique()
    foreach ($this->view->display_handler
        ->getHandlers('sort') as $key => $handler) {
        if ($handler->canExpose() && $handler->isExposed()) {
            if ($form_state->get('id') !== $key && isset($handler->options['expose']['field_identifier']) && $field_identifier === $handler->options['expose']['field_identifier']) {
                $form_state->setErrorByName('expose][field_identifier', $this->t('This identifier is already used by %label sort handler.', [
                    '%label' => $handler->adminLabel(TRUE),
                ]));
                return;
            }
        }
    }
}

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