function NodeSearch::parseAdvancedDefaults

Same name and namespace in other branches
  1. 9 core/modules/node/src/Plugin/Search/NodeSearch.php \Drupal\node\Plugin\Search\NodeSearch::parseAdvancedDefaults()
  2. 8.9.x core/modules/node/src/Plugin/Search/NodeSearch.php \Drupal\node\Plugin\Search\NodeSearch::parseAdvancedDefaults()
  3. 10 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.

1 call to NodeSearch::parseAdvancedDefaults()
NodeSearch::searchFormAlter in core/modules/node/src/Plugin/Search/NodeSearch.php
Alters the search form when being built for a given plugin.

File

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

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;
}

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