You are here

class ConfigImportEventSubscriber in Config Ignore Keys 8

Class ConfigImportEventSubscriber.

@package Drupal\config_ignore_keys\EventSubscriber

Hierarchy

Expanded class hierarchy of ConfigImportEventSubscriber

1 string reference to 'ConfigImportEventSubscriber'
config_ignore_keys.services.yml in ./config_ignore_keys.services.yml
config_ignore_keys.services.yml
1 service uses ConfigImportEventSubscriber
config_ignore_keys.config_import_event_subscriber in ./config_ignore_keys.services.yml
Drupal\config_ignore_keys\EventSubscriber\ConfigImportEventSubscriber

File

src/EventSubscriber/ConfigImportEventSubscriber.php, line 19

Namespace

Drupal\config_ignore_keys\EventSubscriber
View source
class ConfigImportEventSubscriber implements EventSubscriberInterface {
  use StringTranslationTrait;

  /**
   * The state service.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * The configuration factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The config key ignore plugin manager.
   *
   * @var \Drupal\config_ignore_keys\Plugin\Manager\ConfigurationIgnorePluginManager
   */
  protected $pluginManager;

  /**
   * List of all plugins defined.
   *
   * @var array
   */
  protected $plugins;

  /**
   * The language override factory.
   *
   * @var \Drupal\language\Config\LanguageConfigFactoryOverride
   */
  protected $languageConfigFactoryOverride;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * ConfigImportEventSubscriber constructor.
   *
   * @param \Drupal\Core\State\StateInterface $state
   *   The state service.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The config factory.
   * @param \Drupal\config_ignore_keys\Plugin\Manager\ConfigurationIgnorePluginManager $pluginManager
   *   The plugin manager.
   * @param \Drupal\Core\Language\LanguageManager $languageManager
   *   The language manager.
   * @param \Drupal\language\Config\LanguageConfigFactoryOverride $languageConfigFactoryOverride
   *   The language config override factory.
   */
  public function __construct(StateInterface $state, ConfigFactoryInterface $configFactory, ConfigurationIgnorePluginManager $pluginManager, LanguageManager $languageManager, LanguageConfigFactoryOverride $languageConfigFactoryOverride) {
    $this->state = $state;
    $this->configFactory = $configFactory;
    $this->pluginManager = $pluginManager;
    $this->languageManager = $languageManager;
    $this->languageConfigFactoryOverride = $languageConfigFactoryOverride;
    $this->plugins = $pluginManager
      ->getDefinitions();
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[ConfigEvents::IMPORT_VALIDATE][] = [
      'onConfigValidate',
      20,
    ];
    $events[ConfigEvents::IMPORT][] = [
      'onConfigImport',
      20,
    ];
    return $events;
  }

  /**
   * Reacts to configuration validation.
   *
   * Fetches the  plugins containing the needed configurations to be ignored and
   * saves in state their initial value.
   */
  public function onConfigValidate() {
    foreach ($this->plugins as $plugin) {
      foreach ($this->pluginManager
        ->createInstance($plugin['id'])
        ->getConfigurations() as $configFile => $keys) {
        if (is_array($keys)) {
          foreach ($keys as $configKey) {
            $this
              ->saveStateWithConfig($configFile, $configKey);
          }
        }
        else {
          $this
            ->saveStateWithConfig($configFile, $configFile);
        }
      }
    }
  }

  /**
   * Saves the old configuration for the ignored config keys.
   */
  public function onConfigImport() {
    foreach ($this->plugins as $plugin) {
      foreach ($this->pluginManager
        ->createInstance($plugin['id'])
        ->getConfigurations() as $configFile => $keys) {
        if (is_array($keys)) {
          foreach ($keys as $configKey) {
            $this
              ->saveConfigFromState($configFile, $configKey);
          }
        }
        else {
          $this
            ->saveConfigFileFromState($configFile, $configFile);
        }
      }
    }
  }

  /**
   * Fetches the key data from the file and saves it into state.
   *
   * @param string $configFile
   *   The config file name.
   * @param string $configKey
   *   The configuration key which needs to be ignored.
   */
  private function saveStateWithConfig($configFile, $configKey) {
    $config = $this->configFactory
      ->getEditable($configFile);
    foreach ($this->languageManager
      ->getLanguages() as $language) {
      $config_translation = $this->languageConfigFactoryOverride
        ->getOverride($language
        ->getId(), $configFile);
      if (!$config_translation
        ->isNew()) {
        $data = $config_translation
          ->get($configKey);
        if (isset($data)) {
          $this->state
            ->set($language
            ->getId() . $configFile . $configKey, $data);
        }
      }
    }
    if (!empty($config) && NULL !== ($config = $config
      ->get($configKey))) {
      $this->state
        ->set($configFile . $configKey, $config);
    }
  }

  /**
   * Fetches the state variable defined by key/file and saves it.
   *
   * @param string $configFile
   *   The config file name.
   * @param string $configKey
   *   The configuration key which needs to be ignored.
   */
  private function saveConfigFromState($configFile, $configKey) {
    foreach ($this->languageManager
      ->getLanguages() as $language) {
      $stateValue = $stateValue = $this->state
        ->get($language
        ->getId() . $configFile . $configKey);
      if (isset($stateValue)) {
        $config_translation = $this->languageConfigFactoryOverride
          ->getOverride($language
          ->getId(), $configFile);
        $config_translation
          ->set($configKey, $stateValue);
        $config_translation
          ->save();
      }
    }
    $stateValue = $this->state
      ->get($configFile . $configKey);
    if (isset($stateValue)) {
      $config = $this->configFactory
        ->getEditable($configFile);
      if (!empty($config)) {
        $config
          ->set($configKey, $stateValue);
        $config
          ->save();
      }
    }
  }

  /**
   * Fetches the state variable defined by key/file and saves it.
   *
   * @param string $configFile
   *   The config file name.
   * @param string $configKey
   *   The configuration key which needs to be ignored.
   */
  private function saveConfigFileFromState($configFile, $configKey) {
    foreach ($this->languageManager
      ->getLanguages() as $language) {
      $stateValue = $stateValue = $this->state
        ->get($language
        ->getId() . $configFile . $configKey);
      if (isset($stateValue)) {
        $config_translation = $this->languageConfigFactoryOverride
          ->getOverride($language
          ->getId(), $configFile);
        $config_translation
          ->setData($stateValue);
        $config_translation
          ->save();
      }
    }
    $stateValue = $this->state
      ->get($configFile . $configKey);
    if (isset($stateValue)) {
      $config = $this->configFactory
        ->getEditable($configFile);
      if (!empty($config)) {
        $config
          ->setData($stateValue);
        $config
          ->save();
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigImportEventSubscriber::$configFactory protected property The configuration factory service.
ConfigImportEventSubscriber::$languageConfigFactoryOverride protected property The language override factory.
ConfigImportEventSubscriber::$languageManager protected property The language manager.
ConfigImportEventSubscriber::$pluginManager protected property The config key ignore plugin manager.
ConfigImportEventSubscriber::$plugins protected property List of all plugins defined.
ConfigImportEventSubscriber::$state protected property The state service.
ConfigImportEventSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
ConfigImportEventSubscriber::onConfigImport public function Saves the old configuration for the ignored config keys.
ConfigImportEventSubscriber::onConfigValidate public function Reacts to configuration validation.
ConfigImportEventSubscriber::saveConfigFileFromState private function Fetches the state variable defined by key/file and saves it.
ConfigImportEventSubscriber::saveConfigFromState private function Fetches the state variable defined by key/file and saves it.
ConfigImportEventSubscriber::saveStateWithConfig private function Fetches the key data from the file and saves it into state.
ConfigImportEventSubscriber::__construct public function ConfigImportEventSubscriber 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.