You are here

public function ConfigImporterExporter::importConfig in Configuration development 8

Imports a config item from the given filename.

Parameters

string $filename: The filename to import from with the Drupal-relative path.

string $original_hash: (optional) The original hash. TODO: document this properly!

string $contents: (optional) The file contents. The file will be read if omitted.

Return value

string|null The new hash of the config, or NULL if there was no need to import because the config hash was identical.

File

src/ConfigImporterExporter.php, line 175

Class

ConfigImporterExporter
Imports and exports config.

Namespace

Drupal\config_devel

Code

public function importConfig($filename, $original_hash = '', $contents = '') {
  $hash = '';
  if (!$contents && !($contents = @file_get_contents($filename))) {
    return $hash;
  }
  $needs_import = TRUE;
  $hash = Crypt::hashBase64($contents);
  if ($original_hash) {
    if ($hash == $original_hash) {
      $needs_import = FALSE;
    }
  }
  if ($needs_import) {
    $data = (new InstallStorage())
      ->decode($contents);
    $config_name = basename($filename, '.yml');
    $source_storage = new StorageReplaceDataWrapper($this->configStorage);
    $source_storage
      ->replaceData($config_name, $data);
    $storage_comparer = new StorageComparer($source_storage, $this->configStorage);
    $storage_comparer
      ->createChangelist();

    // TODO: simplify this when
    // https://www.drupal.org/project/drupal/issues/3123491 is fixed.
    $config_importer = new ConfigImporter($storage_comparer, $this->eventDispatcher, $this->configManager, $this->persistentLockBackend, $this->typedConfigManager, $this->moduleHandler, $this->moduleInstaller, $this->themeHandler, $this->stringTranslation, $this->moduleExtensionList);
    $config_importer
      ->import();
    return $hash;
  }
}