You are here

public function SearchEditForm::buildForm in Search API Autocomplete 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 EntityForm::buildForm

File

src/Form/SearchEditForm.php, line 94

Class

SearchEditForm
Provides an edit form for autocomplete search entities.

Namespace

Drupal\search_api_autocomplete\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);
  $search = $this->entity;
  $form['#title'] = $this
    ->t('Configure autocompletion for %search', [
    '%search' => $search
      ->label(),
  ]);
  $form['#tree'] = TRUE;
  $form['status'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enabled'),
    '#default_value' => $search
      ->status(),
  ];
  $form['suggesters'] = $this
    ->buildSuggestersForm($form_state);
  if (!$search
    ->hasValidSearchPlugin()) {
    $this->messenger
      ->addError($this
      ->t('No information about this search could be found. Unless this is a temporary problem for some reason, you are advised to delete this search configuration.'));
  }
  else {
    $search_plugin = $search
      ->getSearchPlugin();
    if ($search_plugin instanceof PluginFormInterface) {
      $form['search_settings'] = [];
      $plugin_form_state = SubFormState::createForSubform($form['search_settings'], $form, $form_state);
      $form['search_settings'] = $search_plugin
        ->buildConfigurationForm($form['search_settings'], $plugin_form_state);
    }
  }
  $form['options']['limit'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Maximum number of suggestions'),
    '#description' => $this
      ->t('The maximum number of autocomplete suggestions to display in total.'),
    '#required' => TRUE,
    '#min' => 1,
    '#step' => 1,
    '#default_value' => $search
      ->getOption('limit'),
  ];
  $form['options']['min_length'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Minimum length of keywords for autocompletion'),
    '#description' => $this
      ->t('If the entered keywords are shorter than this, no autocomplete suggestions will be displayed.'),
    '#min' => 1,
    '#step' => 1,
    '#default_value' => $search
      ->getOption('min_length'),
  ];
  $form['options']['show_count'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Display result count estimates'),
    '#description' => $this
      ->t('Display the estimated number of result for each suggestion. This option might not have an effect for some servers or types of suggestion.'),
    '#default_value' => (bool) $search
      ->getOption('show_count'),
  ];
  $form['advanced'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Advanced settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  ];
  $form['advanced']['autosubmit'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable auto-submit'),
    '#description' => $this
      ->t('When enabled, the search form will automatically be submitted when a selection is made by pressing "Enter".'),
    '#default_value' => $search
      ->getOption('autosubmit'),
    '#parents' => [
      'options',
      'autosubmit',
    ],
  ];
  $form['advanced']['submit_button_selector'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Search button selector'),
    '#description' => $this
      ->t('<a href="@jquery_url">jQuery selector</a> identifying the button to use for submitting the search form. Use the ID attribute of the "Search" submit button to prevent issues when another button is present (e.g., "Reset"). The selector is evaluated relative to the form. The default value is "@default".', [
      '@jquery_url' => 'https://api.jquery.com/category/selectors/',
      '@default' => ':submit',
    ]),
    '#default_value' => $search
      ->getOption('submit_button_selector'),
    '#required' => TRUE,
    '#parents' => [
      'options',
      'submit_button_selector',
    ],
    '#states' => [
      'visible' => [
        ':input[name="options[autosubmit]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['advanced']['delay'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Delay (in ms)'),
    '#description' => $this
      ->t('The delay in milliseconds between when a keystroke occurs and when a search is performed. Low values will result in a more responsive experience for users, but can also cause a higher load on the server. Defaults to 300 ms.'),
    '#min' => 0,
    '#step' => 1,
    '#default_value' => $search
      ->getOption('delay'),
    '#parents' => [
      'options',
      'delay',
    ],
  ];
  return $form;
}