3 namespace Drupal\language\Config;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Config\StorableConfigBase;
7 use Drupal\Core\Config\StorageInterface;
8 use Drupal\Core\Config\TypedConfigManagerInterface;
9 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
12 * Defines language configuration overrides.
14 class LanguageConfigOverride extends StorableConfigBase {
16 use LanguageConfigCollectionNameTrait;
19 * The event dispatcher.
21 * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
23 protected $eventDispatcher;
26 * Constructs a language override object.
29 * The name of the configuration object being overridden.
30 * @param \Drupal\Core\Config\StorageInterface $storage
31 * A storage controller object to use for reading and writing the
32 * configuration override.
33 * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config
34 * The typed configuration manager service.
35 * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
36 * The event dispatcher.
38 public function __construct($name, StorageInterface $storage, TypedConfigManagerInterface $typed_config, EventDispatcherInterface $event_dispatcher) {
40 $this->storage = $storage;
41 $this->typedConfigManager = $typed_config;
42 $this->eventDispatcher = $event_dispatcher;
48 public function save($has_trusted_data = FALSE) {
49 if (!$has_trusted_data) {
50 // @todo Use configuration schema to validate.
51 // https://www.drupal.org/node/2270399
52 // Perform basic data validation.
53 foreach ($this->data as $key => $value) {
54 $this->validateValue($key, $value);
58 $this->storage->write($this->name, $this->data);
59 // Invalidate the cache tags not only when updating, but also when creating,
60 // because a language config override object uses the same cache tag as the
61 // default configuration object. Hence creating a language override is like
62 // an update of configuration, but only for a specific language.
63 Cache::invalidateTags($this->getCacheTags());
65 $this->eventDispatcher->dispatch(LanguageConfigOverrideEvents::SAVE_OVERRIDE, new LanguageConfigOverrideCrudEvent($this));
66 $this->originalData = $this->data;
73 public function delete() {
75 $this->storage->delete($this->name);
76 Cache::invalidateTags($this->getCacheTags());
78 $this->eventDispatcher->dispatch(LanguageConfigOverrideEvents::DELETE_OVERRIDE, new LanguageConfigOverrideCrudEvent($this));
79 $this->originalData = $this->data;
84 * Returns the language code of this language override.
89 public function getLangcode() {
90 return $this->getLangcodeFromCollectionName($this->getStorage()->getCollectionName());