You are here

public function ConfigSingleImportForm::submitForm in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/config/src/Form/ConfigSingleImportForm.php \Drupal\config\Form\ConfigSingleImportForm::submitForm()
  2. 9 core/modules/config/src/Form/ConfigSingleImportForm.php \Drupal\config\Form\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 399

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_builder = (new BatchBuilder())
        ->setTitle($this
        ->t('Importing configuration'))
        ->setFinishCallback([
        ConfigImporterBatch::class,
        'finish',
      ])
        ->setInitMessage($this
        ->t('Starting configuration import.'))
        ->setProgressMessage($this
        ->t('Completed @current step of @total.'))
        ->setErrorMessage($this
        ->t('Configuration import has encountered an error.'));
      foreach ($sync_steps as $sync_step) {
        $batch_builder
          ->addOperation([
          ConfigImporterBatch::class,
          'process',
        ], [
          $config_importer,
          $sync_step,
        ]);
      }
      batch_set($batch_builder
        ->toArray());
    } 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);
      }
    }
  }
}