You are here

class ConfigParamUpdaterService in Config Importer and Tools 8

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

Class ConfigParamUpdaterService.

Hierarchy

Expanded class hierarchy of ConfigParamUpdaterService

1 string reference to 'ConfigParamUpdaterService'
config_import.services.yml in ./config_import.services.yml
config_import.services.yml
1 service uses ConfigParamUpdaterService
config_import.param_updater in ./config_import.services.yml
Drupal\config_import\ConfigParamUpdaterService

File

src/ConfigParamUpdaterService.php, line 14

Namespace

Drupal\config_import
View source
class ConfigParamUpdaterService {
  use StringTranslationTrait;

  /**
   * ConfigManager definition.
   *
   * @var ConfigManagerInterface
   */
  protected $configManager;

  /**
   * The logger factory.
   *
   * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
   */
  protected $logger;

  /**
   * ConfigImporterService constructor.
   *
   * @param ConfigManagerInterface $config_manager
   *   ConfigManager.
   * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
   *   The logger factory.
   */
  public function __construct(ConfigManagerInterface $config_manager, LoggerChannelFactoryInterface $logger_factory) {
    $this->configManager = $config_manager;
    $this->logger = $logger_factory
      ->get('config_update');
  }

  /**
   * Update configuration param from existing config file.
   *
   * @param string $config
   *   Config full name with path.
   *   Example:
   *   drupal_get_path('module', 'test') . '/config/install/test.config.yml'.
   * @param string $config_name
   *   Config name.
   *   Example: "views.view.who_s_online".
   * @param string $param
   *   Identifier to store value in configuration.
   *   Example: "dependencies.module".
   */
  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,
    ]));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigParamUpdaterService::$configManager protected property ConfigManager definition.
ConfigParamUpdaterService::$logger protected property The logger factory.
ConfigParamUpdaterService::update public function Update configuration param from existing config file.
ConfigParamUpdaterService::__construct public function ConfigImporterService constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.