You are here

public function FederatedSearchPageBlockForm::buildForm in Search API Federated Solr 8

Same name and namespace in other branches
  1. 8.3 src/Form/FederatedSearchPageBlockForm.php \Drupal\search_api_federated_solr\Form\FederatedSearchPageBlockForm::buildForm()
  2. 8.2 src/Form/FederatedSearchPageBlockForm.php \Drupal\search_api_federated_solr\Form\FederatedSearchPageBlockForm::buildForm()
  3. 4.x src/Form/FederatedSearchPageBlockForm.php \Drupal\search_api_federated_solr\Form\FederatedSearchPageBlockForm::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

src/Form/FederatedSearchPageBlockForm.php, line 30
Contains \Drupal\search_api_solr_federated\Form\FederatedSearchPageBlockForm.

Class

FederatedSearchPageBlockForm
Class FederatedSearchPageForm.

Namespace

Drupal\search_api_federated_solr\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $renderer = \Drupal::service('renderer');
  $app_config = \Drupal::config('search_api_federated_solr.search_app.settings');
  $form['search'] = [
    '#type' => 'search',
    '#name' => 'search',
    '#title' => $this
      ->t('Search'),
    '#title_display' => 'invisible',
    '#size' => 15,
    '#default_value' => '',
    '#attributes' => [
      'title' => $this
        ->t('Enter the terms you wish to search for.'),
      'placeholder' => 'Search',
    ],
    '#prefix' => '<div class="container-inline">',
  ];
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Search'),
    '#name' => '',
    '#suffix' => '</div>',
  ];

  // Send site name as qs param if app is configured to load w/default site.
  if ($app_config
    ->get('facet.site_name.set_default')) {
    $search_index = $app_config
      ->get('index.id');
    $index_config = \Drupal::config('search_api.index.' . $search_index);
    $site_name = $index_config
      ->get('field_settings.site_name.configuration.site_name');
    $form['sm_site_name'] = [
      '#type' => 'hidden',
      '#name' => 'sm_site_name',
      '#default_value' => $site_name,
    ];

    // Ensure that this form's render cache is invalidated when search app
    // config is updated.
    $renderer
      ->addCacheableDependency($form, $index_config);
  }
  $form['#action'] = $this
    ->getUrlGenerator()
    ->generateFromRoute('search_api_federated_solr.search');
  $form['#method'] = 'get';

  // Ensure that this form's render cache is invalidated when search app
  // config is updated.
  $renderer
    ->addCacheableDependency($form, $app_config);
  return $form;
}