You are here

public function SimpleSearchForm::buildForm in Simple search form 8

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/SimpleSearchForm.php, line 24

Class

SimpleSearchForm
SimpleSearchForm definition.

Namespace

Drupal\simple_search_form\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, array $config = []) {
  $form['#method'] = 'get';
  $form['#action'] = Url::fromUserInput($config['action_path'])
    ->toString();
  $form['#token'] = FALSE;
  $form[$config['get_parameter']] = [
    '#type' => $config['input_type'],
    '#title' => $config['input_label'],
    '#title_display' => $config['input_label_display'],
    '#attributes' => [
      'placeholder' => $config['input_placeholder'],
      'class' => explode(' ', $config['input_css_classes']),
    ],
    '#default_value' => $config['input_keep_value'] ? $this
      ->getRequest()->query
      ->get($config['get_parameter']) : '',
  ];
  if ($config['input_type'] === 'search_api_autocomplete') {
    $this
      ->setupSearchApiAutocomplete($form, $config);
  }
  if ($config['submit_display']) {
    $form['actions'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'form-actions',
        ],
      ],
    ];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $config['submit_label'],
      // Prevent op from showing up in the query string.
      '#name' => '',
    ];
  }

  // Remove after fix https://www.drupal.org/project/drupal/issues/1191278.
  $form['#after_build'][] = '::cleanupGetParams';
  return $form;
}