3 namespace Drupal\search\Plugin;
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;
14 * Defines a base class for plugins wishing to support search.
16 abstract class SearchPluginBase extends PluginBase implements ContainerFactoryPluginInterface, SearchInterface, RefinableCacheableDependencyInterface {
18 use RefinableCacheableDependencyTrait;
21 * The keywords to use in a search.
28 * Array of parameters from the query string from the request.
32 protected $searchParameters;
35 * Array of attributes - usually from the request object.
39 protected $searchAttributes;
44 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
45 return new static($configuration, $plugin_id, $plugin_definition);
51 public function setSearch($keywords, array $parameters, array $attributes) {
52 $this->keywords = (string) $keywords;
53 $this->searchParameters = $parameters;
54 $this->searchAttributes = $attributes;
61 public function getKeywords() {
62 return $this->keywords;
68 public function getParameters() {
69 return $this->searchParameters;
75 public function getAttributes() {
76 return $this->searchAttributes;
82 public function isSearchExecutable() {
83 // Default implementation suitable for plugins that only use keywords.
84 return !empty($this->keywords);
90 public function getType() {
97 public function buildResults() {
98 $results = $this->execute();
101 foreach ($results as $result) {
103 '#theme' => 'search_result',
104 '#result' => $result,
105 '#plugin_id' => $this->getPluginId(),
115 public function searchFormAlter(array &$form, FormStateInterface $form_state) {
116 // Empty default implementation.
122 public function suggestedTitle() {
123 // If the user entered a search string, truncate it and append it to the
125 if (!empty($this->keywords)) {
126 return $this->t('Search for @keywords', ['@keywords' => Unicode::truncate($this->keywords, 60, TRUE, TRUE)]);
128 // Use the default 'Search' title.
129 return $this->t('Search');
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];
146 public function getHelp() {
147 // This default search help is appropriate for plugins like NodeSearch
148 // that use the SearchQuery class.
151 '#theme' => 'item_list',
153 $this->t('Search looks for exact, case-insensitive keywords; keywords shorter than a minimum length are ignored.'),
154 $this->t('Use upper-case OR to get more results. Example: cat OR dog (content contains either "cat" or "dog").'),
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").'),
156 $this->t('Use quotes to search for a phrase. Example: "the cat eats mice".'),
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).'),