You are here

protected function ConfigRewriter::rewriteDirectoryConfig in Configuration Rewrite 8.2

Same name and namespace in other branches
  1. 8 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 118

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 */
        $original_config = $this->languageConfigFactoryOverride
          ->getOverride($langcode, $file->name);
        $rewrite = $this
          ->rewriteConfig($original_config
          ->get(), $rewrite);
      }
      else {
        $original_config = $this->configFactory
          ->getEditable($file->name);
        $rewrite = $this
          ->rewriteConfig($original_config
          ->getRawData(), $rewrite);
      }

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

      // Save the rewritten configuration data.
      $result = $original_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(),
      ]);
    }
  }
}