class LanguageConfigFactoryOverride

Same name in other branches
  1. 8.9.x core/modules/language/src/Config/LanguageConfigFactoryOverride.php \Drupal\language\Config\LanguageConfigFactoryOverride
  2. 10 core/modules/language/src/Config/LanguageConfigFactoryOverride.php \Drupal\language\Config\LanguageConfigFactoryOverride
  3. 11.x core/modules/language/src/Config/LanguageConfigFactoryOverride.php \Drupal\language\Config\LanguageConfigFactoryOverride

Provides language overrides for the configuration factory.

Hierarchy

Expanded class hierarchy of LanguageConfigFactoryOverride

1 string reference to 'LanguageConfigFactoryOverride'
language.services.yml in core/modules/language/language.services.yml
core/modules/language/language.services.yml
1 service uses LanguageConfigFactoryOverride
language.config_factory_override in core/modules/language/language.services.yml
Drupal\language\Config\LanguageConfigFactoryOverride

File

core/modules/language/src/Config/LanguageConfigFactoryOverride.php, line 20

Namespace

Drupal\language\Config
View source
class LanguageConfigFactoryOverride extends ConfigFactoryOverrideBase implements LanguageConfigFactoryOverrideInterface, EventSubscriberInterface {
    use LanguageConfigCollectionNameTrait;
    
    /**
     * The configuration storage.
     *
     * Do not access this directly. Should be accessed through self::getStorage()
     * so that the cache of storages per langcode is used.
     *
     * @var \Drupal\Core\Config\StorageInterface
     */
    protected $baseStorage;
    
    /**
     * An array of configuration storages keyed by langcode.
     *
     * @var \Drupal\Core\Config\StorageInterface[]
     */
    protected $storages;
    
    /**
     * The typed config manager.
     *
     * @var \Drupal\Core\Config\TypedConfigManagerInterface
     */
    protected $typedConfigManager;
    
    /**
     * An event dispatcher instance to use for configuration events.
     *
     * @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface
     */
    protected $eventDispatcher;
    
    /**
     * The language object used to override configuration data.
     *
     * @var \Drupal\Core\Language\LanguageInterface
     */
    protected $language;
    
    /**
     * Constructs the LanguageConfigFactoryOverride object.
     *
     * @param \Drupal\Core\Config\StorageInterface $storage
     *   The configuration storage engine.
     * @param \Symfony\Contracts\EventDispatcher\EventDispatcherInterface $event_dispatcher
     *   An event dispatcher instance to use for configuration events.
     * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config
     *   The typed configuration manager.
     * @param \Drupal\Core\Language\LanguageDefault $default_language
     *   The default 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;
        // Prior to negotiation the override language should be the default
        // language.
        $this->language = $default_language->get();
    }
    
    /**
     * {@inheritdoc}
     */
    public function loadOverrides($names) {
        if ($this->language) {
            $storage = $this->getStorage($this->language
                ->getId());
            return $storage->readMultiple($names);
        }
        return [];
    }
    
    /**
     * {@inheritdoc}
     */
    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;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getStorage($langcode) {
        if (!isset($this->storages[$langcode])) {
            $this->storages[$langcode] = $this->baseStorage
                ->createCollection($this->createConfigCollectionName($langcode));
        }
        return $this->storages[$langcode];
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCacheSuffix() {
        return $this->language ? $this->language
            ->getId() : NULL;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getLanguage() {
        return $this->language;
    }
    
    /**
     * {@inheritdoc}
     */
    public function setLanguage(LanguageInterface $language = NULL) {
        $this->language = $language;
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function installLanguageOverrides($langcode) {
        
        /** @var \Drupal\Core\Config\ConfigInstallerInterface $config_installer */
        $config_installer = \Drupal::service('config.installer');
        $config_installer->installCollectionDefaultConfig($this->createConfigCollectionName($langcode));
    }
    
    /**
     * {@inheritdoc}
     */
    public function createConfigObject($name, $collection = StorageInterface::DEFAULT_COLLECTION) {
        $langcode = $this->getLangcodeFromCollectionName($collection);
        return $this->getOverride($langcode, $name);
    }
    
    /**
     * {@inheritdoc}
     */
    public function addCollections(ConfigCollectionInfo $collection_info) {
        foreach (\Drupal::languageManager()->getLanguages() as $language) {
            $collection_info->addCollection($this->createConfigCollectionName($language->getId()), $this);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    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);
            }
        }
    }
    
    /**
     * {@inheritdoc}
     */
    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();
            }
        }
    }
    
    /**
     * {@inheritdoc}
     */
    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();
            }
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCacheableMetadata($name) {
        $metadata = new CacheableMetadata();
        if ($this->language) {
            $metadata->setCacheContexts([
                'languages:language_interface',
            ]);
        }
        return $metadata;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ConfigFactoryOverrideBase::filterNestedArray protected function Filters data in nested arrays.
ConfigFactoryOverrideBase::filterOverride protected function Filters data in the override based on what is currently in configuration.
ConfigFactoryOverrideBase::getSubscribedEvents public static function
LanguageConfigCollectionNameTrait::createConfigCollectionName protected function Creates a configuration collection name based on a language code.
LanguageConfigCollectionNameTrait::getLangcodeFromCollectionName protected function Converts a configuration collection name to a language code.
LanguageConfigFactoryOverride::$baseStorage protected property The configuration storage.
LanguageConfigFactoryOverride::$eventDispatcher protected property An event dispatcher instance to use for configuration events.
LanguageConfigFactoryOverride::$language protected property The language object used to override configuration data.
LanguageConfigFactoryOverride::$storages protected property An array of configuration storages keyed by langcode.
LanguageConfigFactoryOverride::$typedConfigManager protected property The typed config manager.
LanguageConfigFactoryOverride::addCollections public function Reacts to the ConfigEvents::COLLECTION_INFO event. Overrides ConfigFactoryOverrideBase::addCollections
LanguageConfigFactoryOverride::createConfigObject public function Creates a configuration object for use during install and synchronization. Overrides ConfigFactoryOverrideInterface::createConfigObject
LanguageConfigFactoryOverride::getCacheableMetadata public function Gets the cacheability metadata associated with the config factory override. Overrides ConfigFactoryOverrideInterface::getCacheableMetadata
LanguageConfigFactoryOverride::getCacheSuffix public function The string to append to the configuration static cache name. Overrides ConfigFactoryOverrideInterface::getCacheSuffix
LanguageConfigFactoryOverride::getLanguage public function Gets the language object used to override configuration data. Overrides LanguageConfigFactoryOverrideInterface::getLanguage
LanguageConfigFactoryOverride::getOverride public function Get language override for given language and configuration name. Overrides LanguageConfigFactoryOverrideInterface::getOverride
LanguageConfigFactoryOverride::getStorage public function Returns the storage instance for a particular langcode. Overrides LanguageConfigFactoryOverrideInterface::getStorage
LanguageConfigFactoryOverride::installLanguageOverrides public function Installs available language configuration overrides for a given langcode. Overrides LanguageConfigFactoryOverrideInterface::installLanguageOverrides
LanguageConfigFactoryOverride::loadOverrides public function Returns config overrides. Overrides ConfigFactoryOverrideInterface::loadOverrides
LanguageConfigFactoryOverride::onConfigDelete public function Actions to be performed to configuration override on configuration delete. Overrides ConfigFactoryOverrideBase::onConfigDelete
LanguageConfigFactoryOverride::onConfigRename public function Actions to be performed to configuration override on configuration rename. Overrides ConfigFactoryOverrideBase::onConfigRename
LanguageConfigFactoryOverride::onConfigSave public function Actions to be performed to configuration override on configuration save. Overrides ConfigFactoryOverrideBase::onConfigSave
LanguageConfigFactoryOverride::setLanguage public function Sets the language to be used in configuration overrides. Overrides LanguageConfigFactoryOverrideInterface::setLanguage
LanguageConfigFactoryOverride::__construct public function Constructs the LanguageConfigFactoryOverride object.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.