Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / search / tests / modules / search_extra_type / src / Plugin / Search / SearchExtraTypeSearch.php
1 <?php
2
3 namespace Drupal\search_extra_type\Plugin\Search;
4
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Routing\UrlGeneratorTrait;
8 use Drupal\Core\Url;
9 use Drupal\search\Plugin\ConfigurableSearchPluginBase;
10
11 /**
12  * Executes a dummy keyword search.
13  *
14  * @SearchPlugin(
15  *   id = "search_extra_type_search",
16  *   title = @Translation("Dummy search type")
17  * )
18  */
19 class SearchExtraTypeSearch extends ConfigurableSearchPluginBase {
20
21   use UrlGeneratorTrait;
22
23   /**
24    * {@inheritdoc}
25    */
26   public function setSearch($keywords, array $parameters, array $attributes) {
27     if (empty($parameters['search_conditions'])) {
28       $parameters['search_conditions'] = '';
29     }
30     parent::setSearch($keywords, $parameters, $attributes);
31   }
32
33   /**
34    * Verifies if the given parameters are valid enough to execute a search for.
35    *
36    * @return bool
37    *   TRUE if there are keywords or search conditions in the query.
38    */
39   public function isSearchExecutable() {
40     return (bool) ($this->keywords || !empty($this->searchParameters['search_conditions']));
41   }
42
43   /**
44    * Execute the search.
45    *
46    * This is a dummy search, so when search "executes", we just return a dummy
47    * result containing the keywords and a list of conditions.
48    *
49    * @return array
50    *   A structured list of search results
51    */
52   public function execute() {
53     $results = [];
54     if (!$this->isSearchExecutable()) {
55       return $results;
56     }
57     return [
58       [
59         'link' => Url::fromRoute('test_page_test.test_page')->toString(),
60         'type' => 'Dummy result type',
61         'title' => 'Dummy title',
62         'snippet' => SafeMarkup::format("Dummy search snippet to display. Keywords: @keywords\n\nConditions: @search_parameters", ['@keywords' => $this->keywords, '@search_parameters' => print_r($this->searchParameters, TRUE)]),
63       ],
64     ];
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public function buildResults() {
71     $results = $this->execute();
72     $output['prefix']['#markup'] = '<h2>Test page text is here</h2> <ol class="search-results">';
73
74     foreach ($results as $entry) {
75       $output[] = [
76         '#theme' => 'search_result',
77         '#result' => $entry,
78         '#plugin_id' => 'search_extra_type_search',
79       ];
80     }
81     $pager = [
82       '#type' => 'pager',
83     ];
84     $output['suffix']['#markup'] = '</ol>' . \Drupal::service('renderer')->render($pager);
85
86     return $output;
87   }
88
89   /**
90    * {@inheritdoc}
91    */
92   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
93     // Output form for defining rank factor weights.
94     $form['extra_type_settings'] = [
95       '#type' => 'fieldset',
96       '#title' => t('Extra type settings'),
97       '#tree' => TRUE,
98     ];
99
100     $form['extra_type_settings']['boost'] = [
101       '#type' => 'select',
102       '#title' => t('Boost method'),
103       '#options' => [
104         'bi' => t('Bistromathic'),
105         'ii' => t('Infinite Improbability'),
106       ],
107       '#default_value' => $this->configuration['boost'],
108     ];
109     return $form;
110   }
111
112   /**
113    * {@inheritdoc}
114    */
115   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
116     $this->configuration['boost'] = $form_state->getValue(['extra_type_settings', 'boost']);
117   }
118
119   /**
120    * {@inheritdoc}
121    */
122   public function defaultConfiguration() {
123     return [
124       'boost' => 'bi',
125     ];
126   }
127
128 }