Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
namespace Drupal\uw_ct_service\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\path_alias\AliasManager;
use Drupal\Core\Messenger\MessengerInterface;
/**
* Class ServiceSearchForm.
*
* Form for searching services.
*/
class ServiceSearchForm extends FormBase {
/**
* Entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The variable for current path.
*
* @var \Drupal\Core\Path\CurrentPathStack
*/
protected $currentPath;
/**
* The variable for route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* The variable for alias manager.
*
* @var \Drupal\path_alias\AliasManager
*/
protected $aliasManager;
/**
* The Messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* Class constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
* @param \Drupal\Core\Path\CurrentPathStack $currentPath
* The currentPath.
* @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch
* The routeMatch.
* @param \Drupal\path_alias\AliasManager $aliasManager
* The aliasManager.
* @param Drupal\Core\Messenger\MessengerInterface $messenger
* The Drupal messenger.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager, CurrentPathStack $currentPath, RouteMatchInterface $routeMatch, AliasManager $aliasManager, MessengerInterface $messenger) {
$this->entityTypeManager = $entityTypeManager;
$this->currentPath = $currentPath;
$this->routeMatch = $routeMatch;
$this->aliasManager = $aliasManager;
$this->messenger = $messenger;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
// Instantiates this form class.
return new static(
$container->get('entity_type.manager'),
$container->get('path.current'),
$container->get('current_route_match'),
$container->get('path_alias.manager'),
$container->get('messenger')
);
}
/**
* Returns a unique string identifying the form.
*
* The returned ID should be a unique string that can be a valid PHP function
* name, since it's used in hook implementation names such as
* hook_form_FORM_ID_alter().
*
* @return string
* The unique string identifying the form.
*/
public function getFormId(): string {
return 'service_search_form';
}
/**
* Form constructor.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param int $tid
* The term id.
* @param string $description
* The description for the input element.
* @param bool $use_placeholder
* A flag to use a placeholder.
* @param string $placeholder
* The placeholder string.
*
* @return array
* The form structure.
*/
public function buildForm(array $form, FormStateInterface $form_state, int $tid = NULL, string $description = NULL, bool $use_placeholder = TRUE, string $placeholder = NULL): array {
// Get the current route.
$route = $this->routeMatch->getRouteName();
// If the route is the all services page, set placeholder to
// search with all services.
if ($route == 'view.uw_view_services.services_page') {
// Set the placeholder for all services.
$placeholder = 'Search within all services';
}
// If this is a service term page, then set placeholder to
// search within <service_name>.
elseif ($route == 'entity.taxonomy_term.canonical') {
// Load the term.
$term = $this->routeMatch->getParameter('taxonomy_term');
// Ensure that we are going to use a placeholder then set it.
if ($use_placeholder && !$placeholder) {
// Set the placeholder for specific catalog.
$placeholder = 'Search within ' . $term->label();
}
}
// If this is a node page, we need to set the tid in the form
// state so that it can be used later to load name and path.
elseif ($route == 'entity.node.canonical') {
// If we are to use a placeholder, set it.
if ($use_placeholder && !$placeholder) {
// Load the term in from the tid.
$term = $this->entityTypeManager->getStorage('taxonomy_term')->load($tid);
// Set the placeholder.
$placeholder = 'Search within ' . $term->label();
}
// Set the tid in the form state.
$form_state->set('tid', $tid);
}
// If this is any other route mainly:
// /services/<service_name>/<new,category,audience, etc...>,
// Then we have to get the tid from the URL, but ensure that
// we are not in layout builder or error will be thrown.
else {
// Ensure that we are not on a layout builder page.
// We do not need the tid on a layout builder page, because
// this form can never get submitted on a layout builder page.
if (strpos($route, 'layout_builder') !== 0) {
// Get the current path.
$url = $this->currentPath->getPath();
// Break the path into parts.
$url = explode('/', $url);
// The tid will always be the 4th element.
$tid = $url[3] ?? NULL;
// One last check to ensure that we have a tid.
// If we do not have a tid, throw a Drupal error.
if (is_numeric($tid)) {
// If we are to use a placeholder, set it.
if ($use_placeholder && !$placeholder) {
// Load the term in from the tid.
$term = $this->entityTypeManager->getStorage('taxonomy_term')->load($tid);
// Set the placeholder.
$placeholder = 'Search within ' . $term->label();
}
// Set the tid into the form state, so it can be used
// in the form submit.
$form_state->set('tid', $url[3]);
}
}
}
// The search input.
$form['search_input'] = [
'#type' => 'textfield',
'#placeholder' => $use_placeholder ? $placeholder : NULL,
'#description' => $description ?? NULL,
'#required' => TRUE,
];
// Add a submit button that handles the submission of the form.
$form['submit'] = [
'#type' => 'submit',
'#value' => '',
'#attributes' => ['aria-label' => 'search'],
];
return $form;
}
/**
* Form submission handler.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Get the values of the form state.
$values = $form_state->getValues();
// Get the tid from form state, if there is not one
// it will be NULL.
$tid = $form_state->get('tid');
// This is an internal URL.
$url = 'internal:';
// If the tid is NULL, we are on a /catalogs page, so we
// can just build the url from the current route.
// If tid is not NULL, we have to determine if this is a
// search all catalogs or specific and set things accordingly.
if ($tid === NULL) {
// Get the current route as the view for catalog
// search will handle all and specific, based on URL.
$url .= Url::fromRoute('<current>')->toString();
}
else {
// If the tid is 0, this is a search all catalogs,
// so set the URL to the /services.
// If not set the search to /services/<service_path>.
if ($tid == 0) {
$url .= '/services';
}
else {
$url .= $this->aliasManager->getAliasByPath('/taxonomy/term/' . $tid);
}
}
// Add the search string.
$url .= '/search?search-input=' . $values['search_input'];
// Get a URL object.
$url = Url::fromUri($url);
// Set the form redirection URL.
$form_state->setRedirectUrl($url);
}
}