You are here

public function Config::exportFile in Helper 8

Export a single configuration file.

Parameters

string $config_name: The configuration ID.

string $directory: The directory that the configuration file will be written to.

array $options: An optional array of options.

Return value

bool TRUE if the file was written successfully, otherwise FALSE.

Throws

\RuntimeException If the directory is not writeable or if the configuration does not exist.

1 call to Config::exportFile()
Config::reExportDirectory in src/Config.php
Re-export a directory containing configuration files.

File

src/Config.php, line 168

Class

Config
Provides helper for working with configuration.

Namespace

Drupal\helper

Code

public function exportFile($config_name, $directory, array $options = []) {
  if (!is_writable($directory)) {
    throw new \RuntimeException("The directory {$directory} is not writeable.");
  }
  $config = $this->configFactory
    ->get($config_name);
  $data = $config
    ->getRawData();
  if (empty($data)) {
    throw new \RuntimeException("The config {$config_name} does not exist.");
  }

  // Remove _core property.
  unset($data['_core']);

  // Remove UUIDs from exported config.
  if (!empty($options['remove_uuid']) && isset($data['uuid']) && $this->configManager
    ->getEntityTypeIdByName($config_name)) {
    unset($data['uuid']);
  }

  // Merge in additonal information.
  if (!empty($options['merge']) && is_array($options['merge'])) {
    NestedArray::mergeDeep($data, $options['merge']);
  }
  $uri = $directory . '/' . $config_name . '.yml';
  $this->logger
    ->notice('Exporting @uri.', [
    '@uri' => $uri,
  ]);
  return (bool) file_put_contents($uri, Yaml::encode($data));
}