SearchBlockForm.php 3.42 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\search\Form;

5
use Drupal\Core\Config\ConfigFactoryInterface;
6
use Drupal\Core\Form\FormBase;
7
use Drupal\Core\Form\FormStateInterface;
8
use Drupal\Core\Render\RendererInterface;
9 10
use Drupal\search\SearchPageRepositoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
11 12 13 14

/**
 * Builds the search form for the search block.
 */
15
class SearchBlockForm extends FormBase {
16

17 18 19 20 21 22 23
  /**
   * The search page repository.
   *
   * @var \Drupal\search\SearchPageRepositoryInterface
   */
  protected $searchPageRepository;

24 25 26 27 28 29 30 31 32 33 34 35 36 37
  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The renderer.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

38 39 40 41 42
  /**
   * Constructs a new SearchBlockForm.
   *
   * @param \Drupal\search\SearchPageRepositoryInterface $search_page_repository
   *   The search page repository.
43 44
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
45
   * @param \Drupal\Core\Render\RendererInterface $renderer
46
   *   The renderer.
47
   */
48
  public function __construct(SearchPageRepositoryInterface $search_page_repository, ConfigFactoryInterface $config_factory, RendererInterface $renderer) {
49
    $this->searchPageRepository = $search_page_repository;
50 51
    $this->configFactory = $config_factory;
    $this->renderer = $renderer;
52 53 54 55 56 57 58
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
59 60 61
      $container->get('search.search_page_repository'),
      $container->get('config.factory'),
      $container->get('renderer')
62 63 64
    );
  }

65 66 67
  /**
   * {@inheritdoc}
   */
68
  public function getFormId() {
69 70 71 72 73 74
    return 'search_block_form';
  }

  /**
   * {@inheritdoc}
   */
75
  public function buildForm(array $form, FormStateInterface $form_state) {
76 77
    // Set up the form to submit using GET to the correct search page.
    $entity_id = $this->searchPageRepository->getDefaultSearchPage();
78 79 80 81 82 83 84 85 86 87

    $form = [];

    // 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'));

88 89 90 91 92 93 94 95 96 97 98 99
    if (!$entity_id) {
      $form['message'] = array(
        '#markup' => $this->t('Search is currently disabled'),
      );
      return $form;
    }

    $route = 'search.view_' . $entity_id;
    $form['#action'] = $this->url($route);
    $form['#method'] = 'get';

    $form['keys'] = array(
100
      '#type' => 'search',
101
      '#title' => $this->t('Search'),
102 103 104
      '#title_display' => 'invisible',
      '#size' => 15,
      '#default_value' => '',
105
      '#attributes' => array('title' => $this->t('Enter the terms you wish to search for.')),
106
    );
107

108
    $form['actions'] = array('#type' => 'actions');
109 110 111 112 113 114
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => $this->t('Search'),
      // Prevent op from showing up in the query string.
      '#name' => '',
    );
115 116 117 118 119 120 121

    return $form;
  }

  /**
   * {@inheritdoc}
   */
122
  public function submitForm(array &$form, FormStateInterface $form_state) {
123
    // This form submits to the search page, so processing happens there.
124
  }
125

126
}