-
Notifications
You must be signed in to change notification settings - Fork 0
/
os2web_search.module
67 lines (56 loc) · 2.31 KB
/
os2web_search.module
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
<?php
use Drupal\Core\Url;
use Drupal\node\Entity\Node;
use Drupal\search_api\Query\QueryInterface;
use Drupal\search_api\Query\ResultSetInterface;
use Drupal\search_api\Utility\Utility;
use Solarium\QueryType\Select\Result\Result;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* Implements hook_form_FORM_ID_alter().
*
* Updates placeholder.
*/
function os2web_search_form_views_exposed_form_alter(array &$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
switch ($form['#id']) {
case 'views-exposed-form-os2web-search-os2web-search-page':
// Setting placeholder and adding class.
$form['sq']['#attributes']['placeholder'] = $form['#info']['filter-search_api_fulltext']['label'];
$form['sq']['#attributes']['class'][] = 'search-input__input';
// Cleaning the filter labels.
unset($form['#info']['filter-search_api_fulltext']['label']);
// Removing text value and adding class.
$form['actions']['submit']['#value'] = '';
$form['actions']['submit']['#attributes']['class'][] = 'search-input__button';
break;
}
}
/**
* Implements hook_search_api_solr_search_results_alter().
*
* If this is exclusive search and we have only one result, perfoming redirect
* to a selected node.
*/
function os2web_search_search_api_solr_search_results_alter(ResultSetInterface $result_set, QueryInterface $query, Result $result) {
if ($query->getOption('solr_param_exclusive') == 'true' && $result->count() == 1) {
$resultItems = $result_set->getResultItems();
/** @var \Drupal\search_api\Item\Item $item */
$item = array_pop($resultItems);
// Item is stored like "entity:node/2:da".
$itemId = $item->getId();
// getting raw_id like "2:da".
list(, $raw_id) = Utility::splitCombinedId($item->getId());
// getting $nodeId as numeric value.
list($nodeId, ) = Utility::splitPropertyPath($raw_id);
$node = Node::load($nodeId);
// Performing redirect to this node.
$response = new RedirectResponse($node->toUrl()->toString());
$request = \Drupal::request();
// Save the session so things like messages get saved.
$request->getSession()->save();
$response->prepare($request);
// Make sure to trigger kernel events.
\Drupal::service('kernel')->terminate($request, $response);
$response->send();
}
}