ConfigImportEventSubscriber.php in Config Ignore Keys 8
File
src/EventSubscriber/ConfigImportEventSubscriber.php
View source
<?php
namespace Drupal\config_ignore_keys\EventSubscriber;
use Drupal\Core\Config\ConfigEvents;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Language\LanguageManager;
use Drupal\Core\State\StateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\config_ignore_keys\Plugin\Manager\ConfigurationIgnorePluginManager;
use Drupal\language\Config\LanguageConfigFactoryOverride;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ConfigImportEventSubscriber implements EventSubscriberInterface {
use StringTranslationTrait;
protected $state;
protected $configFactory;
protected $pluginManager;
protected $plugins;
protected $languageConfigFactoryOverride;
protected $languageManager;
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();
}
public static function getSubscribedEvents() {
$events[ConfigEvents::IMPORT_VALIDATE][] = [
'onConfigValidate',
20,
];
$events[ConfigEvents::IMPORT][] = [
'onConfigImport',
20,
];
return $events;
}
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);
}
}
}
}
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);
}
}
}
}
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);
}
}
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();
}
}
}
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();
}
}
}
}