You are here

public function IndexForm::buildDatasourcesConfigForm in Search API 8

Builds the configuration forms for all selected datasources.

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::buildDatasourcesConfigForm()
IndexForm::buildEntityForm in src/Form/IndexForm.php
Builds the form for the basic index properties.

File

src/Form/IndexForm.php, line 344

Class

IndexForm
Provides a form for the Index entity.

Namespace

Drupal\search_api\Form

Code

public function buildDatasourcesConfigForm(array &$form, FormStateInterface $form_state, IndexInterface $index) {
  $selected_datasources = $form_state
    ->getValue('datasources');
  if ($selected_datasources === NULL) {

    // Initial form build, use the saved datasources (or none for new
    // indexes).
    $datasources = $index
      ->getDatasources();
  }
  else {

    // The form is being rebuilt – use the datasources selected by the user
    // instead of the ones saved in the config.
    $datasources = $this->pluginHelper
      ->createDatasourcePlugins($index, $selected_datasources);
  }
  $form_state
    ->set('datasources', array_keys($datasources));
  $show_message = FALSE;
  foreach ($datasources as $datasource_id => $datasource) {
    if ($datasource instanceof PluginFormInterface) {

      // Get the "sub-form state" and appropriate form part to send to
      // buildConfigurationForm().
      $datasource_form = [];
      if (!empty($form['datasource_configs'][$datasource_id])) {
        $datasource_form = $form['datasource_configs'][$datasource_id];
      }
      $datasource_form_state = SubformState::createForSubform($datasource_form, $form, $form_state);
      $form['datasource_configs'][$datasource_id] = $datasource
        ->buildConfigurationForm($datasource_form, $datasource_form_state);
      $show_message = TRUE;
      $form['datasource_configs'][$datasource_id]['#type'] = 'details';
      $form['datasource_configs'][$datasource_id]['#title'] = $this
        ->t('Configure the %datasource datasource', [
        '%datasource' => $datasource
          ->label(),
      ]);
      $form['datasource_configs'][$datasource_id]['#open'] = $index
        ->isNew();
    }
  }

  // If the user changed the datasources and there is at least one datasource
  // config form, show a message telling the user to configure it.
  if ($selected_datasources && $show_message) {
    $this->messenger
      ->addWarning($this
      ->t('Please configure the used datasources.'));
  }
}