SearchBlockForm::buildForm

public SearchBlockForm::buildForm(array $form, FormStateInterface $form_state)

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 75

Class

SearchBlockForm
Builds the search form for the search block.

Namespace

Drupal\search\Form

Code

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();
  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(
    '#type' => 'search',
    '#title' => $this->t('Search'),
    '#title_display' => 'invisible',
    '#size' => 15,
    '#default_value' => '',
    '#attributes' => array('title' => $this->t('Enter the terms you wish to search for.')),
  );

  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => $this->t('Search'),
    // Prevent op from showing up in the query string.
    '#name' => '',
  );

  // SearchPageRepository::getDefaultSearchPage() depends on search.settings.
  $this->renderer->addCacheableDependency($form, $this->configFactory->get('search.settings'));

  return $form;
}
doc_Drupal
2016-10-29 09:40:11
Comments
Leave a Comment

Please login to continue.