Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\search\Controller;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Cache\CacheableDependencyInterface;
|
Chris@0
|
6 use Drupal\Core\Controller\ControllerBase;
|
Chris@0
|
7 use Drupal\Core\Render\RendererInterface;
|
Chris@0
|
8 use Drupal\search\Form\SearchPageForm;
|
Chris@0
|
9 use Drupal\search\SearchPageInterface;
|
Chris@0
|
10 use Drupal\search\SearchPageRepositoryInterface;
|
Chris@0
|
11 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
12 use Symfony\Component\HttpFoundation\Request;
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * Route controller for search.
|
Chris@0
|
16 */
|
Chris@0
|
17 class SearchController extends ControllerBase {
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * The search page repository.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @var \Drupal\search\SearchPageRepositoryInterface
|
Chris@0
|
23 */
|
Chris@0
|
24 protected $searchPageRepository;
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * A logger instance.
|
Chris@0
|
28 *
|
Chris@0
|
29 * @var \Psr\Log\LoggerInterface
|
Chris@0
|
30 */
|
Chris@0
|
31 protected $logger;
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * The renderer.
|
Chris@0
|
35 *
|
Chris@0
|
36 * @var \Drupal\Core\Render\RendererInterface
|
Chris@0
|
37 */
|
Chris@0
|
38 protected $renderer;
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * Constructs a new search controller.
|
Chris@0
|
42 *
|
Chris@0
|
43 * @param \Drupal\search\SearchPageRepositoryInterface $search_page_repository
|
Chris@0
|
44 * The search page repository.
|
Chris@0
|
45 * @param \Drupal\Core\Render\RendererInterface $renderer
|
Chris@0
|
46 * The renderer.
|
Chris@0
|
47 */
|
Chris@0
|
48 public function __construct(SearchPageRepositoryInterface $search_page_repository, RendererInterface $renderer) {
|
Chris@0
|
49 $this->searchPageRepository = $search_page_repository;
|
Chris@0
|
50 $this->logger = $this->getLogger('search');
|
Chris@0
|
51 $this->renderer = $renderer;
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 /**
|
Chris@0
|
55 * {@inheritdoc}
|
Chris@0
|
56 */
|
Chris@0
|
57 public static function create(ContainerInterface $container) {
|
Chris@0
|
58 return new static(
|
Chris@0
|
59 $container->get('search.search_page_repository'),
|
Chris@0
|
60 $container->get('renderer')
|
Chris@0
|
61 );
|
Chris@0
|
62 }
|
Chris@0
|
63
|
Chris@0
|
64 /**
|
Chris@0
|
65 * Creates a render array for the search page.
|
Chris@0
|
66 *
|
Chris@0
|
67 * @param \Symfony\Component\HttpFoundation\Request $request
|
Chris@0
|
68 * The request object.
|
Chris@0
|
69 * @param \Drupal\search\SearchPageInterface $entity
|
Chris@0
|
70 * The search page entity.
|
Chris@0
|
71 *
|
Chris@0
|
72 * @return array
|
Chris@0
|
73 * The search form and search results build array.
|
Chris@0
|
74 */
|
Chris@0
|
75 public function view(Request $request, SearchPageInterface $entity) {
|
Chris@0
|
76 $build = [];
|
Chris@0
|
77 $plugin = $entity->getPlugin();
|
Chris@0
|
78
|
Chris@0
|
79 // Build the form first, because it may redirect during the submit,
|
Chris@0
|
80 // and we don't want to build the results based on last time's request.
|
Chris@0
|
81 $build['#cache']['contexts'][] = 'url.query_args:keys';
|
Chris@0
|
82 if ($request->query->has('keys')) {
|
Chris@0
|
83 $keys = trim($request->query->get('keys'));
|
Chris@0
|
84 $plugin->setSearch($keys, $request->query->all(), $request->attributes->all());
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 $build['#title'] = $plugin->suggestedTitle();
|
Chris@0
|
88 $build['search_form'] = $this->formBuilder()->getForm(SearchPageForm::class, $entity);
|
Chris@0
|
89
|
Chris@0
|
90 // Build search results, if keywords or other search parameters are in the
|
Chris@0
|
91 // GET parameters. Note that we need to try the search if 'keys' is in
|
Chris@0
|
92 // there at all, vs. being empty, due to advanced search.
|
Chris@0
|
93 $results = [];
|
Chris@0
|
94 if ($request->query->has('keys')) {
|
Chris@0
|
95 if ($plugin->isSearchExecutable()) {
|
Chris@0
|
96 // Log the search.
|
Chris@0
|
97 if ($this->config('search.settings')->get('logging')) {
|
Chris@0
|
98 $this->logger->notice('Searched %type for %keys.', ['%keys' => $keys, '%type' => $entity->label()]);
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 // Collect the search results.
|
Chris@0
|
102 $results = $plugin->buildResults();
|
Chris@0
|
103 }
|
Chris@0
|
104 else {
|
Chris@0
|
105 // The search not being executable means that no keywords or other
|
Chris@0
|
106 // conditions were entered.
|
Chris@17
|
107 $this->messenger()->addError($this->t('Please enter some keywords.'));
|
Chris@0
|
108 }
|
Chris@0
|
109 }
|
Chris@0
|
110
|
Chris@0
|
111 if (count($results)) {
|
Chris@0
|
112 $build['search_results_title'] = [
|
Chris@0
|
113 '#markup' => '<h2>' . $this->t('Search results') . '</h2>',
|
Chris@0
|
114 ];
|
Chris@0
|
115 }
|
Chris@0
|
116
|
Chris@0
|
117 $build['search_results'] = [
|
Chris@0
|
118 '#theme' => ['item_list__search_results__' . $plugin->getPluginId(), 'item_list__search_results'],
|
Chris@0
|
119 '#items' => $results,
|
Chris@0
|
120 '#empty' => [
|
Chris@0
|
121 '#markup' => '<h3>' . $this->t('Your search yielded no results.') . '</h3>',
|
Chris@0
|
122 ],
|
Chris@0
|
123 '#list_type' => 'ol',
|
Chris@0
|
124 '#context' => [
|
Chris@0
|
125 'plugin' => $plugin->getPluginId(),
|
Chris@0
|
126 ],
|
Chris@0
|
127 ];
|
Chris@0
|
128
|
Chris@0
|
129 $this->renderer->addCacheableDependency($build, $entity);
|
Chris@0
|
130 if ($plugin instanceof CacheableDependencyInterface) {
|
Chris@0
|
131 $this->renderer->addCacheableDependency($build, $plugin);
|
Chris@0
|
132 }
|
Chris@0
|
133
|
Chris@0
|
134 // If this plugin uses a search index, then also add the cache tag tracking
|
Chris@0
|
135 // that search index, so that cached search result pages are invalidated
|
Chris@0
|
136 // when necessary.
|
Chris@0
|
137 if ($plugin->getType()) {
|
Chris@0
|
138 $build['search_results']['#cache']['tags'][] = 'search_index';
|
Chris@0
|
139 $build['search_results']['#cache']['tags'][] = 'search_index:' . $plugin->getType();
|
Chris@0
|
140 }
|
Chris@0
|
141
|
Chris@0
|
142 $build['pager'] = [
|
Chris@0
|
143 '#type' => 'pager',
|
Chris@0
|
144 ];
|
Chris@0
|
145
|
Chris@0
|
146 return $build;
|
Chris@0
|
147 }
|
Chris@0
|
148
|
Chris@0
|
149 /**
|
Chris@0
|
150 * Creates a render array for the search help page.
|
Chris@0
|
151 *
|
Chris@0
|
152 * @param \Drupal\search\SearchPageInterface $entity
|
Chris@0
|
153 * The search page entity.
|
Chris@0
|
154 *
|
Chris@0
|
155 * @return array
|
Chris@0
|
156 * The search help page.
|
Chris@0
|
157 */
|
Chris@0
|
158 public function searchHelp(SearchPageInterface $entity) {
|
Chris@0
|
159 $build = [];
|
Chris@0
|
160
|
Chris@0
|
161 $build['search_help'] = $entity->getPlugin()->getHelp();
|
Chris@0
|
162
|
Chris@0
|
163 return $build;
|
Chris@0
|
164 }
|
Chris@0
|
165
|
Chris@0
|
166 /**
|
Chris@0
|
167 * Redirects to a search page.
|
Chris@0
|
168 *
|
Chris@0
|
169 * This is used to redirect from /search to the default search page.
|
Chris@0
|
170 *
|
Chris@0
|
171 * @param \Drupal\search\SearchPageInterface $entity
|
Chris@0
|
172 * The search page entity.
|
Chris@0
|
173 *
|
Chris@0
|
174 * @return \Symfony\Component\HttpFoundation\RedirectResponse
|
Chris@0
|
175 * A redirect to the search page.
|
Chris@0
|
176 */
|
Chris@0
|
177 public function redirectSearchPage(SearchPageInterface $entity) {
|
Chris@0
|
178 return $this->redirect('search.view_' . $entity->id());
|
Chris@0
|
179 }
|
Chris@0
|
180
|
Chris@0
|
181 /**
|
Chris@0
|
182 * Route title callback.
|
Chris@0
|
183 *
|
Chris@0
|
184 * @param \Drupal\search\SearchPageInterface $search_page
|
Chris@0
|
185 * The search page entity.
|
Chris@0
|
186 *
|
Chris@0
|
187 * @return string
|
Chris@0
|
188 * The title for the search page edit form.
|
Chris@0
|
189 */
|
Chris@0
|
190 public function editTitle(SearchPageInterface $search_page) {
|
Chris@0
|
191 return $this->t('Edit %label search page', ['%label' => $search_page->label()]);
|
Chris@0
|
192 }
|
Chris@0
|
193
|
Chris@0
|
194 /**
|
Chris@0
|
195 * Performs an operation on the search page entity.
|
Chris@0
|
196 *
|
Chris@0
|
197 * @param \Drupal\search\SearchPageInterface $search_page
|
Chris@0
|
198 * The search page entity.
|
Chris@0
|
199 * @param string $op
|
Chris@0
|
200 * The operation to perform, usually 'enable' or 'disable'.
|
Chris@0
|
201 *
|
Chris@0
|
202 * @return \Symfony\Component\HttpFoundation\RedirectResponse
|
Chris@0
|
203 * A redirect back to the search settings page.
|
Chris@0
|
204 */
|
Chris@0
|
205 public function performOperation(SearchPageInterface $search_page, $op) {
|
Chris@0
|
206 $search_page->$op()->save();
|
Chris@0
|
207
|
Chris@0
|
208 if ($op == 'enable') {
|
Chris@17
|
209 $this->messenger()->addStatus($this->t('The %label search page has been enabled.', ['%label' => $search_page->label()]));
|
Chris@0
|
210 }
|
Chris@0
|
211 elseif ($op == 'disable') {
|
Chris@17
|
212 $this->messenger()->addStatus($this->t('The %label search page has been disabled.', ['%label' => $search_page->label()]));
|
Chris@0
|
213 }
|
Chris@0
|
214
|
Chris@18
|
215 $url = $search_page->toUrl('collection');
|
Chris@0
|
216 return $this->redirect($url->getRouteName(), $url->getRouteParameters(), $url->getOptions());
|
Chris@0
|
217 }
|
Chris@0
|
218
|
Chris@0
|
219 /**
|
Chris@0
|
220 * Sets the search page as the default.
|
Chris@0
|
221 *
|
Chris@0
|
222 * @param \Drupal\search\SearchPageInterface $search_page
|
Chris@0
|
223 * The search page entity.
|
Chris@0
|
224 *
|
Chris@0
|
225 * @return \Symfony\Component\HttpFoundation\RedirectResponse
|
Chris@0
|
226 * A redirect to the search settings page.
|
Chris@0
|
227 */
|
Chris@0
|
228 public function setAsDefault(SearchPageInterface $search_page) {
|
Chris@0
|
229 // Set the default page to this search page.
|
Chris@0
|
230 $this->searchPageRepository->setDefaultSearchPage($search_page);
|
Chris@0
|
231
|
Chris@17
|
232 $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
|
233 return $this->redirect('entity.search_page.collection');
|
Chris@0
|
234 }
|
Chris@0
|
235
|
Chris@0
|
236 }
|