searchPageRepository = $search_page_repository; $this->configFactory = $config_factory; $this->renderer = $renderer; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { return new static( $container->get('search.search_page_repository'), $container->get('config.factory'), $container->get('renderer') ); } /** * {@inheritdoc} */ public function getFormId() { return 'search_block_form'; } /** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { // Set up the form to submit using GET to the correct search page. $entity_id = $this->searchPageRepository->getDefaultSearchPage(); // SearchPageRepository::getDefaultSearchPage() depends on search.settings. // The dependency needs to be added before the conditional return, otherwise // the block would get cached without the necessary cacheablity metadata in // case there is no default search page and would not be invalidated if that // changes. $this->renderer->addCacheableDependency($form, $this->configFactory->get('search.settings')); if (!$entity_id) { $form['message'] = [ '#markup' => $this->t('Search is currently disabled'), ]; return $form; } $route = 'search.view_' . $entity_id; $form['#action'] = $this->url($route); $form['#method'] = 'get'; $form['keys'] = [ '#type' => 'search', '#title' => $this->t('Search'), '#title_display' => 'invisible', '#size' => 15, '#default_value' => '', '#attributes' => ['title' => $this->t('Enter the terms you wish to search for.')], ]; $form['actions'] = ['#type' => 'actions']; $form['actions']['submit'] = [ '#type' => 'submit', '#value' => $this->t('Search'), // Prevent op from showing up in the query string. '#name' => '', ]; return $form; } /** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { // This form submits to the search page, so processing happens there. } }