You are here

public function ConfigImporterExporter::writeBackConfig in Configuration development 8

Write a configuration item to files.

Parameters

\Drupal\Core\Config\Config $config: The config object.

array $file_names: The file names to which the configuration should be written.

Return value

string[] An array of the filenames that were written.

File

src/ConfigImporterExporter.php, line 229

Class

ConfigImporterExporter
Imports and exports config.

Namespace

Drupal\config_devel

Code

public function writeBackConfig(Config $config, array $file_names) {
  $written_files = [];
  if ($file_names) {
    $data = $config
      ->get();
    $config_name = $config
      ->getName();
    unset($data['_core']);
    if ($entity_type_id = $this->configManager
      ->getEntityTypeIdByName($config_name)) {
      unset($data['uuid']);
    }

    // Let everyone else have a change to update the exported data.
    $event = new ConfigDevelSaveEvent($file_names, $data);
    $this->eventDispatcher
      ->dispatch(ConfigDevelEvents::SAVE, $event);
    $data = $event
      ->getData();
    $file_names = $event
      ->getFileNames();
    foreach ($file_names as $file_name) {
      try {
        $result = file_put_contents($file_name, (new InstallStorage())
          ->encode($data));
        if ($result !== FALSE) {
          $written_files[] = $file_name;
        }
      } catch (DumpException $e) {

        // Do nothing. What could we do?
      }
    }
  }
  return $written_files;
}