public function NodeSearch::buildSearchUrlQuery in Drupal 9
Same name and namespace in other branches
- 8 core/modules/node/src/Plugin/Search/NodeSearch.php \Drupal\node\Plugin\Search\NodeSearch::buildSearchUrlQuery()
Builds the URL GET query parameters array for search.
When the search form is submitted, a redirect is generated with the search input as GET query parameters. Plugins using the searchFormAlter() method to add form elements to the search form will need to override this method to gather the form input and add it to the GET query parameters.
Parameters
\Drupal\Core\Form\FormStateInterface $form_state: The form state, with submitted form information.
Return value
array An array of GET query parameters containing all relevant form values to process the search. The 'keys' element must be present in order to trigger generation of search results, even if it is empty or unused by the search plugin.
Overrides SearchPluginBase::buildSearchUrlQuery
See also
SearchInterface::searchFormAlter()
File
- core/
modules/ node/ src/ Plugin/ Search/ NodeSearch.php, line 675
Class
- NodeSearch
- Handles searching for node entities using the Search module index.
Namespace
Drupal\node\Plugin\SearchCode
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;
}