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