0d8a34e2ea3b2bce050c148879c54a3b33de7240
[yaffs-website] / web / core / modules / search / src / Form / SearchPageForm.php
1 <?php
2
3 namespace Drupal\search\Form;
4
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Url;
8 use Drupal\search\SearchPageInterface;
9
10 /**
11  * Provides a search form for site wide search.
12  *
13  * Search plugins can define method searchFormAlter() to alter the form. If they
14  * have additional or substitute fields, they will need to override the form
15  * submit, making sure to redirect with a GET parameter of 'keys' included, to
16  * trigger the search being processed by the controller, and adding in any
17  * additional query parameters they need to execute search.
18  *
19  * @internal
20  */
21 class SearchPageForm extends FormBase {
22
23   /**
24    * The search page entity.
25    *
26    * @var \Drupal\search\SearchPageInterface
27    */
28   protected $entity;
29
30   /**
31    * {@inheritdoc}
32    */
33   public function getFormId() {
34     return 'search_form';
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   public function buildForm(array $form, FormStateInterface $form_state, SearchPageInterface $search_page = NULL) {
41     $this->entity = $search_page;
42
43     $plugin = $this->entity->getPlugin();
44     $form_state->set('search_page_id', $this->entity->id());
45
46     $form['basic'] = [
47       '#type' => 'container',
48       '#attributes' => [
49         'class' => ['container-inline'],
50       ],
51     ];
52     $form['basic']['keys'] = [
53       '#type' => 'search',
54       '#title' => $this->t('Enter your keywords'),
55       '#default_value' => $plugin->getKeywords(),
56       '#size' => 30,
57       '#maxlength' => 255,
58     ];
59
60     // processed_keys is used to coordinate keyword passing between other forms
61     // that hook into the basic search form.
62     $form['basic']['processed_keys'] = [
63       '#type' => 'value',
64       '#value' => '',
65     ];
66     $form['basic']['submit'] = [
67       '#type' => 'submit',
68       '#value' => $this->t('Search'),
69     ];
70
71     $form['help_link'] = [
72       '#type' => 'link',
73       '#url' => new Url('search.help_' . $this->entity->id()),
74       '#title' => $this->t('Search help'),
75       '#options' => ['attributes' => ['class' => 'search-help-link']],
76     ];
77
78     // Allow the plugin to add to or alter the search form.
79     $plugin->searchFormAlter($form, $form_state);
80     return $form;
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public function submitForm(array &$form, FormStateInterface $form_state) {
87     // Redirect to the search page with keywords in the GET parameters.
88     // Plugins with additional search parameters will need to provide their
89     // own form submit handler to replace this, so they can put their values
90     // into the GET as well. If so, make sure to put 'keys' into the GET
91     // parameters so that the search results generation is triggered.
92     $query = $this->entity->getPlugin()->buildSearchUrlQuery($form_state);
93     $route = 'search.view_' . $form_state->get('search_page_id');
94     $form_state->setRedirect(
95       $route,
96       [],
97       ['query' => $query]
98     );
99   }
100
101 }