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