Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\language\Config;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Cache\Cache;
|
Chris@0
|
6 use Drupal\Core\Config\StorableConfigBase;
|
Chris@0
|
7 use Drupal\Core\Config\StorageInterface;
|
Chris@0
|
8 use Drupal\Core\Config\TypedConfigManagerInterface;
|
Chris@0
|
9 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
Chris@0
|
10
|
Chris@0
|
11 /**
|
Chris@0
|
12 * Defines language configuration overrides.
|
Chris@0
|
13 */
|
Chris@0
|
14 class LanguageConfigOverride extends StorableConfigBase {
|
Chris@0
|
15
|
Chris@0
|
16 use LanguageConfigCollectionNameTrait;
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * The event dispatcher.
|
Chris@0
|
20 *
|
Chris@0
|
21 * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
|
Chris@0
|
22 */
|
Chris@0
|
23 protected $eventDispatcher;
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * Constructs a language override object.
|
Chris@0
|
27 *
|
Chris@0
|
28 * @param string $name
|
Chris@0
|
29 * The name of the configuration object being overridden.
|
Chris@0
|
30 * @param \Drupal\Core\Config\StorageInterface $storage
|
Chris@0
|
31 * A storage controller object to use for reading and writing the
|
Chris@0
|
32 * configuration override.
|
Chris@0
|
33 * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config
|
Chris@0
|
34 * The typed configuration manager service.
|
Chris@0
|
35 * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
|
Chris@0
|
36 * The event dispatcher.
|
Chris@0
|
37 */
|
Chris@0
|
38 public function __construct($name, StorageInterface $storage, TypedConfigManagerInterface $typed_config, EventDispatcherInterface $event_dispatcher) {
|
Chris@0
|
39 $this->name = $name;
|
Chris@0
|
40 $this->storage = $storage;
|
Chris@0
|
41 $this->typedConfigManager = $typed_config;
|
Chris@0
|
42 $this->eventDispatcher = $event_dispatcher;
|
Chris@0
|
43 }
|
Chris@0
|
44
|
Chris@0
|
45 /**
|
Chris@0
|
46 * {@inheritdoc}
|
Chris@0
|
47 */
|
Chris@0
|
48 public function save($has_trusted_data = FALSE) {
|
Chris@0
|
49 if (!$has_trusted_data) {
|
Chris@0
|
50 // @todo Use configuration schema to validate.
|
Chris@0
|
51 // https://www.drupal.org/node/2270399
|
Chris@0
|
52 // Perform basic data validation.
|
Chris@0
|
53 foreach ($this->data as $key => $value) {
|
Chris@0
|
54 $this->validateValue($key, $value);
|
Chris@0
|
55 }
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 $this->storage->write($this->name, $this->data);
|
Chris@0
|
59 // Invalidate the cache tags not only when updating, but also when creating,
|
Chris@0
|
60 // because a language config override object uses the same cache tag as the
|
Chris@0
|
61 // default configuration object. Hence creating a language override is like
|
Chris@0
|
62 // an update of configuration, but only for a specific language.
|
Chris@0
|
63 Cache::invalidateTags($this->getCacheTags());
|
Chris@0
|
64 $this->isNew = FALSE;
|
Chris@0
|
65 $this->eventDispatcher->dispatch(LanguageConfigOverrideEvents::SAVE_OVERRIDE, new LanguageConfigOverrideCrudEvent($this));
|
Chris@0
|
66 $this->originalData = $this->data;
|
Chris@0
|
67 return $this;
|
Chris@0
|
68 }
|
Chris@0
|
69
|
Chris@0
|
70 /**
|
Chris@0
|
71 * {@inheritdoc}
|
Chris@0
|
72 */
|
Chris@0
|
73 public function delete() {
|
Chris@0
|
74 $this->data = [];
|
Chris@0
|
75 $this->storage->delete($this->name);
|
Chris@0
|
76 Cache::invalidateTags($this->getCacheTags());
|
Chris@0
|
77 $this->isNew = TRUE;
|
Chris@0
|
78 $this->eventDispatcher->dispatch(LanguageConfigOverrideEvents::DELETE_OVERRIDE, new LanguageConfigOverrideCrudEvent($this));
|
Chris@0
|
79 $this->originalData = $this->data;
|
Chris@0
|
80 return $this;
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 /**
|
Chris@0
|
84 * Returns the language code of this language override.
|
Chris@0
|
85 *
|
Chris@0
|
86 * @return string
|
Chris@0
|
87 * The language code.
|
Chris@0
|
88 */
|
Chris@0
|
89 public function getLangcode() {
|
Chris@0
|
90 return $this->getLangcodeFromCollectionName($this->getStorage()->getCollectionName());
|
Chris@0
|
91 }
|
Chris@0
|
92
|
Chris@0
|
93 }
|