You are here

public function ConfigParamUpdaterService::update in Config Importer and Tools 8

Same name and namespace in other branches
  1. 8.2 src/ConfigParamUpdaterService.php \Drupal\config_import\ConfigParamUpdaterService::update()

Update configuration param from existing config file.

Parameters

string $config: Config full name with path. Example: drupal_get_path('module', 'test') . '/config/install/test.config.yml'.

string $config_name: Config name. Example: "views.view.who_s_online".

string $param: Identifier to store value in configuration. Example: "dependencies.module".

File

src/ConfigParamUpdaterService.php, line 61

Class

ConfigParamUpdaterService
Class ConfigParamUpdaterService.

Namespace

Drupal\config_import

Code

public function update($config, $config_name, $param) {

  // Get base storage config.
  if (!file_exists($config)) {
    $this->logger
      ->error($this
      ->t('File @file does not exist.', [
      '@file' => $config,
    ]));
    return;
  }
  $storage_config = Yaml::decode(file_get_contents($config));

  // Retrieve a value from a nested array with variable depth.
  $update_value = NestedArray::getValue($storage_config, explode('.', $param));
  if (!$update_value) {
    $this->logger
      ->info($this
      ->t('Param "@param" not exist in config @name.', [
      '@name' => $config_name,
      '@param' => $param,
    ]));
    return;
  }

  // Get active storage config.
  $config_factory = $this->configManager
    ->getConfigFactory();
  $config = $config_factory
    ->getEditable($config_name);
  if ($config
    ->isNew() && empty($config
    ->getOriginal())) {
    $this->logger
      ->error($this
      ->t('Config @name does not exist.', [
      '@name' => $config_name,
    ]));
    return;
  }

  // Update value retrieved from storage config.
  $config
    ->set($param, $update_value);
  $config
    ->save();
  $this->logger
    ->info($this
    ->t('Param "@param" in config @name was updated.', [
    '@name' => $config_name,
    '@param' => $param,
  ]));
}