function NodeSearch::buildSearchUrlQuery

Same name and namespace in other branches
  1. 9 core/modules/node/src/Plugin/Search/NodeSearch.php \Drupal\node\Plugin\Search\NodeSearch::buildSearchUrlQuery()
  2. 8.9.x core/modules/node/src/Plugin/Search/NodeSearch.php \Drupal\node\Plugin\Search\NodeSearch::buildSearchUrlQuery()
  3. 10 core/modules/node/src/Plugin/Search/NodeSearch.php \Drupal\node\Plugin\Search\NodeSearch::buildSearchUrlQuery()

Overrides SearchPluginBase::buildSearchUrlQuery

File

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

Class

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

Namespace

Drupal\node\Plugin\Search

Code

public function buildSearchUrlQuery(FormStateInterface $form_state) {
    // Read keyword and advanced search information from the form values,
    // and put these into the GET parameters.
    $keys = trim($form_state->getValue('keys'));
    $advanced = FALSE;
    // Collect extra filters.
    $filters = [];
    if ($form_state->hasValue('type') && is_array($form_state->getValue('type'))) {
        // Retrieve selected types - Form API sets the value of unselected
        // checkboxes to 0.
        foreach ($form_state->getValue('type') as $type) {
            if ($type) {
                $advanced = TRUE;
                $filters[] = 'type:' . $type;
            }
        }
    }
    if ($form_state->hasValue('term') && is_array($form_state->getValue('term'))) {
        foreach ($form_state->getValue('term') as $term) {
            $filters[] = 'term:' . $term;
            $advanced = TRUE;
        }
    }
    if ($form_state->hasValue('language') && is_array($form_state->getValue('language'))) {
        foreach ($form_state->getValue('language') as $language) {
            if ($language) {
                $advanced = TRUE;
                $filters[] = 'language:' . $language;
            }
        }
    }
    if ($form_state->getValue('or') != '') {
        if (preg_match_all('/ ("[^"]+"|[^" ]+)/i', ' ' . $form_state->getValue('or'), $matches)) {
            $keys .= ' ' . implode(' OR ', $matches[1]);
            $advanced = TRUE;
        }
    }
    if ($form_state->getValue('negative') != '') {
        if (preg_match_all('/ ("[^"]+"|[^" ]+)/i', ' ' . $form_state->getValue('negative'), $matches)) {
            $keys .= ' -' . implode(' -', $matches[1]);
            $advanced = TRUE;
        }
    }
    if ($form_state->getValue('phrase') != '') {
        $keys .= ' "' . str_replace('"', ' ', $form_state->getValue('phrase')) . '"';
        $advanced = TRUE;
    }
    $keys = trim($keys);
    // Put the keywords and advanced parameters into GET parameters. Make sure
    // to put keywords into the query even if it is empty, because the page
    // controller uses that to decide it's time to check for search results.
    $query = [
        'keys' => $keys,
    ];
    if ($filters) {
        $query['f'] = $filters;
    }
    // Record that the person used the advanced search form, if they did.
    if ($advanced) {
        $query[self::ADVANCED_FORM] = '1';
    }
    return $query;
}

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