You are here

public function IndexOverviewForm::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 FormInterface::buildForm

File

src/Form/IndexOverviewForm.php, line 116

Class

IndexOverviewForm
Defines the overview of all search autocompletion configurations.

Namespace

Drupal\search_api_autocomplete\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, IndexInterface $search_api_index = NULL) {
  $form_state
    ->set('index', $search_api_index);
  $index_id = $search_api_index
    ->id();
  $form['#tree'] = TRUE;
  $form['#title'] = $this
    ->t('Manage autocomplete for search index %label', [
    '%label' => $search_api_index
      ->label(),
  ]);

  /** @var \Drupal\search_api_autocomplete\SearchInterface[] $searches_by_plugin */
  $searches_by_plugin = [];
  foreach ($this
    ->loadAutocompleteSearchByIndex($index_id) as $search) {
    $searches_by_plugin[$search
      ->getSearchPluginId()] = $search;
  }
  $used_ids = [];
  $plugins = $this->pluginHelper
    ->createSearchPluginsForIndex($index_id);
  foreach ($plugins as $plugin_id => $plugin) {
    $group_label = (string) $plugin
      ->getGroupLabel();
    if (empty($form[$group_label])) {
      $form[$group_label] = [
        '#type' => 'details',
        '#open' => TRUE,
        '#title' => $plugin
          ->getGroupLabel(),
      ];
      if ($description = $plugin
        ->getGroupDescription()) {
        $form[$group_label]['#description'] = '<p>' . $description . '</p>';
      }
      $form[$group_label]['searches']['#type'] = 'tableselect';
      $form[$group_label]['searches']['#header'] = [
        'label' => $this
          ->t('Label'),
        'operations' => $this
          ->t('Operations'),
      ];
      $form[$group_label]['searches']['#empty'] = '';
      $form[$group_label]['searches']['#js_select'] = TRUE;
    }
    if (isset($searches_by_plugin[$plugin_id])) {
      $search = $searches_by_plugin[$plugin_id];
    }
    else {
      $search = $this
        ->createSearchForPlugin($plugin, $used_ids);
      $used_ids[$search
        ->id()] = TRUE;
    }
    $id = $search
      ->id();
    $form_state
      ->set([
      'searches',
      $id,
    ], $search);
    $form[$group_label]['searches'][$id] = [
      '#type' => 'checkbox',
      '#default_value' => $search
        ->status(),
      '#parents' => [
        'searches',
        $id,
      ],
    ];
    $options =& $form[$group_label]['searches']['#options'][$id];
    $options['label'] = $search
      ->label();
    $items = [];
    if ($search
      ->status()) {
      $items[] = [
        'title' => $this
          ->t('Edit'),
        'url' => $search
          ->toUrl('edit-form'),
      ];
    }
    if (!$search
      ->isNew()) {
      $items[] = [
        'title' => $this
          ->t('Delete'),
        'url' => $search
          ->toUrl('delete-form'),
      ];
    }
    if ($items) {
      $options['operations'] = [
        'data' => [
          '#type' => 'operations',
          '#links' => $items,
        ],
      ];
    }
    else {
      $options['operations'] = '';
    }
    unset($options);
  }
  if (!Element::children($form)) {
    $form['message']['#markup'] = '<p>' . $this
      ->t('There are currently no searches known for this index. You need to create at least one search view.') . '</p>';
  }
  else {
    $form['actions'] = [
      '#type' => 'actions',
    ];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#button_type' => 'primary',
      '#value' => $this
        ->t('Save'),
    ];
  }
  return $form;
}