You are here

protected function SearchEditForm::buildSuggestersForm in Search API Autocomplete 8

Builds a form for the search's suggester plugins.

Parameters

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the (parent) form.

Return value

array A form for selecting and configuring the search's suggesters.

1 call to SearchEditForm::buildSuggestersForm()
SearchEditForm::buildForm in src/Form/SearchEditForm.php
Form constructor.

File

src/Form/SearchEditForm.php, line 194

Class

SearchEditForm
Provides an edit form for autocomplete search entities.

Namespace

Drupal\search_api_autocomplete\Form

Code

protected function buildSuggestersForm(FormStateInterface $form_state) {
  $search = $this->entity;
  $available_suggesters = $this
    ->getAvailableSuggesters();
  $suggester_ids = array_keys($available_suggesters);
  $suggester_weights = $search
    ->getSuggesterWeights();
  $suggester_weights += array_fill_keys($suggester_ids, 0);
  $suggester_limits = $search
    ->getSuggesterLimits();
  $suggester_limits += array_fill_keys($suggester_ids, NULL);
  $form = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Suggester plugins'),
    '#description' => $this
      ->t('Suggester plugins represent methods for creating autocomplete suggestions from user input.'),
  ];
  $form['enabled'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Enabled suggesters'),
    '#description' => $this
      ->t('Choose the suggester plugins to use for creating autocomplete suggestions.'),
    '#options' => [],
    '#required' => TRUE,
    '#default_value' => $search
      ->getSuggesterIds(),
  ];
  $form['weights'] = [
    '#type' => 'table',
  ];
  $form['weights']['#tabledrag'][] = [
    'action' => 'order',
    'relationship' => 'sibling',
    'group' => 'search-api-autocomplete-suggester-weight',
  ];

  // For the "limit" setting we use a sentence. This requires a bit of
  // preparation.
  $sentence = (string) $this
    ->t('At most @num results');
  list($prefix, $suffix) = explode('@num', $sentence);
  $prefix = '<div class="container-inline">' . $prefix;
  $suffix .= '</div>';
  foreach ($available_suggesters as $suggester_id => $suggester) {
    $label = $suggester
      ->label();

    // Option (with additional description) for the "Enabled" checkboxes.
    $form['enabled']['#options'][$suggester_id] = $label;
    $form['enabled'][$suggester_id] = [
      '#description' => $suggester
        ->getDescription(),
    ];
    $states = [
      'visible' => [
        ":input[name=\"suggesters[enabled][{$suggester_id}]\"]" => [
          'checked' => TRUE,
        ],
      ],
    ];

    // Entry in the "Suggester order" table. (Hidden while the suggester is
    // disabled.)
    $form['weights'][$suggester_id] = [
      '#weight' => $suggester_weights[$suggester_id],
      'label' => [
        '#plain_text' => $label,
      ],
      'limit' => [
        '#type' => 'number',
        '#prefix' => $prefix,
        '#suffix' => $suffix,
        '#min' => 1,
        '#step' => 1,
        '#size' => 2,
        '#default_value' => $suggester_limits[$suggester_id],
      ],
      'weight' => [
        '#type' => 'weight',
        '#title' => $this
          ->t('Weight for suggester %label', [
          '%label' => $label,
        ]),
        '#title_display' => 'invisible',
        '#delta' => 50,
        '#default_value' => $suggester_weights[$suggester_id],
        '#attributes' => [
          'class' => [
            'search-api-autocomplete-suggester-weight',
          ],
        ],
      ],
      '#attributes' => [
        'class' => [
          'draggable',
          'js-form-wrapper',
        ],
        'data-drupal-states' => Json::encode($states),
      ],
    ];

    // "Details" container with the suggester config form, if any. (Hidden
    // while the suggester is disabled.)
    if ($suggester instanceof PluginFormInterface) {
      $args = [
        '%label' => $suggester
          ->label(),
      ];
      $form['settings'][$suggester_id] = [
        '#type' => 'details',
        '#title' => $this
          ->t('Configure suggester %label', $args),
        '#states' => $states,
      ];
      $suggester_form_state = SubformState::createForSubform($form['settings'][$suggester_id], $form, $form_state);
      $form['settings'][$suggester_id] += $suggester
        ->buildConfigurationForm($form['settings'][$suggester_id], $suggester_form_state);
    }
  }
  if (!empty($form['weights'])) {
    uasort($form['weights'], [
      SortArray::class,
      'sortByWeightProperty',
    ]);
  }
  return $form;
}