ConfigParamUpdaterService.php in Config Importer and Tools 8.2
File
src/ConfigParamUpdaterService.php
View source
<?php
namespace Drupal\config_import;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class ConfigParamUpdaterService {
use StringTranslationTrait;
protected $configManager;
protected $logger;
public function __construct(ConfigManagerInterface $config_manager, LoggerChannelFactoryInterface $logger_factory) {
$this->configManager = $config_manager;
$this->logger = $logger_factory
->get('config_update');
}
public function update($config, $config_name, $param) {
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));
$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;
}
$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;
}
$config
->set($param, $update_value);
$config
->save();
$this->logger
->info($this
->t('Param "@param" in config @name was updated.', [
'@name' => $config_name,
'@param' => $param,
]));
}
}