3 namespace Drupal\search\Form;
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\search\SearchPageInterface;
11 * Provides a search form for site wide search.
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.
21 class SearchPageForm extends FormBase {
24 * The search page entity.
26 * @var \Drupal\search\SearchPageInterface
33 public function getFormId() {
40 public function buildForm(array $form, FormStateInterface $form_state, SearchPageInterface $search_page = NULL) {
41 $this->entity = $search_page;
43 $plugin = $this->entity->getPlugin();
44 $form_state->set('search_page_id', $this->entity->id());
47 '#type' => 'container',
49 'class' => ['container-inline'],
52 $form['basic']['keys'] = [
54 '#title' => $this->t('Enter your keywords'),
55 '#default_value' => $plugin->getKeywords(),
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'] = [
66 $form['basic']['submit'] = [
68 '#value' => $this->t('Search'),
71 $form['help_link'] = [
73 '#url' => new Url('search.help_' . $this->entity->id()),
74 '#title' => $this->t('Search help'),
75 '#options' => ['attributes' => ['class' => 'search-help-link']],
78 // Allow the plugin to add to or alter the search form.
79 $plugin->searchFormAlter($form, $form_state);
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(