Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\search\Plugin;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
|
Chris@0
|
6 use Drupal\Core\Cache\RefinableCacheableDependencyTrait;
|
Chris@0
|
7 use Drupal\Core\Form\FormStateInterface;
|
Chris@0
|
8 use Drupal\Core\Plugin\PluginBase;
|
Chris@0
|
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
Chris@0
|
10 use Drupal\Component\Utility\Unicode;
|
Chris@0
|
11 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * Defines a base class for plugins wishing to support search.
|
Chris@0
|
15 */
|
Chris@0
|
16 abstract class SearchPluginBase extends PluginBase implements ContainerFactoryPluginInterface, SearchInterface, RefinableCacheableDependencyInterface {
|
Chris@0
|
17
|
Chris@0
|
18 use RefinableCacheableDependencyTrait;
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * The keywords to use in a search.
|
Chris@0
|
22 *
|
Chris@0
|
23 * @var string
|
Chris@0
|
24 */
|
Chris@0
|
25 protected $keywords;
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * Array of parameters from the query string from the request.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @var array
|
Chris@0
|
31 */
|
Chris@0
|
32 protected $searchParameters;
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * Array of attributes - usually from the request object.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @var array
|
Chris@0
|
38 */
|
Chris@0
|
39 protected $searchAttributes;
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * {@inheritdoc}
|
Chris@0
|
43 */
|
Chris@0
|
44 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
|
Chris@0
|
45 return new static($configuration, $plugin_id, $plugin_definition);
|
Chris@0
|
46 }
|
Chris@0
|
47
|
Chris@0
|
48 /**
|
Chris@0
|
49 * {@inheritdoc}
|
Chris@0
|
50 */
|
Chris@0
|
51 public function setSearch($keywords, array $parameters, array $attributes) {
|
Chris@0
|
52 $this->keywords = (string) $keywords;
|
Chris@0
|
53 $this->searchParameters = $parameters;
|
Chris@0
|
54 $this->searchAttributes = $attributes;
|
Chris@0
|
55 return $this;
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 /**
|
Chris@0
|
59 * {@inheritdoc}
|
Chris@0
|
60 */
|
Chris@0
|
61 public function getKeywords() {
|
Chris@0
|
62 return $this->keywords;
|
Chris@0
|
63 }
|
Chris@0
|
64
|
Chris@0
|
65 /**
|
Chris@0
|
66 * {@inheritdoc}
|
Chris@0
|
67 */
|
Chris@0
|
68 public function getParameters() {
|
Chris@0
|
69 return $this->searchParameters;
|
Chris@0
|
70 }
|
Chris@0
|
71
|
Chris@0
|
72 /**
|
Chris@0
|
73 * {@inheritdoc}
|
Chris@0
|
74 */
|
Chris@0
|
75 public function getAttributes() {
|
Chris@0
|
76 return $this->searchAttributes;
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 /**
|
Chris@0
|
80 * {@inheritdoc}
|
Chris@0
|
81 */
|
Chris@0
|
82 public function isSearchExecutable() {
|
Chris@0
|
83 // Default implementation suitable for plugins that only use keywords.
|
Chris@0
|
84 return !empty($this->keywords);
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 /**
|
Chris@0
|
88 * {@inheritdoc}
|
Chris@0
|
89 */
|
Chris@0
|
90 public function getType() {
|
Chris@0
|
91 return NULL;
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@0
|
94 /**
|
Chris@0
|
95 * {@inheritdoc}
|
Chris@0
|
96 */
|
Chris@0
|
97 public function buildResults() {
|
Chris@0
|
98 $results = $this->execute();
|
Chris@0
|
99
|
Chris@0
|
100 $built = [];
|
Chris@0
|
101 foreach ($results as $result) {
|
Chris@0
|
102 $built[] = [
|
Chris@0
|
103 '#theme' => 'search_result',
|
Chris@0
|
104 '#result' => $result,
|
Chris@0
|
105 '#plugin_id' => $this->getPluginId(),
|
Chris@0
|
106 ];
|
Chris@0
|
107 }
|
Chris@0
|
108
|
Chris@0
|
109 return $built;
|
Chris@0
|
110 }
|
Chris@0
|
111
|
Chris@0
|
112 /**
|
Chris@0
|
113 * {@inheritdoc}
|
Chris@0
|
114 */
|
Chris@0
|
115 public function searchFormAlter(array &$form, FormStateInterface $form_state) {
|
Chris@0
|
116 // Empty default implementation.
|
Chris@0
|
117 }
|
Chris@0
|
118
|
Chris@0
|
119 /**
|
Chris@0
|
120 * {@inheritdoc}
|
Chris@0
|
121 */
|
Chris@0
|
122 public function suggestedTitle() {
|
Chris@0
|
123 // If the user entered a search string, truncate it and append it to the
|
Chris@0
|
124 // title.
|
Chris@0
|
125 if (!empty($this->keywords)) {
|
Chris@0
|
126 return $this->t('Search for @keywords', ['@keywords' => Unicode::truncate($this->keywords, 60, TRUE, TRUE)]);
|
Chris@0
|
127 }
|
Chris@0
|
128 // Use the default 'Search' title.
|
Chris@0
|
129 return $this->t('Search');
|
Chris@0
|
130 }
|
Chris@0
|
131
|
Chris@0
|
132 /**
|
Chris@0
|
133 * {@inheritdoc}
|
Chris@0
|
134 */
|
Chris@0
|
135 public function buildSearchUrlQuery(FormStateInterface $form_state) {
|
Chris@0
|
136 // Grab the keywords entered in the form and put them as 'keys' in the GET.
|
Chris@0
|
137 $keys = trim($form_state->getValue('keys'));
|
Chris@0
|
138 $query = ['keys' => $keys];
|
Chris@0
|
139
|
Chris@0
|
140 return $query;
|
Chris@0
|
141 }
|
Chris@0
|
142
|
Chris@0
|
143 /**
|
Chris@0
|
144 * {@inheritdoc}
|
Chris@0
|
145 */
|
Chris@0
|
146 public function getHelp() {
|
Chris@0
|
147 // This default search help is appropriate for plugins like NodeSearch
|
Chris@0
|
148 // that use the SearchQuery class.
|
Chris@0
|
149 $help = [
|
Chris@0
|
150 'list' => [
|
Chris@0
|
151 '#theme' => 'item_list',
|
Chris@0
|
152 '#items' => [
|
Chris@0
|
153 $this->t('Search looks for exact, case-insensitive keywords; keywords shorter than a minimum length are ignored.'),
|
Chris@0
|
154 $this->t('Use upper-case OR to get more results. Example: cat OR dog (content contains either "cat" or "dog").'),
|
Chris@0
|
155 $this->t('You can use upper-case AND to require all words, but this is the same as the default behavior. Example: cat AND dog (same as cat dog, content must contain both "cat" and "dog").'),
|
Chris@0
|
156 $this->t('Use quotes to search for a phrase. Example: "the cat eats mice".'),
|
Chris@0
|
157 $this->t('You can precede keywords by - to exclude them; you must still have at least one "positive" keyword. Example: cat -dog (content must contain cat and cannot contain dog).'),
|
Chris@0
|
158 ],
|
Chris@0
|
159 ],
|
Chris@0
|
160 ];
|
Chris@0
|
161
|
Chris@0
|
162 return $help;
|
Chris@0
|
163 }
|
Chris@0
|
164
|
Chris@0
|
165 }
|