function SqlBase::validateOptionsForm

Same name and namespace in other branches
  1. 9 core/modules/views/src/Plugin/views/pager/SqlBase.php \Drupal\views\Plugin\views\pager\SqlBase::validateOptionsForm()
  2. 8.9.x core/modules/views/src/Plugin/views/pager/SqlBase.php \Drupal\views\Plugin\views\pager\SqlBase::validateOptionsForm()
  3. 11.x core/modules/views/src/Plugin/views/pager/SqlBase.php \Drupal\views\Plugin\views\pager\SqlBase::validateOptionsForm()

Provide the default form for validating options.

Overrides PagerPluginBase::validateOptionsForm

File

core/modules/views/src/Plugin/views/pager/SqlBase.php, line 235

Class

SqlBase
A common base class for sql based pager.

Namespace

Drupal\views\Plugin\views\pager

Code

public function validateOptionsForm(&$form, FormStateInterface $form_state) {
  // Only accept integer values.
  $error = FALSE;
  $exposed_options = $form_state->getValue([
    'pager_options',
    'expose',
    'items_per_page_options',
  ]);
  if (str_contains($exposed_options, '.')) {
    $error = TRUE;
  }
  $options = explode(',', $exposed_options);
  if (!$error && is_array($options)) {
    foreach ($options as $option) {
      if (!is_numeric($option) || intval($option) == 0) {
        $error = TRUE;
      }
    }
  }
  else {
    $error = TRUE;
  }
  if ($error) {
    $form_state->setErrorByName('pager_options][expose][items_per_page_options', $this->t('Insert a list of integer numeric values separated by commas: e.g: 10, 20, 50, 100'));
  }
  // Make sure that the items_per_page is part of the expose settings.
  if (!$form_state->isValueEmpty([
    'pager_options',
    'expose',
    'items_per_page',
  ]) && !$form_state->isValueEmpty([
    'pager_options',
    'items_per_page',
  ])) {
    $items_per_page = $form_state->getValue([
      'pager_options',
      'items_per_page',
    ]);
    if (array_search($items_per_page, $options) === FALSE) {
      $form_state->setErrorByName('pager_options][expose][items_per_page_options', $this->t("The <em>Exposed items per page</em> field's options must include the value from the <em>Items per page</em> field (@items_per_page).", [
        '@items_per_page' => $items_per_page,
      ]));
    }
  }
}

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