e46195baeaa2b877f03090e58dac4b77debb4589
[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 class SearchPageForm extends FormBase {
20
21   /**
22    * The search page entity.
23    *
24    * @var \Drupal\search\SearchPageInterface
25    */
26   protected $entity;
27
28   /**
29    * {@inheritdoc}
30    */
31   public function getFormId() {
32     return 'search_form';
33   }
34
35   /**
36    * {@inheritdoc}
37    */
38   public function buildForm(array $form, FormStateInterface $form_state, SearchPageInterface $search_page = NULL) {
39     $this->entity = $search_page;
40
41     $plugin = $this->entity->getPlugin();
42     $form_state->set('search_page_id', $this->entity->id());
43
44     $form['basic'] = [
45       '#type' => 'container',
46       '#attributes' => [
47         'class' => ['container-inline'],
48       ],
49     ];
50     $form['basic']['keys'] = [
51       '#type' => 'search',
52       '#title' => $this->t('Enter your keywords'),
53       '#default_value' => $plugin->getKeywords(),
54       '#size' => 30,
55       '#maxlength' => 255,
56     ];
57
58     // processed_keys is used to coordinate keyword passing between other forms
59     // that hook into the basic search form.
60     $form['basic']['processed_keys'] = [
61       '#type' => 'value',
62       '#value' => '',
63     ];
64     $form['basic']['submit'] = [
65       '#type' => 'submit',
66       '#value' => $this->t('Search'),
67     ];
68
69     $form['help_link'] = [
70       '#type' => 'link',
71       '#url' => new Url('search.help_' . $this->entity->id()),
72       '#title' => $this->t('Search help'),
73       '#options' => ['attributes' => ['class' => 'search-help-link']],
74     ];
75
76     // Allow the plugin to add to or alter the search form.
77     $plugin->searchFormAlter($form, $form_state);
78     return $form;
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function submitForm(array &$form, FormStateInterface $form_state) {
85     // Redirect to the search page with keywords in the GET parameters.
86     // Plugins with additional search parameters will need to provide their
87     // own form submit handler to replace this, so they can put their values
88     // into the GET as well. If so, make sure to put 'keys' into the GET
89     // parameters so that the search results generation is triggered.
90     $query = $this->entity->getPlugin()->buildSearchUrlQuery($form_state);
91     $route = 'search.view_' . $form_state->get('search_page_id');
92     $form_state->setRedirect(
93       $route,
94       [],
95       ['query' => $query]
96     );
97   }
98
99 }