function ConfigSingleImportForm::submitForm

Form submission 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 FormInterface::submitForm

File

core/modules/config/src/Form/ConfigSingleImportForm.php, line 407

Class

ConfigSingleImportForm
Provides a form for importing a single configuration file.

Namespace

Drupal\config\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  // If this form has not yet been confirmed, store the values and rebuild.
  if (!$this->data) {
    $form_state->setRebuild();
    $this->data = $form_state->getValues();
    return;
  }
  /** @var \Drupal\Core\Config\ConfigImporter $config_importer */
  $config_importer = $form_state->get('config_importer');
  if ($config_importer->alreadyImporting()) {
    $this->messenger()
      ->addError($this->t('Another request may be importing configuration already.'));
  }
  else {
    try {
      $sync_steps = $config_importer->initialize();
      $batch = [
        'operations' => [],
        'finished' => [
          ConfigImporterBatch::class,
          'finish',
        ],
        'title' => $this->t('Importing configuration'),
        'init_message' => $this->t('Starting configuration import.'),
        'progress_message' => $this->t('Completed @current step of @total.'),
        'error_message' => $this->t('Configuration import has encountered an error.'),
      ];
      foreach ($sync_steps as $sync_step) {
        $batch['operations'][] = [
          [
            ConfigImporterBatch::class,
            'process',
          ],
          [
            $config_importer,
            $sync_step,
          ],
        ];
      }
      batch_set($batch);
    } catch (ConfigImporterException $e) {
      // There are validation errors.
      $this->messenger()
        ->addError($this->t('The configuration import failed for the following reasons:'));
      foreach ($config_importer->getErrors() as $message) {
        $this->messenger()
          ->addError($message);
      }
    }
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.