function SearchController::view
Same name in other branches
- 9 core/modules/search/src/Controller/SearchController.php \Drupal\search\Controller\SearchController::view()
- 10 core/modules/search/src/Controller/SearchController.php \Drupal\search\Controller\SearchController::view()
- 11.x core/modules/search/src/Controller/SearchController.php \Drupal\search\Controller\SearchController::view()
Creates a render array for the search page.
Parameters
\Symfony\Component\HttpFoundation\Request $request: The request object.
\Drupal\search\SearchPageInterface $entity: The search page entity.
Return value
array The search form and search results build array.
File
-
core/
modules/ search/ src/ Controller/ SearchController.php, line 75
Class
- SearchController
- Route controller for search.
Namespace
Drupal\search\ControllerCode
public function view(Request $request, SearchPageInterface $entity) {
$build = [];
$plugin = $entity->getPlugin();
// Build the form first, because it may redirect during the submit,
// and we don't want to build the results based on last time's request.
$build['#cache']['contexts'][] = 'url.query_args:keys';
if ($request->query
->has('keys')) {
$keys = trim($request->query
->get('keys'));
$plugin->setSearch($keys, $request->query
->all(), $request->attributes
->all());
}
$build['#title'] = $plugin->suggestedTitle();
$build['search_form'] = $this->formBuilder()
->getForm(SearchPageForm::class, $entity);
// Build search results, if keywords or other search parameters are in the
// GET parameters. Note that we need to try the search if 'keys' is in
// there at all, vs. being empty, due to advanced search.
$results = [];
if ($request->query
->has('keys')) {
if ($plugin->isSearchExecutable()) {
// Log the search.
if ($this->config('search.settings')
->get('logging')) {
$this->logger
->notice('Searched %type for %keys.', [
'%keys' => $keys,
'%type' => $entity->label(),
]);
}
// Collect the search results.
$results = $plugin->buildResults();
}
else {
// The search not being executable means that no keywords or other
// conditions were entered.
$this->messenger()
->addError($this->t('Please enter some keywords.'));
}
}
if (count($results)) {
$build['search_results_title'] = [
'#markup' => '<h2>' . $this->t('Search results') . '</h2>',
];
}
$build['search_results'] = [
'#theme' => [
'item_list__search_results__' . $plugin->getPluginId(),
'item_list__search_results',
],
'#items' => $results,
'#empty' => [
'#markup' => '<h3>' . $this->t('Your search yielded no results.') . '</h3>',
],
'#list_type' => 'ol',
'#context' => [
'plugin' => $plugin->getPluginId(),
],
];
$this->renderer
->addCacheableDependency($build, $entity);
if ($plugin instanceof CacheableDependencyInterface) {
$this->renderer
->addCacheableDependency($build, $plugin);
}
// If this plugin uses a search index, then also add the cache tag tracking
// that search index, so that cached search result pages are invalidated
// when necessary.
if ($plugin->getType()) {
$build['search_results']['#cache']['tags'][] = 'search_index';
$build['search_results']['#cache']['tags'][] = 'search_index:' . $plugin->getType();
}
$build['pager'] = [
'#type' => 'pager',
];
return $build;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.