SearchBlockForm.php

Same filename in other branches
  1. 9 core/modules/search/src/Form/SearchBlockForm.php
  2. 8.9.x core/modules/search/src/Form/SearchBlockForm.php
  3. 10 core/modules/search/src/Form/SearchBlockForm.php

Namespace

Drupal\search\Form

File

core/modules/search/src/Form/SearchBlockForm.php

View source
<?php

namespace Drupal\search\Form;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\WorkspaceSafeFormInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Url;
use Drupal\search\SearchPageRepositoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Builds the search form for the search block.
 *
 * @internal
 */
class SearchBlockForm extends FormBase implements WorkspaceSafeFormInterface {
    
    /**
     * The search page repository.
     *
     * @var \Drupal\search\SearchPageRepositoryInterface
     */
    protected $searchPageRepository;
    
    /**
     * The config factory.
     *
     * @var \Drupal\Core\Config\ConfigFactoryInterface
     */
    protected $configFactory;
    
    /**
     * The renderer.
     *
     * @var \Drupal\Core\Render\RendererInterface
     */
    protected $renderer;
    
    /**
     * Constructs a new SearchBlockForm.
     *
     * @param \Drupal\search\SearchPageRepositoryInterface $search_page_repository
     *   The search page repository.
     * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
     *   The config factory.
     * @param \Drupal\Core\Render\RendererInterface $renderer
     *   The renderer.
     */
    public function __construct(SearchPageRepositoryInterface $search_page_repository, ConfigFactoryInterface $config_factory, RendererInterface $renderer) {
        $this->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, $entity_id = NULL) {
        // Set up the form to submit using GET to the correct search page.
        if (!$entity_id) {
            $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 cacheability 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'] = Url::fromRoute($route)->toString();
        $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.
    }

}

Classes

Title Deprecated Summary
SearchBlockForm Builds the search form for the search block.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.