You are here

protected function ConfigRewriter::rewriteDirectoryConfig in Configuration Rewrite 8

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

Finds files in a given directory and uses them to rewrite active config.

Parameters

\Drupal\Core\Extension\Extension $extension: The extension that contains the config rewrites.

string $rewrite_dir: The directory that contains config rewrites.

string $langcode: (Optional) The langcode that this configuration is for, if applicable.

1 call to ConfigRewriter::rewriteDirectoryConfig()
ConfigRewriter::rewriteModuleConfig in src/ConfigRewriter.php
Rewrites configuration for a given module.

File

src/ConfigRewriter.php, line 119

Class

ConfigRewriter
Provides methods to rewrite configuration.

Namespace

Drupal\config_rewrite

Code

protected function rewriteDirectoryConfig($extension, $rewrite_dir, $langcode = NULL) {
  if ($langcode && !$this->languageConfigFactoryOverride) {
    return;
  }

  // Scan the rewrite directory for rewrites.
  if (file_exists($rewrite_dir) && ($files = $this
    ->fileScanDirectory($rewrite_dir, '/^.*\\.yml$/i', [
    'recurse' => FALSE,
  ]))) {
    foreach ($files as $file) {

      // Parse the rewrites and retrieve the original config.
      $rewrite = Yaml::parse(file_get_contents($rewrite_dir . DIRECTORY_SEPARATOR . $file->name . '.yml'));
      if ($langcode) {

        /** @var \Drupal\language\Config\LanguageConfigOverride $original_config */
        $config = $this->languageConfigFactoryOverride
          ->getOverride($langcode, $file->name);
        $original_data = $config
          ->get();
        $rewrite = $this
          ->rewriteConfig($original_data, $rewrite, $file->name, $extension
          ->getName());
      }
      else {
        $config = $this->configFactory
          ->getEditable($file->name);
        $original_data = $config
          ->getRawData();
        $rewrite = $this
          ->rewriteConfig($original_data, $rewrite, $file->name, $extension
          ->getName());
      }

      // Unset 'config_rewrite' key before saving rewritten values.
      if (isset($rewrite['config_rewrite'])) {
        unset($rewrite['config_rewrite']);
      }

      // Retain the original 'uuid' and '_core' keys if it's not explicitly
      // asked to rewrite them.
      if (isset($rewrite['config_rewrite_uuids'])) {
        unset($rewrite['config_rewrite_uuids']);
      }
      else {
        foreach ([
          '_core',
          'uuid',
        ] as $key) {
          if (isset($original_data[$key])) {
            $rewrite[$key] = $original_data[$key];
          }
        }
      }

      // Save the rewritten configuration data.
      $result = $config
        ->setData($rewrite)
        ->save() ? 'rewritten' : 'not rewritten';

      // Log a message indicating whether the config was rewritten or not.
      $log = $langcode ? '@config (@langcode) @result by @module' : '@config @result by @module';
      $this->logger
        ->notice($log, [
        '@config' => $file->name,
        '@result' => $result,
        '@module' => $extension
          ->getName(),
      ]);
    }
  }
}