View source
<?php
namespace Drupal\simple_search_form\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class SimpleSearchForm extends FormBase {
public function getFormId() {
return 'simple_search_form';
}
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'],
'#name' => '',
];
}
$form['#after_build'][] = '::cleanupGetParams';
return $form;
}
public function cleanupGetParams(array $form) {
$form['form_id']['#access'] = FALSE;
$form['form_build_id']['#access'] = FALSE;
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
protected function setupSearchApiAutocomplete(array &$form, array $config) {
$autocomplete_config = $config['search_api_autocomplete'];
$arguments = explode(',', $autocomplete_config['arguments']);
$arguments = array_map('trim', $arguments);
$form[$config['get_parameter']]['#search_id'] = $autocomplete_config['search_id'];
$form[$config['get_parameter']]['#additional_data'] = [
'filter' => $autocomplete_config['filter'],
'display' => $autocomplete_config['display'],
'arguments' => $arguments,
];
}
}