function ConfigSingleImportForm::submitForm

Same name in other branches
  1. 8.9.x core/modules/config/src/Form/ConfigSingleImportForm.php \Drupal\config\Form\ConfigSingleImportForm::submitForm()
  2. 10 core/modules/config/src/Form/ConfigSingleImportForm.php \Drupal\config\Form\ConfigSingleImportForm::submitForm()
  3. 11.x core/modules/config/src/Form/ConfigSingleImportForm.php \Drupal\config\Form\ConfigSingleImportForm::submitForm()

Overrides FormInterface::submitForm

File

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

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);
            }
        }
    }
}

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