You are here

public function SearchBlockForm::buildForm in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/search/src/Form/SearchBlockForm.php \Drupal\search\Form\SearchBlockForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

core/modules/search/src/Form/SearchBlockForm.php, line 78

Class

SearchBlockForm
Builds the search form for the search block.

Namespace

Drupal\search\Form

Code

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;
}