Chris@0: searchPageRepository = $search_page_repository; Chris@0: $this->logger = $this->getLogger('search'); Chris@0: $this->renderer = $renderer; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function create(ContainerInterface $container) { Chris@0: return new static( Chris@0: $container->get('search.search_page_repository'), Chris@0: $container->get('renderer') Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a render array for the search page. Chris@0: * Chris@0: * @param \Symfony\Component\HttpFoundation\Request $request Chris@0: * The request object. Chris@0: * @param \Drupal\search\SearchPageInterface $entity Chris@0: * The search page entity. Chris@0: * Chris@0: * @return array Chris@0: * The search form and search results build array. Chris@0: */ Chris@0: public function view(Request $request, SearchPageInterface $entity) { Chris@0: $build = []; Chris@0: $plugin = $entity->getPlugin(); Chris@0: Chris@0: // Build the form first, because it may redirect during the submit, Chris@0: // and we don't want to build the results based on last time's request. Chris@0: $build['#cache']['contexts'][] = 'url.query_args:keys'; Chris@0: if ($request->query->has('keys')) { Chris@0: $keys = trim($request->query->get('keys')); Chris@0: $plugin->setSearch($keys, $request->query->all(), $request->attributes->all()); Chris@0: } Chris@0: Chris@0: $build['#title'] = $plugin->suggestedTitle(); Chris@0: $build['search_form'] = $this->formBuilder()->getForm(SearchPageForm::class, $entity); Chris@0: Chris@0: // Build search results, if keywords or other search parameters are in the Chris@0: // GET parameters. Note that we need to try the search if 'keys' is in Chris@0: // there at all, vs. being empty, due to advanced search. Chris@0: $results = []; Chris@0: if ($request->query->has('keys')) { Chris@0: if ($plugin->isSearchExecutable()) { Chris@0: // Log the search. Chris@0: if ($this->config('search.settings')->get('logging')) { Chris@0: $this->logger->notice('Searched %type for %keys.', ['%keys' => $keys, '%type' => $entity->label()]); Chris@0: } Chris@0: Chris@0: // Collect the search results. Chris@0: $results = $plugin->buildResults(); Chris@0: } Chris@0: else { Chris@0: // The search not being executable means that no keywords or other Chris@0: // conditions were entered. Chris@17: $this->messenger()->addError($this->t('Please enter some keywords.')); Chris@0: } Chris@0: } Chris@0: Chris@0: if (count($results)) { Chris@0: $build['search_results_title'] = [ Chris@0: '#markup' => '

' . $this->t('Search results') . '

', Chris@0: ]; Chris@0: } Chris@0: Chris@0: $build['search_results'] = [ Chris@0: '#theme' => ['item_list__search_results__' . $plugin->getPluginId(), 'item_list__search_results'], Chris@0: '#items' => $results, Chris@0: '#empty' => [ Chris@0: '#markup' => '

' . $this->t('Your search yielded no results.') . '

', Chris@0: ], Chris@0: '#list_type' => 'ol', Chris@0: '#context' => [ Chris@0: 'plugin' => $plugin->getPluginId(), Chris@0: ], Chris@0: ]; Chris@0: Chris@0: $this->renderer->addCacheableDependency($build, $entity); Chris@0: if ($plugin instanceof CacheableDependencyInterface) { Chris@0: $this->renderer->addCacheableDependency($build, $plugin); Chris@0: } Chris@0: Chris@0: // If this plugin uses a search index, then also add the cache tag tracking Chris@0: // that search index, so that cached search result pages are invalidated Chris@0: // when necessary. Chris@0: if ($plugin->getType()) { Chris@0: $build['search_results']['#cache']['tags'][] = 'search_index'; Chris@0: $build['search_results']['#cache']['tags'][] = 'search_index:' . $plugin->getType(); Chris@0: } Chris@0: Chris@0: $build['pager'] = [ Chris@0: '#type' => 'pager', Chris@0: ]; Chris@0: Chris@0: return $build; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a render array for the search help page. Chris@0: * Chris@0: * @param \Drupal\search\SearchPageInterface $entity Chris@0: * The search page entity. Chris@0: * Chris@0: * @return array Chris@0: * The search help page. Chris@0: */ Chris@0: public function searchHelp(SearchPageInterface $entity) { Chris@0: $build = []; Chris@0: Chris@0: $build['search_help'] = $entity->getPlugin()->getHelp(); Chris@0: Chris@0: return $build; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Redirects to a search page. Chris@0: * Chris@0: * This is used to redirect from /search to the default search page. Chris@0: * Chris@0: * @param \Drupal\search\SearchPageInterface $entity Chris@0: * The search page entity. Chris@0: * Chris@0: * @return \Symfony\Component\HttpFoundation\RedirectResponse Chris@0: * A redirect to the search page. Chris@0: */ Chris@0: public function redirectSearchPage(SearchPageInterface $entity) { Chris@0: return $this->redirect('search.view_' . $entity->id()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Route title callback. Chris@0: * Chris@0: * @param \Drupal\search\SearchPageInterface $search_page Chris@0: * The search page entity. Chris@0: * Chris@0: * @return string Chris@0: * The title for the search page edit form. Chris@0: */ Chris@0: public function editTitle(SearchPageInterface $search_page) { Chris@0: return $this->t('Edit %label search page', ['%label' => $search_page->label()]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Performs an operation on the search page entity. Chris@0: * Chris@0: * @param \Drupal\search\SearchPageInterface $search_page Chris@0: * The search page entity. Chris@0: * @param string $op Chris@0: * The operation to perform, usually 'enable' or 'disable'. Chris@0: * Chris@0: * @return \Symfony\Component\HttpFoundation\RedirectResponse Chris@0: * A redirect back to the search settings page. Chris@0: */ Chris@0: public function performOperation(SearchPageInterface $search_page, $op) { Chris@0: $search_page->$op()->save(); Chris@0: Chris@0: if ($op == 'enable') { Chris@17: $this->messenger()->addStatus($this->t('The %label search page has been enabled.', ['%label' => $search_page->label()])); Chris@0: } Chris@0: elseif ($op == 'disable') { Chris@17: $this->messenger()->addStatus($this->t('The %label search page has been disabled.', ['%label' => $search_page->label()])); Chris@0: } Chris@0: Chris@18: $url = $search_page->toUrl('collection'); Chris@0: return $this->redirect($url->getRouteName(), $url->getRouteParameters(), $url->getOptions()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the search page as the default. Chris@0: * Chris@0: * @param \Drupal\search\SearchPageInterface $search_page Chris@0: * The search page entity. Chris@0: * Chris@0: * @return \Symfony\Component\HttpFoundation\RedirectResponse Chris@0: * A redirect to the search settings page. Chris@0: */ Chris@0: public function setAsDefault(SearchPageInterface $search_page) { Chris@0: // Set the default page to this search page. Chris@0: $this->searchPageRepository->setDefaultSearchPage($search_page); Chris@0: Chris@17: $this->messenger()->addStatus($this->t('The default search page is now %label. Be sure to check the ordering of your search pages.', ['%label' => $search_page->label()])); Chris@0: return $this->redirect('entity.search_page.collection'); Chris@0: } Chris@0: Chris@0: }