3 namespace Drupal\search_extra_type\Plugin\Search;
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Routing\UrlGeneratorTrait;
9 use Drupal\search\Plugin\ConfigurableSearchPluginBase;
12 * Executes a dummy keyword search.
15 * id = "search_extra_type_search",
16 * title = @Translation("Dummy search type")
19 class SearchExtraTypeSearch extends ConfigurableSearchPluginBase {
21 use UrlGeneratorTrait;
26 public function setSearch($keywords, array $parameters, array $attributes) {
27 if (empty($parameters['search_conditions'])) {
28 $parameters['search_conditions'] = '';
30 parent::setSearch($keywords, $parameters, $attributes);
34 * Verifies if the given parameters are valid enough to execute a search for.
37 * TRUE if there are keywords or search conditions in the query.
39 public function isSearchExecutable() {
40 return (bool) ($this->keywords || !empty($this->searchParameters['search_conditions']));
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.
50 * A structured list of search results
52 public function execute() {
54 if (!$this->isSearchExecutable()) {
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)]),
70 public function buildResults() {
71 $results = $this->execute();
72 $output['prefix']['#markup'] = '<h2>Test page text is here</h2> <ol class="search-results">';
74 foreach ($results as $entry) {
76 '#theme' => 'search_result',
78 '#plugin_id' => 'search_extra_type_search',
84 $output['suffix']['#markup'] = '</ol>' . \Drupal::service('renderer')->render($pager);
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'),
100 $form['extra_type_settings']['boost'] = [
102 '#title' => t('Boost method'),
104 'bi' => t('Bistromathic'),
105 'ii' => t('Infinite Improbability'),
107 '#default_value' => $this->configuration['boost'],
115 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
116 $this->configuration['boost'] = $form_state->getValue(['extra_type_settings', 'boost']);
122 public function defaultConfiguration() {