You are here

public function ConfigRewriter::rewriteConfig in Configuration Rewrite 8

Same name and namespace in other branches
  1. 8.2 src/ConfigRewriter.php \Drupal\config_rewrite\ConfigRewriter::rewriteConfig()

Parameters

array $original_config:

array $rewrite:

string $config_name:

string $extensionName:

Return value

array

Throws

\Drupal\config_rewrite\Exception\NonexistentInitialConfigException

Overrides ConfigRewriterInterface::rewriteConfig

1 call to ConfigRewriter::rewriteConfig()
ConfigRewriter::rewriteDirectoryConfig in src/ConfigRewriter.php
Finds files in a given directory and uses them to rewrite active config.

File

src/ConfigRewriter.php, line 179

Class

ConfigRewriter
Provides methods to rewrite configuration.

Namespace

Drupal\config_rewrite

Code

public function rewriteConfig($original_config, $rewrite, $config_name, $extensionName) {
  if (empty($original_config)) {
    $message = 'Tried to rewrite config @config via @module module without initial config present.';
    $this->logger
      ->error($message, [
      '@config' => $config_name,
      '@module' => $extensionName,
    ]);
    throw new NonexistentInitialConfigException("Tried to rewrite config {$config_name} by {$extensionName} module without initial config.");
  }
  if (isset($rewrite['config_rewrite']) && $rewrite['config_rewrite'] == 'replace') {
    $rewritten_config = $rewrite;
  }
  else {
    $rewritten_config = NestedArray::mergeDeep($original_config, $rewrite);
  }
  if (isset($rewrite['config_rewrite']['replace']) && is_array($rewrite['config_rewrite']['replace'])) {
    foreach ($rewrite['config_rewrite']['replace'] as $key) {
      $parts = explode('.', $key);
      $key_exists = NULL;
      $value = NestedArray::getValue($rewrite, $parts, $key_exists);
      if ($key_exists) {
        NestedArray::setValue($rewritten_config, $parts, $value, TRUE);
      }
      else {
        NestedArray::unsetValue($rewritten_config, $parts);
      }
    }
  }
  return $rewritten_config;
}