public function SearchApiFulltext::validateExposed in Search API 8
Validate the exposed handler form
Overrides HandlerBase::validateExposed
File
- src/
Plugin/ views/ filter/ SearchApiFulltext.php, line 310
Class
- SearchApiFulltext
- Defines a filter for adding a fulltext search to the view.
Namespace
Drupal\search_api\Plugin\views\filterCode
public function validateExposed(&$form, FormStateInterface $form_state) {
// Only validate exposed input.
if (empty($this->options['exposed']) || empty($this->options['expose']['identifier'])) {
return;
}
// Store searched fields.
$searched_fields_identifier = $this->options['id'] . '_searched_fields';
if (!empty($this->options['expose']['searched_fields_id'])) {
$searched_fields_identifier = $this->options['expose']['searched_fields_id'];
}
$this->searchedFields = $form_state
->getValue($searched_fields_identifier, []);
$identifier = $this->options['expose']['identifier'];
$input =& $form_state
->getValue($identifier, '');
if ($this->options['is_grouped'] && isset($this->options['group_info']['group_items'][$input])) {
$this->operator = $this->options['group_info']['group_items'][$input]['operator'];
$input =& $this->options['group_info']['group_items'][$input]['value'];
}
// Under some circumstances, input will be an array containing the string
// value. Not sure why, but just deal with that.
while (is_array($input)) {
$input = $input ? reset($input) : '';
}
if (trim($input) === '') {
// No input was given by the user. If the filter was set to "required" and
// there is a query (not the case when an exposed filter block is
// displayed stand-alone), abort it.
if (!empty($this->options['expose']['required']) && $this
->getQuery()) {
$this
->getQuery()
->abort();
}
// If the input is empty, there is nothing to validate: return early.
return;
}
// Only continue if there is a minimum word length set.
if ($this->options['min_length'] < 2) {
return;
}
$words = preg_split('/\\s+/', $input);
foreach ($words as $i => $word) {
if (mb_strlen($word) < $this->options['min_length']) {
unset($words[$i]);
}
}
if (!$words) {
$vars['@count'] = $this->options['min_length'];
$msg = $this
->t('You must include at least one positive keyword with @count characters or more.', $vars);
$form_state
->setErrorByName($identifier, $msg);
}
$input = implode(' ', $words);
}