Mercurial > hg > isophonics-drupal-site
comparison core/modules/language/src/Config/LanguageConfigOverride.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\language\Config; | |
4 | |
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; | |
10 | |
11 /** | |
12 * Defines language configuration overrides. | |
13 */ | |
14 class LanguageConfigOverride extends StorableConfigBase { | |
15 | |
16 use LanguageConfigCollectionNameTrait; | |
17 | |
18 /** | |
19 * The event dispatcher. | |
20 * | |
21 * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface | |
22 */ | |
23 protected $eventDispatcher; | |
24 | |
25 /** | |
26 * Constructs a language override object. | |
27 * | |
28 * @param string $name | |
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. | |
37 */ | |
38 public function __construct($name, StorageInterface $storage, TypedConfigManagerInterface $typed_config, EventDispatcherInterface $event_dispatcher) { | |
39 $this->name = $name; | |
40 $this->storage = $storage; | |
41 $this->typedConfigManager = $typed_config; | |
42 $this->eventDispatcher = $event_dispatcher; | |
43 } | |
44 | |
45 /** | |
46 * {@inheritdoc} | |
47 */ | |
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); | |
55 } | |
56 } | |
57 | |
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()); | |
64 $this->isNew = FALSE; | |
65 $this->eventDispatcher->dispatch(LanguageConfigOverrideEvents::SAVE_OVERRIDE, new LanguageConfigOverrideCrudEvent($this)); | |
66 $this->originalData = $this->data; | |
67 return $this; | |
68 } | |
69 | |
70 /** | |
71 * {@inheritdoc} | |
72 */ | |
73 public function delete() { | |
74 $this->data = []; | |
75 $this->storage->delete($this->name); | |
76 Cache::invalidateTags($this->getCacheTags()); | |
77 $this->isNew = TRUE; | |
78 $this->eventDispatcher->dispatch(LanguageConfigOverrideEvents::DELETE_OVERRIDE, new LanguageConfigOverrideCrudEvent($this)); | |
79 $this->originalData = $this->data; | |
80 return $this; | |
81 } | |
82 | |
83 /** | |
84 * Returns the language code of this language override. | |
85 * | |
86 * @return string | |
87 * The language code. | |
88 */ | |
89 public function getLangcode() { | |
90 return $this->getLangcodeFromCollectionName($this->getStorage()->getCollectionName()); | |
91 } | |
92 | |
93 } |