You are here

public function IndexForm::buildTrackerConfigForm in Search API 8

Builds the tracker configuration form.

Parameters

array $form: The current form array.

\Drupal\Core\Form\FormStateInterface $form_state: The current form state.

\Drupal\search_api\IndexInterface $index: The index being created or edited.

1 call to IndexForm::buildTrackerConfigForm()
IndexForm::buildEntityForm in src/Form/IndexForm.php
Builds the form for the basic index properties.

File

src/Form/IndexForm.php, line 394

Class

IndexForm
Provides a form for the Index entity.

Namespace

Drupal\search_api\Form

Code

public function buildTrackerConfigForm(array &$form, FormStateInterface $form_state, IndexInterface $index) {
  $selected_tracker = $form_state
    ->getValue('tracker');
  if ($selected_tracker === NULL || $selected_tracker == $index
    ->getTrackerId()) {

    // Initial form build, use the saved tracker (or none for new indexes).
    if ($index
      ->hasValidTracker()) {
      $tracker = $index
        ->getTrackerInstance();
    }
    elseif (!$index
      ->isNew()) {
      $this->messenger
        ->addError($this
        ->t('The tracker plugin is missing or invalid.'));
    }
  }
  else {

    // Probably an AJAX rebuild of the form – use the tracker selected by
    // the user.
    $tracker = $this->pluginHelper
      ->createTrackerPlugin($index, $selected_tracker);
  }
  if (empty($tracker)) {
    return;
  }
  $form_state
    ->set('tracker', $tracker
    ->getPluginId());
  if ($tracker instanceof PluginFormInterface) {

    // Get the "sub-form state" and appropriate form part to send to
    // buildConfigurationForm().
    $tracker_form = $form['tracker_config'] ?? [];
    $tracker_form_state = SubformState::createForSubform($tracker_form, $form, $form_state);
    $form['tracker_config'] = $tracker
      ->buildConfigurationForm($tracker_form, $tracker_form_state);
    $form['tracker_config']['#type'] = 'details';
    $form['tracker_config']['#title'] = $this
      ->t('Configure the %plugin tracker', [
      '%plugin' => $tracker
        ->label(),
    ]);
    $form['tracker_config']['#description'] = Utility::escapeHtml($tracker
      ->getDescription());
    $form['tracker_config']['#open'] = $index
      ->isNew();

    // If the user changed the tracker and the new one has a config form, show
    // a message telling the user to configure it.
    if ($selected_tracker && $selected_tracker != $tracker
      ->getPluginId()) {
      $this->messenger
        ->addWarning($this
        ->t('Please configure the used tracker.'));
    }
  }
}