You are here

protected function NodeSearch::parseAdvancedDefaults in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/node/src/Plugin/Search/NodeSearch.php \Drupal\node\Plugin\Search\NodeSearch::parseAdvancedDefaults()
  2. 9 core/modules/node/src/Plugin/Search/NodeSearch.php \Drupal\node\Plugin\Search\NodeSearch::parseAdvancedDefaults()

Parses the advanced search form default values.

Parameters

array $f: The 'f' query parameter set up in self::buildUrlSearchQuery(), which contains the advanced query values.

string $keys: The search keywords string, which contains some information from the advanced search form.

Return value

array Array of default form values for the advanced search form, including a modified 'keys' element for the bare search keywords.

File

core/modules/node/src/Plugin/Search/NodeSearch.php, line 755

Class

NodeSearch
Handles searching for node entities using the Search module index.

Namespace

Drupal\node\Plugin\Search

Code

protected function parseAdvancedDefaults($f, $keys) {
  $defaults = [];

  // Split out the advanced search parameters.
  foreach ($f as $advanced) {
    [
      $key,
      $value,
    ] = explode(':', $advanced, 2);
    if (!isset($defaults[$key])) {
      $defaults[$key] = [];
    }
    $defaults[$key][] = $value;
  }

  // Split out the negative, phrase, and OR parts of keywords.
  // For phrases, the form only supports one phrase.
  $matches = [];
  $keys = ' ' . $keys . ' ';
  if (preg_match('/ "([^"]+)" /', $keys, $matches)) {
    $keys = str_replace($matches[0], ' ', $keys);
    $defaults['phrase'] = $matches[1];
  }

  // Negative keywords: pull all of them out.
  if (preg_match_all('/ -([^ ]+)/', $keys, $matches)) {
    $keys = str_replace($matches[0], ' ', $keys);
    $defaults['negative'] = implode(' ', $matches[1]);
  }

  // OR keywords: pull up to one set of them out of the query.
  if (preg_match('/ [^ ]+( OR [^ ]+)+ /', $keys, $matches)) {
    $keys = str_replace($matches[0], ' ', $keys);
    $words = explode(' OR ', trim($matches[0]));
    $defaults['or'] = implode(' ', $words);
  }

  // Put remaining keywords string back into keywords.
  $defaults['keys'] = trim($keys);
  return $defaults;
}