Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176
This commit is contained in:
commit
9921556621
13277 changed files with 1459781 additions and 0 deletions
|
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\language\Config\LanguageConfigCollectionNameTrait.
|
||||
*/
|
||||
|
||||
namespace Drupal\language\Config;
|
||||
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
|
||||
/**
|
||||
* Provides a common trait for working with language override collection names.
|
||||
*/
|
||||
trait LanguageConfigCollectionNameTrait {
|
||||
|
||||
/**
|
||||
* Creates a configuration collection name based on a language code.
|
||||
*
|
||||
* @param string $langcode
|
||||
* The language code.
|
||||
*
|
||||
* @return string
|
||||
* The configuration collection name for a language code.
|
||||
*/
|
||||
protected function createConfigCollectionName($langcode) {
|
||||
return 'language.' . $langcode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a configuration collection name to a language code.
|
||||
*
|
||||
* @param string $collection
|
||||
* The configuration collection name.
|
||||
*
|
||||
* @return string
|
||||
* The language code of the collection.
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
* Exception thrown if the provided collection name is not in the format
|
||||
* "language.LANGCODE".
|
||||
*
|
||||
* @see self::createConfigCollectionName()
|
||||
*/
|
||||
protected function getLangcodeFromCollectionName($collection) {
|
||||
preg_match('/^language\.(.*)$/', $collection, $matches);
|
||||
if (!isset($matches[1])) {
|
||||
throw new \InvalidArgumentException(SafeMarkup::format('!collection is not a valid language override collection', array('!collection' => $collection)));
|
||||
}
|
||||
return $matches[1];
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,225 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\language\Config\LanguageConfigFactoryOverride.
|
||||
*/
|
||||
|
||||
namespace Drupal\language\Config;
|
||||
|
||||
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\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
|
||||
/**
|
||||
* Provides language overrides for the configuration factory.
|
||||
*/
|
||||
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\Component\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\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
|
||||
* An event dispatcher instance to use for configuration events.
|
||||
* @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config
|
||||
* The typed configuration manager.
|
||||
*/
|
||||
public function __construct(StorageInterface $storage, EventDispatcherInterface $event_dispatcher, TypedConfigManagerInterface $typed_config) {
|
||||
$this->baseStorage = $storage;
|
||||
$this->eventDispatcher = $event_dispatcher;
|
||||
$this->typedConfigManager = $typed_config;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function loadOverrides($names) {
|
||||
if ($this->language) {
|
||||
$storage = $this->getStorage($this->language->getId());
|
||||
return $storage->readMultiple($names);
|
||||
}
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@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 setLanguageFromDefault(LanguageDefault $language_default = NULL) {
|
||||
$this->language = $language_default ? $language_default->get() : NULL;
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,79 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\language\Config\LanguageConfigFactoryOverrideInterface.
|
||||
*/
|
||||
|
||||
namespace Drupal\language\Config;
|
||||
|
||||
use Drupal\Core\Config\ConfigFactoryOverrideInterface;
|
||||
use Drupal\Core\Language\LanguageInterface;
|
||||
use Drupal\Core\Language\LanguageDefault;
|
||||
|
||||
/**
|
||||
* Defines the interface for a configuration factory language override object.
|
||||
*/
|
||||
interface LanguageConfigFactoryOverrideInterface extends ConfigFactoryOverrideInterface {
|
||||
|
||||
/**
|
||||
* Gets the language object used to override configuration data.
|
||||
*
|
||||
* @return \Drupal\Core\Language\LanguageInterface
|
||||
* The language object used to override configuration data.
|
||||
*/
|
||||
public function getLanguage();
|
||||
|
||||
/**
|
||||
* Sets the language to be used in configuration overrides.
|
||||
*
|
||||
* @param \Drupal\Core\Language\LanguageInterface $language
|
||||
* The language object used to override configuration data.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setLanguage(LanguageInterface $language = NULL);
|
||||
|
||||
/**
|
||||
* Sets the language to be used in configuration overrides from the default.
|
||||
*
|
||||
* @param \Drupal\Core\Language\LanguageDefault $language_default
|
||||
* The default language.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setLanguageFromDefault(LanguageDefault $language_default = NULL);
|
||||
|
||||
/**
|
||||
* Get language override for given language and configuration name.
|
||||
*
|
||||
* @param string $langcode
|
||||
* Language code.
|
||||
* @param string $name
|
||||
* Configuration name.
|
||||
*
|
||||
* @return \Drupal\Core\Config\Config
|
||||
* Configuration override object.
|
||||
*/
|
||||
public function getOverride($langcode, $name);
|
||||
|
||||
/**
|
||||
* Returns the storage instance for a particular langcode.
|
||||
*
|
||||
* @param string $langcode
|
||||
* Language code.
|
||||
*
|
||||
* @return \Drupal\Core\Config\StorageInterface
|
||||
* The storage instance for a particular langcode.
|
||||
*/
|
||||
public function getStorage($langcode);
|
||||
|
||||
/**
|
||||
* Installs available language configuration overrides for a given langcode.
|
||||
*
|
||||
* @param string $langcode
|
||||
* Language code.
|
||||
*/
|
||||
public function installLanguageOverrides($langcode);
|
||||
|
||||
}
|
||||
98
core/modules/language/src/Config/LanguageConfigOverride.php
Normal file
98
core/modules/language/src/Config/LanguageConfigOverride.php
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\language\Config\LanguageConfigOverride.
|
||||
*/
|
||||
|
||||
namespace Drupal\language\Config;
|
||||
|
||||
use Drupal\Core\Cache\Cache;
|
||||
use Drupal\Core\Config\StorableConfigBase;
|
||||
use Drupal\Core\Config\StorageInterface;
|
||||
use Drupal\Core\Config\TypedConfigManagerInterface;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
/**
|
||||
* Defines language configuration overrides.
|
||||
*/
|
||||
class LanguageConfigOverride extends StorableConfigBase {
|
||||
|
||||
use LanguageConfigCollectionNameTrait;
|
||||
|
||||
/**
|
||||
* The event dispatcher.
|
||||
*
|
||||
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
|
||||
*/
|
||||
protected $eventDispatcher;
|
||||
|
||||
/**
|
||||
* Constructs a language override object.
|
||||
*
|
||||
* @param string $name
|
||||
* The name of the configuration object being overridden.
|
||||
* @param \Drupal\Core\Config\StorageInterface $storage
|
||||
* A storage controller object to use for reading and writing the
|
||||
* configuration override.
|
||||
* @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config
|
||||
* The typed configuration manager service.
|
||||
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
|
||||
* The event dispatcher.
|
||||
*/
|
||||
public function __construct($name, StorageInterface $storage, TypedConfigManagerInterface $typed_config, EventDispatcherInterface $event_dispatcher) {
|
||||
$this->name = $name;
|
||||
$this->storage = $storage;
|
||||
$this->typedConfigManager = $typed_config;
|
||||
$this->eventDispatcher = $event_dispatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function save($has_trusted_data = FALSE) {
|
||||
if (!$has_trusted_data) {
|
||||
// @todo Use configuration schema to validate.
|
||||
// https://www.drupal.org/node/2270399
|
||||
// Perform basic data validation.
|
||||
foreach ($this->data as $key => $value) {
|
||||
$this->validateValue($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
$this->storage->write($this->name, $this->data);
|
||||
// Invalidate the cache tags not only when updating, but also when creating,
|
||||
// because a language config override object uses the same cache tag as the
|
||||
// default configuration object. Hence creating a language override is like
|
||||
// an update of configuration, but only for a specific language.
|
||||
Cache::invalidateTags($this->getCacheTags());
|
||||
$this->isNew = FALSE;
|
||||
$this->eventDispatcher->dispatch(LanguageConfigOverrideEvents::SAVE_OVERRIDE, new LanguageConfigOverrideCrudEvent($this));
|
||||
$this->originalData = $this->data;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function delete() {
|
||||
$this->data = array();
|
||||
$this->storage->delete($this->name);
|
||||
Cache::invalidateTags($this->getCacheTags());
|
||||
$this->isNew = TRUE;
|
||||
$this->eventDispatcher->dispatch(LanguageConfigOverrideEvents::DELETE_OVERRIDE, new LanguageConfigOverrideCrudEvent($this));
|
||||
$this->originalData = $this->data;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the language code of this language override.
|
||||
*
|
||||
* @return string
|
||||
* The language code.
|
||||
*/
|
||||
public function getLangcode() {
|
||||
return $this->getLangcodeFromCollectionName($this->getStorage()->getCollectionName());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\language\Config\LanguageConfigOverrideCrudEvent.
|
||||
*/
|
||||
|
||||
namespace Drupal\language\Config;
|
||||
|
||||
use Symfony\Component\EventDispatcher\Event;
|
||||
|
||||
/**
|
||||
* Provides a language override event for event listeners.
|
||||
*
|
||||
* @see \Drupal\Core\Config\ConfigCrudEvent
|
||||
*/
|
||||
class LanguageConfigOverrideCrudEvent extends Event {
|
||||
|
||||
/**
|
||||
* Configuration object.
|
||||
*
|
||||
* @var \Drupal\language\Config\LanguageConfigOverride
|
||||
*/
|
||||
protected $override;
|
||||
|
||||
/**
|
||||
* Constructs a configuration event object.
|
||||
*
|
||||
* @param \Drupal\language\Config\LanguageConfigOverride $override
|
||||
* Configuration object.
|
||||
*/
|
||||
public function __construct(LanguageConfigOverride $override) {
|
||||
$this->override = $override;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets configuration object.
|
||||
*
|
||||
* @return \Drupal\language\Config\LanguageConfigOverride
|
||||
* The configuration object that caused the event to fire.
|
||||
*/
|
||||
public function getLanguageConfigOverride() {
|
||||
return $this->override;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\language\Config\LanguageConfigOverrideEvents.
|
||||
*/
|
||||
|
||||
namespace Drupal\language\Config;
|
||||
|
||||
/**
|
||||
* Defines events for language configuration overrides.
|
||||
*
|
||||
* @see \Drupal\Core\Config\ConfigCrudEvent
|
||||
*/
|
||||
final class LanguageConfigOverrideEvents {
|
||||
|
||||
/**
|
||||
* The name of the event fired when saving the configuration override.
|
||||
*
|
||||
* This event allows you to perform custom actions whenever a language config
|
||||
* override is saved. The event listener method receives a
|
||||
* \Drupal\language\Config\LanguageConfigOverrideCrudEvent instance.
|
||||
*
|
||||
* @Event
|
||||
*
|
||||
* @see \Drupal\language\Config\LanguageConfigOverrideCrudEvent
|
||||
* @see \Drupal\language\Config\LanguageConfigOverride::save()
|
||||
* @see \Drupal\locale\LocaleConfigSubscriber
|
||||
*/
|
||||
const SAVE_OVERRIDE = 'language.save_override';
|
||||
|
||||
/**
|
||||
* The name of the event fired when deleting the configuration override.
|
||||
*
|
||||
* This event allows you to perform custom actions whenever a language config
|
||||
* override is deleted. The event listener method receives a
|
||||
* \Drupal\language\Config\LanguageConfigOverrideCrudEvent instance.
|
||||
*
|
||||
* @Event
|
||||
*
|
||||
* @see \Drupal\language\Config\LanguageConfigOverrideCrudEvent
|
||||
* @see \Drupal\language\Config\LanguageConfigOverride::delete()
|
||||
* @see \Drupal\locale\LocaleConfigSubscriber
|
||||
*/
|
||||
const DELETE_OVERRIDE = 'language.delete_override';
|
||||
|
||||
}
|
||||
Reference in a new issue