Mercurial > hg > isophonics-drupal-site
comparison core/modules/language/src/Entity/ContentLanguageSettings.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 129ea1e6d783 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\language\Entity; | |
4 | |
5 use Drupal\Core\Config\Entity\ConfigEntityBase; | |
6 use Drupal\Core\Entity\EntityStorageInterface; | |
7 use Drupal\Core\Language\LanguageInterface; | |
8 use Drupal\language\ContentLanguageSettingsException; | |
9 use Drupal\language\ContentLanguageSettingsInterface; | |
10 | |
11 /** | |
12 * Defines the ContentLanguageSettings entity. | |
13 * | |
14 * @ConfigEntityType( | |
15 * id = "language_content_settings", | |
16 * label = @Translation("Content Language Settings"), | |
17 * admin_permission = "administer languages", | |
18 * config_prefix = "content_settings", | |
19 * entity_keys = { | |
20 * "id" = "id" | |
21 * }, | |
22 * list_cache_tags = { "rendered" } | |
23 * ) | |
24 */ | |
25 class ContentLanguageSettings extends ConfigEntityBase implements ContentLanguageSettingsInterface { | |
26 | |
27 /** | |
28 * The id. Combination of $target_entity_type_id.$target_bundle. | |
29 * | |
30 * @var string | |
31 */ | |
32 protected $id; | |
33 | |
34 /** | |
35 * The entity type ID (machine name). | |
36 * | |
37 * @var string | |
38 */ | |
39 protected $target_entity_type_id; | |
40 | |
41 /** | |
42 * The bundle (machine name). | |
43 * | |
44 * @var string | |
45 */ | |
46 protected $target_bundle; | |
47 | |
48 /** | |
49 * The default language code. | |
50 * | |
51 * @var string | |
52 */ | |
53 protected $default_langcode = LanguageInterface::LANGCODE_SITE_DEFAULT; | |
54 | |
55 /** | |
56 * Indicates if the language is alterable or not. | |
57 * | |
58 * @var bool | |
59 */ | |
60 protected $language_alterable = FALSE; | |
61 | |
62 /** | |
63 * Constructs a ContentLanguageSettings object. | |
64 * | |
65 * In most cases, Field entities are created via | |
66 * FieldConfig::create($values), where $values is the same | |
67 * parameter as in this constructor. | |
68 * | |
69 * @param array $values | |
70 * An array of the referring entity bundle with: | |
71 * - target_entity_type_id: The entity type. | |
72 * - target_bundle: The bundle. | |
73 * Other array elements will be used to set the corresponding properties on | |
74 * the class; see the class property documentation for details. | |
75 * | |
76 * @see entity_create() | |
77 */ | |
78 public function __construct(array $values, $entity_type = 'language_content_settings') { | |
79 if (empty($values['target_entity_type_id'])) { | |
80 throw new ContentLanguageSettingsException('Attempt to create content language settings without a target_entity_type_id.'); | |
81 } | |
82 if (empty($values['target_bundle'])) { | |
83 throw new ContentLanguageSettingsException('Attempt to create content language settings without a target_bundle.'); | |
84 } | |
85 parent::__construct($values, $entity_type); | |
86 } | |
87 | |
88 /** | |
89 * {@inheritdoc} | |
90 */ | |
91 public function id() { | |
92 return $this->target_entity_type_id . '.' . $this->target_bundle; | |
93 } | |
94 | |
95 /** | |
96 * {@inheritdoc} | |
97 */ | |
98 public function getTargetEntityTypeId() { | |
99 return $this->target_entity_type_id; | |
100 } | |
101 | |
102 /** | |
103 * {@inheritdoc} | |
104 */ | |
105 public function getTargetBundle() { | |
106 return $this->target_bundle; | |
107 } | |
108 | |
109 /** | |
110 * {@inheritdoc} | |
111 */ | |
112 public function setTargetBundle($target_bundle) { | |
113 $this->target_bundle = $target_bundle; | |
114 | |
115 return $this; | |
116 } | |
117 | |
118 /** | |
119 * {@inheritdoc} | |
120 */ | |
121 public function setDefaultLangcode($default_langcode) { | |
122 $this->default_langcode = $default_langcode; | |
123 | |
124 return $this; | |
125 } | |
126 | |
127 /** | |
128 * {@inheritdoc} | |
129 */ | |
130 public function getDefaultLangcode() { | |
131 return $this->default_langcode; | |
132 } | |
133 | |
134 /** | |
135 * {@inheritdoc} | |
136 */ | |
137 public function setLanguageAlterable($language_alterable) { | |
138 $this->language_alterable = $language_alterable; | |
139 | |
140 return $this; | |
141 } | |
142 | |
143 /** | |
144 * {@inheritdoc} | |
145 */ | |
146 public function isLanguageAlterable() { | |
147 return $this->language_alterable; | |
148 } | |
149 | |
150 /** | |
151 * {@inheritdoc} | |
152 */ | |
153 public function preSave(EntityStorageInterface $storage) { | |
154 $this->id = $this->id(); | |
155 parent::preSave($storage); | |
156 } | |
157 | |
158 /** | |
159 * {@inheritdoc} | |
160 */ | |
161 public function isDefaultConfiguration() { | |
162 return (!$this->language_alterable && $this->default_langcode == LanguageInterface::LANGCODE_SITE_DEFAULT); | |
163 } | |
164 | |
165 /** | |
166 * Loads a content language config entity based on the entity type and bundle. | |
167 * | |
168 * @param string $entity_type_id | |
169 * ID of the entity type. | |
170 * @param string $bundle | |
171 * Bundle name. | |
172 * | |
173 * @return $this | |
174 * The content language config entity if one exists. Otherwise, returns | |
175 * default values. | |
176 */ | |
177 public static function loadByEntityTypeBundle($entity_type_id, $bundle) { | |
178 if ($entity_type_id == NULL || $bundle == NULL) { | |
179 return NULL; | |
180 } | |
181 $config = \Drupal::entityManager()->getStorage('language_content_settings')->load($entity_type_id . '.' . $bundle); | |
182 if ($config == NULL) { | |
183 $config = ContentLanguageSettings::create(['target_entity_type_id' => $entity_type_id, 'target_bundle' => $bundle]); | |
184 } | |
185 return $config; | |
186 } | |
187 | |
188 /** | |
189 * {@inheritdoc} | |
190 */ | |
191 public function calculateDependencies() { | |
192 parent::calculateDependencies(); | |
193 | |
194 // Create dependency on the bundle. | |
195 $entity_type = \Drupal::entityManager()->getDefinition($this->target_entity_type_id); | |
196 $bundle_config_dependency = $entity_type->getBundleConfigDependency($this->target_bundle); | |
197 $this->addDependency($bundle_config_dependency['type'], $bundle_config_dependency['name']); | |
198 | |
199 return $this; | |
200 } | |
201 | |
202 } |