You are here

public function IndexForm::validateForm in Search API 8

Form validation handler.

Parameters

array $form: An associative array containing the structure of the form.

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

Overrides FormBase::validateForm

File

src/Form/IndexForm.php, line 502

Class

IndexForm
Provides a form for the Index entity.

Namespace

Drupal\search_api\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);

  /** @var \Drupal\search_api\IndexInterface $index */
  $index = $this
    ->getEntity();
  $storage = $this->entityTypeManager
    ->getStorage('search_api_index');
  if (!$index
    ->isNew()) {
    $this->originalEntity = $storage
      ->loadUnchanged($index
      ->id());
  }
  if (empty($this->originalEntity)) {
    $this->originalEntity = $storage
      ->create([
      'status' => FALSE,
    ]);
  }

  // Store the array of datasource plugin IDs with integer keys.
  $datasource_ids = array_values(array_filter($form_state
    ->getValue('datasources', [])));
  $form_state
    ->setValue('datasources', $datasource_ids);

  // Call validateConfigurationForm() for each enabled datasource with a form.
  $datasources = $this->pluginHelper
    ->createDatasourcePlugins($index, $datasource_ids);
  $previous_datasources = $form_state
    ->get('datasources');
  foreach ($datasources as $datasource_id => $datasource) {
    if ($datasource instanceof PluginFormInterface) {
      if (!in_array($datasource_id, $previous_datasources)) {
        $form_state
          ->setRebuild();
        continue;
      }
      $datasource_form =& $form['datasource_configs'][$datasource_id];
      $datasource_form_state = SubformState::createForSubform($datasource_form, $form, $form_state);
      $datasource
        ->validateConfigurationForm($datasource_form, $datasource_form_state);
    }
  }

  // Call validateConfigurationForm() for the (possibly new) tracker, if it
  // has not changed and if it has a form.
  $tracker_id = $form_state
    ->getValue('tracker', NULL);
  if ($tracker_id == $form_state
    ->get('tracker')) {
    $tracker = $this->pluginHelper
      ->createTrackerPlugin($index, $tracker_id);
    if ($tracker instanceof PluginFormInterface) {
      $tracker_form_state = SubformState::createForSubform($form['tracker_config'], $form, $form_state);
      $tracker
        ->validateConfigurationForm($form['tracker_config'], $tracker_form_state);
    }
  }
  else {
    $tracker = $this->pluginHelper
      ->createTrackerPlugin($this->originalEntity, $tracker_id);
    if ($tracker instanceof PluginFormInterface) {
      $form_state
        ->setRebuild();
    }
  }
}