LanguageConfigFactoryOverride.php in Drupal 9
File
core/modules/language/src/Config/LanguageConfigFactoryOverride.php
View source
<?php
namespace Drupal\language\Config;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\ConfigCollectionInfo;
use Drupal\Core\Config\ConfigCrudEvent;
use Drupal\Core\Config\ConfigFactoryOverrideBase;
use Drupal\Core\Config\ConfigRenameEvent;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Language\LanguageDefault;
use Drupal\Core\Language\LanguageInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class LanguageConfigFactoryOverride extends ConfigFactoryOverrideBase implements LanguageConfigFactoryOverrideInterface, EventSubscriberInterface {
use LanguageConfigCollectionNameTrait;
protected $baseStorage;
protected $storages;
protected $typedConfigManager;
protected $eventDispatcher;
protected $language;
public function __construct(StorageInterface $storage, EventDispatcherInterface $event_dispatcher, TypedConfigManagerInterface $typed_config, LanguageDefault $default_language) {
$this->baseStorage = $storage;
$this->eventDispatcher = $event_dispatcher;
$this->typedConfigManager = $typed_config;
$this->language = $default_language
->get();
}
public function loadOverrides($names) {
if ($this->language) {
$storage = $this
->getStorage($this->language
->getId());
return $storage
->readMultiple($names);
}
return [];
}
public function getOverride($langcode, $name) {
$storage = $this
->getStorage($langcode);
$data = $storage
->read($name);
$override = new LanguageConfigOverride($name, $storage, $this->typedConfigManager, $this->eventDispatcher);
if (!empty($data)) {
$override
->initWithData($data);
}
return $override;
}
public function getStorage($langcode) {
if (!isset($this->storages[$langcode])) {
$this->storages[$langcode] = $this->baseStorage
->createCollection($this
->createConfigCollectionName($langcode));
}
return $this->storages[$langcode];
}
public function getCacheSuffix() {
return $this->language ? $this->language
->getId() : NULL;
}
public function getLanguage() {
return $this->language;
}
public function setLanguage(LanguageInterface $language = NULL) {
$this->language = $language;
return $this;
}
public function installLanguageOverrides($langcode) {
$config_installer = \Drupal::service('config.installer');
$config_installer
->installCollectionDefaultConfig($this
->createConfigCollectionName($langcode));
}
public function createConfigObject($name, $collection = StorageInterface::DEFAULT_COLLECTION) {
$langcode = $this
->getLangcodeFromCollectionName($collection);
return $this
->getOverride($langcode, $name);
}
public function addCollections(ConfigCollectionInfo $collection_info) {
foreach (\Drupal::languageManager()
->getLanguages() as $language) {
$collection_info
->addCollection($this
->createConfigCollectionName($language
->getId()), $this);
}
}
public function onConfigSave(ConfigCrudEvent $event) {
$config = $event
->getConfig();
$name = $config
->getName();
foreach (\Drupal::languageManager()
->getLanguages() as $language) {
$config_translation = $this
->getOverride($language
->getId(), $name);
if (!$config_translation
->isNew()) {
$this
->filterOverride($config, $config_translation);
}
}
}
public function onConfigRename(ConfigRenameEvent $event) {
$config = $event
->getConfig();
$name = $config
->getName();
$old_name = $event
->getOldName();
foreach (\Drupal::languageManager()
->getLanguages() as $language) {
$config_translation = $this
->getOverride($language
->getId(), $old_name);
if (!$config_translation
->isNew()) {
$saved_config = $config_translation
->get();
$storage = $this
->getStorage($language
->getId());
$storage
->write($name, $saved_config);
$config_translation
->delete();
}
}
}
public function onConfigDelete(ConfigCrudEvent $event) {
$config = $event
->getConfig();
$name = $config
->getName();
foreach (\Drupal::languageManager()
->getLanguages() as $language) {
$config_translation = $this
->getOverride($language
->getId(), $name);
if (!$config_translation
->isNew()) {
$config_translation
->delete();
}
}
}
public function getCacheableMetadata($name) {
$metadata = new CacheableMetadata();
if ($this->language) {
$metadata
->setCacheContexts([
'languages:language_interface',
]);
}
return $metadata;
}
}