You are here

public function ConfigImporterService::importConfigs in Config Importer and Tools 8.2

Same name and namespace in other branches
  1. 8 src/ConfigImporterService.php \Drupal\config_import\ConfigImporterService::importConfigs()
  2. 8.0 src/ConfigImporterService.php \Drupal\config_import\ConfigImporterService::importConfigs()

Import configurations.

@example The next example will import the following configs:

  • /directory/outside/webroot/user.role.authenticated.yml
  • /directory/outside/webroot/user.role.anonymous.yml
$this
  ->importConfigs([
  'user.role.authenticated',
  'user.role.anonymous',
]);

Parameters

string[] $configs: Configurations to import.

Overrides ConfigImporterServiceInterface::importConfigs

File

src/ConfigImporterService.php, line 225

Class

ConfigImporterService
Class ConfigImporterService.

Namespace

Drupal\config_import

Code

public function importConfigs(array $configs) {

  // Stream wrappers are not available during installation.
  $tmp_dir = (defined('MAINTENANCE_MODE') ? '/tmp' : 'temporary:/') . '/confi_' . $this->uuid
    ->generate();
  if (!$this->fileSystem
    ->mkdir($tmp_dir)) {
    throw new ConfigImporterException('Failed to create temporary directory: ' . $tmp_dir);
  }

  // Define temporary storage for our shenanigans.
  $tmp_storage = new FileStorage($tmp_dir);

  // Dump all configurations into temporary directory.
  $this
    ->export($tmp_storage);

  // Overwrite exported configurations by our custom ones.
  foreach ($configs as $config) {
    $file = "{$this->directory}/{$config}.yml";
    if (file_exists($file)) {
      \Drupal::service('file_system')
        ->copy($file, $tmp_dir, FileSystemInterface::EXISTS_REPLACE);
    }
    else {

      // Possibly, config has been exported a little bit above. This could
      // happen if you removed it from disc, but not from database. Export
      // operation will generate it inside of temporary storage and we should
      // take care about this.
      $tmp_storage
        ->delete($config);

      // Remove config if it was specified, but file does not exists.
      $this->configStorage
        ->delete($config);
    }
  }

  // Remove configurations from storage which are not allowed for import.
  $this
    ->filter($tmp_storage);

  // Import changed, just overwritten items, into config storage.
  $this
    ->import($tmp_storage);
}