Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Entity;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Utility\SafeMarkup;
|
Chris@0
|
6 use Drupal\Core\Entity\Plugin\DataType\EntityReference;
|
Chris@0
|
7 use Drupal\Core\Field\BaseFieldDefinition;
|
Chris@0
|
8 use Drupal\Core\Field\ChangedFieldItemList;
|
Chris@0
|
9 use Drupal\Core\Language\Language;
|
Chris@0
|
10 use Drupal\Core\Language\LanguageInterface;
|
Chris@0
|
11 use Drupal\Core\Session\AccountInterface;
|
Chris@0
|
12 use Drupal\Core\StringTranslation\TranslatableMarkup;
|
Chris@0
|
13 use Drupal\Core\TypedData\TranslationStatusInterface;
|
Chris@0
|
14 use Drupal\Core\TypedData\TypedDataInterface;
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * Implements Entity Field API specific enhancements to the Entity class.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @ingroup entity_api
|
Chris@0
|
20 */
|
Chris@0
|
21 abstract class ContentEntityBase extends Entity implements \IteratorAggregate, ContentEntityInterface, TranslationStatusInterface {
|
Chris@0
|
22
|
Chris@14
|
23 use EntityChangesDetectionTrait {
|
Chris@14
|
24 getFieldsToSkipFromTranslationChangesCheck as traitGetFieldsToSkipFromTranslationChangesCheck;
|
Chris@14
|
25 }
|
Chris@14
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * The plain data values of the contained fields.
|
Chris@0
|
29 *
|
Chris@0
|
30 * This always holds the original, unchanged values of the entity. The values
|
Chris@0
|
31 * are keyed by language code, whereas LanguageInterface::LANGCODE_DEFAULT
|
Chris@0
|
32 * is used for values in default language.
|
Chris@0
|
33 *
|
Chris@0
|
34 * @todo: Add methods for getting original fields and for determining
|
Chris@0
|
35 * changes.
|
Chris@0
|
36 * @todo: Provide a better way for defining default values.
|
Chris@0
|
37 *
|
Chris@0
|
38 * @var array
|
Chris@0
|
39 */
|
Chris@0
|
40 protected $values = [];
|
Chris@0
|
41
|
Chris@0
|
42 /**
|
Chris@0
|
43 * The array of fields, each being an instance of FieldItemListInterface.
|
Chris@0
|
44 *
|
Chris@0
|
45 * @var array
|
Chris@0
|
46 */
|
Chris@0
|
47 protected $fields = [];
|
Chris@0
|
48
|
Chris@0
|
49 /**
|
Chris@0
|
50 * Local cache for field definitions.
|
Chris@0
|
51 *
|
Chris@0
|
52 * @see ContentEntityBase::getFieldDefinitions()
|
Chris@0
|
53 *
|
Chris@0
|
54 * @var array
|
Chris@0
|
55 */
|
Chris@0
|
56 protected $fieldDefinitions;
|
Chris@0
|
57
|
Chris@0
|
58 /**
|
Chris@0
|
59 * Local cache for the available language objects.
|
Chris@0
|
60 *
|
Chris@0
|
61 * @var \Drupal\Core\Language\LanguageInterface[]
|
Chris@0
|
62 */
|
Chris@0
|
63 protected $languages;
|
Chris@0
|
64
|
Chris@0
|
65 /**
|
Chris@0
|
66 * The language entity key.
|
Chris@0
|
67 *
|
Chris@0
|
68 * @var string
|
Chris@0
|
69 */
|
Chris@0
|
70 protected $langcodeKey;
|
Chris@0
|
71
|
Chris@0
|
72 /**
|
Chris@0
|
73 * The default langcode entity key.
|
Chris@0
|
74 *
|
Chris@0
|
75 * @var string
|
Chris@0
|
76 */
|
Chris@0
|
77 protected $defaultLangcodeKey;
|
Chris@0
|
78
|
Chris@0
|
79 /**
|
Chris@0
|
80 * Language code identifying the entity active language.
|
Chris@0
|
81 *
|
Chris@0
|
82 * This is the language field accessors will use to determine which field
|
Chris@0
|
83 * values manipulate.
|
Chris@0
|
84 *
|
Chris@0
|
85 * @var string
|
Chris@0
|
86 */
|
Chris@0
|
87 protected $activeLangcode = LanguageInterface::LANGCODE_DEFAULT;
|
Chris@0
|
88
|
Chris@0
|
89 /**
|
Chris@0
|
90 * Local cache for the default language code.
|
Chris@0
|
91 *
|
Chris@0
|
92 * @var string
|
Chris@0
|
93 */
|
Chris@0
|
94 protected $defaultLangcode;
|
Chris@0
|
95
|
Chris@0
|
96 /**
|
Chris@0
|
97 * An array of entity translation metadata.
|
Chris@0
|
98 *
|
Chris@0
|
99 * An associative array keyed by translation language code. Every value is an
|
Chris@0
|
100 * array containing the translation status and the translation object, if it has
|
Chris@0
|
101 * already been instantiated.
|
Chris@0
|
102 *
|
Chris@0
|
103 * @var array
|
Chris@0
|
104 */
|
Chris@0
|
105 protected $translations = [];
|
Chris@0
|
106
|
Chris@0
|
107 /**
|
Chris@0
|
108 * A flag indicating whether a translation object is being initialized.
|
Chris@0
|
109 *
|
Chris@0
|
110 * @var bool
|
Chris@0
|
111 */
|
Chris@0
|
112 protected $translationInitialize = FALSE;
|
Chris@0
|
113
|
Chris@0
|
114 /**
|
Chris@0
|
115 * Boolean indicating whether a new revision should be created on save.
|
Chris@0
|
116 *
|
Chris@0
|
117 * @var bool
|
Chris@0
|
118 */
|
Chris@0
|
119 protected $newRevision = FALSE;
|
Chris@0
|
120
|
Chris@0
|
121 /**
|
Chris@0
|
122 * Indicates whether this is the default revision.
|
Chris@0
|
123 *
|
Chris@0
|
124 * @var bool
|
Chris@0
|
125 */
|
Chris@0
|
126 protected $isDefaultRevision = TRUE;
|
Chris@0
|
127
|
Chris@0
|
128 /**
|
Chris@0
|
129 * Holds untranslatable entity keys such as the ID, bundle, and revision ID.
|
Chris@0
|
130 *
|
Chris@0
|
131 * @var array
|
Chris@0
|
132 */
|
Chris@0
|
133 protected $entityKeys = [];
|
Chris@0
|
134
|
Chris@0
|
135 /**
|
Chris@0
|
136 * Holds translatable entity keys such as the label.
|
Chris@0
|
137 *
|
Chris@0
|
138 * @var array
|
Chris@0
|
139 */
|
Chris@0
|
140 protected $translatableEntityKeys = [];
|
Chris@0
|
141
|
Chris@0
|
142 /**
|
Chris@0
|
143 * Whether entity validation was performed.
|
Chris@0
|
144 *
|
Chris@0
|
145 * @var bool
|
Chris@0
|
146 */
|
Chris@0
|
147 protected $validated = FALSE;
|
Chris@0
|
148
|
Chris@0
|
149 /**
|
Chris@0
|
150 * Whether entity validation is required before saving the entity.
|
Chris@0
|
151 *
|
Chris@0
|
152 * @var bool
|
Chris@0
|
153 */
|
Chris@0
|
154 protected $validationRequired = FALSE;
|
Chris@0
|
155
|
Chris@0
|
156 /**
|
Chris@0
|
157 * The loaded revision ID before the new revision was set.
|
Chris@0
|
158 *
|
Chris@0
|
159 * @var int
|
Chris@0
|
160 */
|
Chris@0
|
161 protected $loadedRevisionId;
|
Chris@0
|
162
|
Chris@0
|
163 /**
|
Chris@14
|
164 * The revision translation affected entity key.
|
Chris@14
|
165 *
|
Chris@14
|
166 * @var string
|
Chris@14
|
167 */
|
Chris@14
|
168 protected $revisionTranslationAffectedKey;
|
Chris@14
|
169
|
Chris@14
|
170 /**
|
Chris@14
|
171 * Whether the revision translation affected flag has been enforced.
|
Chris@14
|
172 *
|
Chris@14
|
173 * An array, keyed by the translation language code.
|
Chris@14
|
174 *
|
Chris@14
|
175 * @var bool[]
|
Chris@14
|
176 */
|
Chris@14
|
177 protected $enforceRevisionTranslationAffected = [];
|
Chris@14
|
178
|
Chris@14
|
179 /**
|
Chris@0
|
180 * {@inheritdoc}
|
Chris@0
|
181 */
|
Chris@0
|
182 public function __construct(array $values, $entity_type, $bundle = FALSE, $translations = []) {
|
Chris@0
|
183 $this->entityTypeId = $entity_type;
|
Chris@0
|
184 $this->entityKeys['bundle'] = $bundle ? $bundle : $this->entityTypeId;
|
Chris@0
|
185 $this->langcodeKey = $this->getEntityType()->getKey('langcode');
|
Chris@0
|
186 $this->defaultLangcodeKey = $this->getEntityType()->getKey('default_langcode');
|
Chris@14
|
187 $this->revisionTranslationAffectedKey = $this->getEntityType()->getKey('revision_translation_affected');
|
Chris@0
|
188
|
Chris@0
|
189 foreach ($values as $key => $value) {
|
Chris@0
|
190 // If the key matches an existing property set the value to the property
|
Chris@0
|
191 // to set properties like isDefaultRevision.
|
Chris@0
|
192 // @todo: Should this be converted somehow?
|
Chris@0
|
193 if (property_exists($this, $key) && isset($value[LanguageInterface::LANGCODE_DEFAULT])) {
|
Chris@0
|
194 $this->$key = $value[LanguageInterface::LANGCODE_DEFAULT];
|
Chris@0
|
195 }
|
Chris@0
|
196 }
|
Chris@0
|
197
|
Chris@0
|
198 $this->values = $values;
|
Chris@0
|
199 foreach ($this->getEntityType()->getKeys() as $key => $field_name) {
|
Chris@0
|
200 if (isset($this->values[$field_name])) {
|
Chris@0
|
201 if (is_array($this->values[$field_name])) {
|
Chris@0
|
202 // We store untranslatable fields into an entity key without using a
|
Chris@0
|
203 // langcode key.
|
Chris@0
|
204 if (!$this->getFieldDefinition($field_name)->isTranslatable()) {
|
Chris@0
|
205 if (isset($this->values[$field_name][LanguageInterface::LANGCODE_DEFAULT])) {
|
Chris@0
|
206 if (is_array($this->values[$field_name][LanguageInterface::LANGCODE_DEFAULT])) {
|
Chris@0
|
207 if (isset($this->values[$field_name][LanguageInterface::LANGCODE_DEFAULT][0]['value'])) {
|
Chris@0
|
208 $this->entityKeys[$key] = $this->values[$field_name][LanguageInterface::LANGCODE_DEFAULT][0]['value'];
|
Chris@0
|
209 }
|
Chris@0
|
210 }
|
Chris@0
|
211 else {
|
Chris@0
|
212 $this->entityKeys[$key] = $this->values[$field_name][LanguageInterface::LANGCODE_DEFAULT];
|
Chris@0
|
213 }
|
Chris@0
|
214 }
|
Chris@0
|
215 }
|
Chris@0
|
216 else {
|
Chris@0
|
217 // We save translatable fields such as the publishing status of a node
|
Chris@0
|
218 // into an entity key array keyed by langcode as a performance
|
Chris@0
|
219 // optimization, so we don't have to go through TypedData when we
|
Chris@0
|
220 // need these values.
|
Chris@0
|
221 foreach ($this->values[$field_name] as $langcode => $field_value) {
|
Chris@0
|
222 if (is_array($this->values[$field_name][$langcode])) {
|
Chris@0
|
223 if (isset($this->values[$field_name][$langcode][0]['value'])) {
|
Chris@0
|
224 $this->translatableEntityKeys[$key][$langcode] = $this->values[$field_name][$langcode][0]['value'];
|
Chris@0
|
225 }
|
Chris@0
|
226 }
|
Chris@0
|
227 else {
|
Chris@0
|
228 $this->translatableEntityKeys[$key][$langcode] = $this->values[$field_name][$langcode];
|
Chris@0
|
229 }
|
Chris@0
|
230 }
|
Chris@0
|
231 }
|
Chris@0
|
232 }
|
Chris@0
|
233 }
|
Chris@0
|
234 }
|
Chris@0
|
235
|
Chris@0
|
236 // Initialize translations. Ensure we have at least an entry for the default
|
Chris@0
|
237 // language.
|
Chris@0
|
238 // We determine if the entity is new by checking in the entity values for
|
Chris@0
|
239 // the presence of the id entity key, as the usage of ::isNew() is not
|
Chris@0
|
240 // possible in the constructor.
|
Chris@0
|
241 $data = isset($values[$this->getEntityType()->getKey('id')]) ? ['status' => static::TRANSLATION_EXISTING] : ['status' => static::TRANSLATION_CREATED];
|
Chris@0
|
242 $this->translations[LanguageInterface::LANGCODE_DEFAULT] = $data;
|
Chris@0
|
243 $this->setDefaultLangcode();
|
Chris@0
|
244 if ($translations) {
|
Chris@0
|
245 foreach ($translations as $langcode) {
|
Chris@0
|
246 if ($langcode != $this->defaultLangcode && $langcode != LanguageInterface::LANGCODE_DEFAULT) {
|
Chris@0
|
247 $this->translations[$langcode] = $data;
|
Chris@0
|
248 }
|
Chris@0
|
249 }
|
Chris@0
|
250 }
|
Chris@0
|
251 if ($this->getEntityType()->isRevisionable()) {
|
Chris@0
|
252 // Store the loaded revision ID the entity has been loaded with to
|
Chris@0
|
253 // keep it safe from changes.
|
Chris@0
|
254 $this->updateLoadedRevisionId();
|
Chris@0
|
255 }
|
Chris@0
|
256 }
|
Chris@0
|
257
|
Chris@0
|
258 /**
|
Chris@0
|
259 * {@inheritdoc}
|
Chris@0
|
260 */
|
Chris@0
|
261 protected function getLanguages() {
|
Chris@0
|
262 if (empty($this->languages)) {
|
Chris@0
|
263 $this->languages = $this->languageManager()->getLanguages(LanguageInterface::STATE_ALL);
|
Chris@0
|
264 // If the entity references a language that is not or no longer available,
|
Chris@0
|
265 // we return a mock language object to avoid disrupting the consuming
|
Chris@0
|
266 // code.
|
Chris@0
|
267 if (!isset($this->languages[$this->defaultLangcode])) {
|
Chris@0
|
268 $this->languages[$this->defaultLangcode] = new Language(['id' => $this->defaultLangcode]);
|
Chris@0
|
269 }
|
Chris@0
|
270 }
|
Chris@0
|
271 return $this->languages;
|
Chris@0
|
272 }
|
Chris@0
|
273
|
Chris@0
|
274 /**
|
Chris@0
|
275 * {@inheritdoc}
|
Chris@0
|
276 */
|
Chris@0
|
277 public function postCreate(EntityStorageInterface $storage) {
|
Chris@0
|
278 $this->newRevision = TRUE;
|
Chris@0
|
279 }
|
Chris@0
|
280
|
Chris@0
|
281 /**
|
Chris@0
|
282 * {@inheritdoc}
|
Chris@0
|
283 */
|
Chris@0
|
284 public function setNewRevision($value = TRUE) {
|
Chris@0
|
285 if (!$this->getEntityType()->hasKey('revision')) {
|
Chris@0
|
286 throw new \LogicException("Entity type {$this->getEntityTypeId()} does not support revisions.");
|
Chris@0
|
287 }
|
Chris@0
|
288
|
Chris@0
|
289 if ($value && !$this->newRevision) {
|
Chris@0
|
290 // When saving a new revision, set any existing revision ID to NULL so as
|
Chris@0
|
291 // to ensure that a new revision will actually be created.
|
Chris@0
|
292 $this->set($this->getEntityType()->getKey('revision'), NULL);
|
Chris@14
|
293 }
|
Chris@14
|
294 elseif (!$value && $this->newRevision) {
|
Chris@14
|
295 // If ::setNewRevision(FALSE) is called after ::setNewRevision(TRUE) we
|
Chris@14
|
296 // have to restore the loaded revision ID.
|
Chris@14
|
297 $this->set($this->getEntityType()->getKey('revision'), $this->getLoadedRevisionId());
|
Chris@0
|
298 }
|
Chris@0
|
299
|
Chris@0
|
300 $this->newRevision = $value;
|
Chris@0
|
301 }
|
Chris@0
|
302
|
Chris@0
|
303 /**
|
Chris@0
|
304 * {@inheritdoc}
|
Chris@0
|
305 */
|
Chris@0
|
306 public function getLoadedRevisionId() {
|
Chris@0
|
307 return $this->loadedRevisionId;
|
Chris@0
|
308 }
|
Chris@0
|
309
|
Chris@0
|
310 /**
|
Chris@0
|
311 * {@inheritdoc}
|
Chris@0
|
312 */
|
Chris@0
|
313 public function updateLoadedRevisionId() {
|
Chris@0
|
314 $this->loadedRevisionId = $this->getRevisionId() ?: $this->loadedRevisionId;
|
Chris@0
|
315 return $this;
|
Chris@0
|
316 }
|
Chris@0
|
317
|
Chris@0
|
318 /**
|
Chris@0
|
319 * {@inheritdoc}
|
Chris@0
|
320 */
|
Chris@0
|
321 public function isNewRevision() {
|
Chris@0
|
322 return $this->newRevision || ($this->getEntityType()->hasKey('revision') && !$this->getRevisionId());
|
Chris@0
|
323 }
|
Chris@0
|
324
|
Chris@0
|
325 /**
|
Chris@0
|
326 * {@inheritdoc}
|
Chris@0
|
327 */
|
Chris@0
|
328 public function isDefaultRevision($new_value = NULL) {
|
Chris@0
|
329 $return = $this->isDefaultRevision;
|
Chris@0
|
330 if (isset($new_value)) {
|
Chris@0
|
331 $this->isDefaultRevision = (bool) $new_value;
|
Chris@0
|
332 }
|
Chris@0
|
333 // New entities should always ensure at least one default revision exists,
|
Chris@0
|
334 // creating an entity without a default revision is an invalid state.
|
Chris@0
|
335 return $this->isNew() || $return;
|
Chris@0
|
336 }
|
Chris@0
|
337
|
Chris@0
|
338 /**
|
Chris@0
|
339 * {@inheritdoc}
|
Chris@0
|
340 */
|
Chris@14
|
341 public function wasDefaultRevision() {
|
Chris@14
|
342 /** @var \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type */
|
Chris@14
|
343 $entity_type = $this->getEntityType();
|
Chris@14
|
344 if (!$entity_type->isRevisionable()) {
|
Chris@14
|
345 return TRUE;
|
Chris@14
|
346 }
|
Chris@14
|
347
|
Chris@14
|
348 $revision_default_key = $entity_type->getRevisionMetadataKey('revision_default');
|
Chris@14
|
349 $value = $this->isNew() || $this->get($revision_default_key)->value;
|
Chris@14
|
350 return $value;
|
Chris@14
|
351 }
|
Chris@14
|
352
|
Chris@14
|
353 /**
|
Chris@14
|
354 * {@inheritdoc}
|
Chris@14
|
355 */
|
Chris@14
|
356 public function isLatestRevision() {
|
Chris@14
|
357 /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
|
Chris@14
|
358 $storage = $this->entityTypeManager()->getStorage($this->getEntityTypeId());
|
Chris@14
|
359
|
Chris@14
|
360 return $this->getLoadedRevisionId() == $storage->getLatestRevisionId($this->id());
|
Chris@14
|
361 }
|
Chris@14
|
362
|
Chris@14
|
363 /**
|
Chris@14
|
364 * {@inheritdoc}
|
Chris@14
|
365 */
|
Chris@14
|
366 public function isLatestTranslationAffectedRevision() {
|
Chris@14
|
367 /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
|
Chris@14
|
368 $storage = $this->entityTypeManager()->getStorage($this->getEntityTypeId());
|
Chris@14
|
369
|
Chris@14
|
370 return $this->getLoadedRevisionId() == $storage->getLatestTranslationAffectedRevisionId($this->id(), $this->language()->getId());
|
Chris@14
|
371 }
|
Chris@14
|
372
|
Chris@14
|
373 /**
|
Chris@14
|
374 * {@inheritdoc}
|
Chris@14
|
375 */
|
Chris@0
|
376 public function isRevisionTranslationAffected() {
|
Chris@14
|
377 return $this->hasField($this->revisionTranslationAffectedKey) ? $this->get($this->revisionTranslationAffectedKey)->value : TRUE;
|
Chris@0
|
378 }
|
Chris@0
|
379
|
Chris@0
|
380 /**
|
Chris@0
|
381 * {@inheritdoc}
|
Chris@0
|
382 */
|
Chris@0
|
383 public function setRevisionTranslationAffected($affected) {
|
Chris@14
|
384 if ($this->hasField($this->revisionTranslationAffectedKey)) {
|
Chris@14
|
385 $this->set($this->revisionTranslationAffectedKey, $affected);
|
Chris@0
|
386 }
|
Chris@0
|
387 return $this;
|
Chris@0
|
388 }
|
Chris@0
|
389
|
Chris@0
|
390 /**
|
Chris@0
|
391 * {@inheritdoc}
|
Chris@0
|
392 */
|
Chris@14
|
393 public function isRevisionTranslationAffectedEnforced() {
|
Chris@14
|
394 return !empty($this->enforceRevisionTranslationAffected[$this->activeLangcode]);
|
Chris@14
|
395 }
|
Chris@14
|
396
|
Chris@14
|
397 /**
|
Chris@14
|
398 * {@inheritdoc}
|
Chris@14
|
399 */
|
Chris@14
|
400 public function setRevisionTranslationAffectedEnforced($enforced) {
|
Chris@14
|
401 $this->enforceRevisionTranslationAffected[$this->activeLangcode] = $enforced;
|
Chris@14
|
402 return $this;
|
Chris@14
|
403 }
|
Chris@14
|
404
|
Chris@14
|
405 /**
|
Chris@14
|
406 * {@inheritdoc}
|
Chris@14
|
407 */
|
Chris@0
|
408 public function isDefaultTranslation() {
|
Chris@0
|
409 return $this->activeLangcode === LanguageInterface::LANGCODE_DEFAULT;
|
Chris@0
|
410 }
|
Chris@0
|
411
|
Chris@0
|
412 /**
|
Chris@0
|
413 * {@inheritdoc}
|
Chris@0
|
414 */
|
Chris@0
|
415 public function getRevisionId() {
|
Chris@0
|
416 return $this->getEntityKey('revision');
|
Chris@0
|
417 }
|
Chris@0
|
418
|
Chris@0
|
419 /**
|
Chris@0
|
420 * {@inheritdoc}
|
Chris@0
|
421 */
|
Chris@0
|
422 public function isTranslatable() {
|
Chris@14
|
423 // Check the bundle is translatable, the entity has a language defined, and
|
Chris@14
|
424 // the site has more than one language.
|
Chris@0
|
425 $bundles = $this->entityManager()->getBundleInfo($this->entityTypeId);
|
Chris@0
|
426 return !empty($bundles[$this->bundle()]['translatable']) && !$this->getUntranslated()->language()->isLocked() && $this->languageManager()->isMultilingual();
|
Chris@0
|
427 }
|
Chris@0
|
428
|
Chris@0
|
429 /**
|
Chris@0
|
430 * {@inheritdoc}
|
Chris@0
|
431 */
|
Chris@0
|
432 public function preSave(EntityStorageInterface $storage) {
|
Chris@0
|
433 // An entity requiring validation should not be saved if it has not been
|
Chris@0
|
434 // actually validated.
|
Chris@0
|
435 if ($this->validationRequired && !$this->validated) {
|
Chris@0
|
436 // @todo Make this an assertion in https://www.drupal.org/node/2408013.
|
Chris@0
|
437 throw new \LogicException('Entity validation was skipped.');
|
Chris@0
|
438 }
|
Chris@0
|
439 else {
|
Chris@0
|
440 $this->validated = FALSE;
|
Chris@0
|
441 }
|
Chris@0
|
442
|
Chris@0
|
443 parent::preSave($storage);
|
Chris@0
|
444 }
|
Chris@0
|
445
|
Chris@0
|
446 /**
|
Chris@0
|
447 * {@inheritdoc}
|
Chris@0
|
448 */
|
Chris@0
|
449 public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record) {
|
Chris@0
|
450 }
|
Chris@0
|
451
|
Chris@0
|
452 /**
|
Chris@0
|
453 * {@inheritdoc}
|
Chris@0
|
454 */
|
Chris@0
|
455 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
|
Chris@0
|
456 parent::postSave($storage, $update);
|
Chris@0
|
457
|
Chris@0
|
458 // Update the status of all saved translations.
|
Chris@0
|
459 $removed = [];
|
Chris@0
|
460 foreach ($this->translations as $langcode => &$data) {
|
Chris@0
|
461 if ($data['status'] == static::TRANSLATION_REMOVED) {
|
Chris@0
|
462 $removed[$langcode] = TRUE;
|
Chris@0
|
463 }
|
Chris@0
|
464 else {
|
Chris@0
|
465 $data['status'] = static::TRANSLATION_EXISTING;
|
Chris@0
|
466 }
|
Chris@0
|
467 }
|
Chris@0
|
468 $this->translations = array_diff_key($this->translations, $removed);
|
Chris@14
|
469
|
Chris@14
|
470 // Reset the new revision flag.
|
Chris@14
|
471 $this->newRevision = FALSE;
|
Chris@14
|
472
|
Chris@14
|
473 // Reset the enforcement of the revision translation affected flag.
|
Chris@14
|
474 $this->enforceRevisionTranslationAffected = [];
|
Chris@0
|
475 }
|
Chris@0
|
476
|
Chris@0
|
477 /**
|
Chris@0
|
478 * {@inheritdoc}
|
Chris@0
|
479 */
|
Chris@0
|
480 public function validate() {
|
Chris@0
|
481 $this->validated = TRUE;
|
Chris@0
|
482 $violations = $this->getTypedData()->validate();
|
Chris@0
|
483 return new EntityConstraintViolationList($this, iterator_to_array($violations));
|
Chris@0
|
484 }
|
Chris@0
|
485
|
Chris@0
|
486 /**
|
Chris@0
|
487 * {@inheritdoc}
|
Chris@0
|
488 */
|
Chris@0
|
489 public function isValidationRequired() {
|
Chris@0
|
490 return (bool) $this->validationRequired;
|
Chris@0
|
491 }
|
Chris@0
|
492
|
Chris@0
|
493 /**
|
Chris@0
|
494 * {@inheritdoc}
|
Chris@0
|
495 */
|
Chris@0
|
496 public function setValidationRequired($required) {
|
Chris@0
|
497 $this->validationRequired = $required;
|
Chris@0
|
498 return $this;
|
Chris@0
|
499 }
|
Chris@0
|
500
|
Chris@0
|
501 /**
|
Chris@0
|
502 * Clear entity translation object cache to remove stale references.
|
Chris@0
|
503 */
|
Chris@0
|
504 protected function clearTranslationCache() {
|
Chris@0
|
505 foreach ($this->translations as &$translation) {
|
Chris@0
|
506 unset($translation['entity']);
|
Chris@0
|
507 }
|
Chris@0
|
508 }
|
Chris@0
|
509
|
Chris@0
|
510 /**
|
Chris@0
|
511 * {@inheritdoc}
|
Chris@0
|
512 */
|
Chris@0
|
513 public function __sleep() {
|
Chris@0
|
514 // Get the values of instantiated field objects, only serialize the values.
|
Chris@0
|
515 foreach ($this->fields as $name => $fields) {
|
Chris@0
|
516 foreach ($fields as $langcode => $field) {
|
Chris@0
|
517 $this->values[$name][$langcode] = $field->getValue();
|
Chris@0
|
518 }
|
Chris@0
|
519 }
|
Chris@0
|
520 $this->fields = [];
|
Chris@0
|
521 $this->fieldDefinitions = NULL;
|
Chris@0
|
522 $this->languages = NULL;
|
Chris@0
|
523 $this->clearTranslationCache();
|
Chris@0
|
524
|
Chris@0
|
525 return parent::__sleep();
|
Chris@0
|
526 }
|
Chris@0
|
527
|
Chris@0
|
528 /**
|
Chris@0
|
529 * {@inheritdoc}
|
Chris@0
|
530 */
|
Chris@0
|
531 public function id() {
|
Chris@0
|
532 return $this->getEntityKey('id');
|
Chris@0
|
533 }
|
Chris@0
|
534
|
Chris@0
|
535 /**
|
Chris@0
|
536 * {@inheritdoc}
|
Chris@0
|
537 */
|
Chris@0
|
538 public function bundle() {
|
Chris@0
|
539 return $this->getEntityKey('bundle');
|
Chris@0
|
540 }
|
Chris@0
|
541
|
Chris@0
|
542 /**
|
Chris@0
|
543 * {@inheritdoc}
|
Chris@0
|
544 */
|
Chris@0
|
545 public function uuid() {
|
Chris@0
|
546 return $this->getEntityKey('uuid');
|
Chris@0
|
547 }
|
Chris@0
|
548
|
Chris@0
|
549 /**
|
Chris@0
|
550 * {@inheritdoc}
|
Chris@0
|
551 */
|
Chris@0
|
552 public function hasField($field_name) {
|
Chris@0
|
553 return (bool) $this->getFieldDefinition($field_name);
|
Chris@0
|
554 }
|
Chris@0
|
555
|
Chris@0
|
556 /**
|
Chris@0
|
557 * {@inheritdoc}
|
Chris@0
|
558 */
|
Chris@0
|
559 public function get($field_name) {
|
Chris@0
|
560 if (!isset($this->fields[$field_name][$this->activeLangcode])) {
|
Chris@0
|
561 return $this->getTranslatedField($field_name, $this->activeLangcode);
|
Chris@0
|
562 }
|
Chris@0
|
563 return $this->fields[$field_name][$this->activeLangcode];
|
Chris@0
|
564 }
|
Chris@0
|
565
|
Chris@0
|
566 /**
|
Chris@0
|
567 * Gets a translated field.
|
Chris@0
|
568 *
|
Chris@0
|
569 * @return \Drupal\Core\Field\FieldItemListInterface
|
Chris@0
|
570 */
|
Chris@0
|
571 protected function getTranslatedField($name, $langcode) {
|
Chris@0
|
572 if ($this->translations[$this->activeLangcode]['status'] == static::TRANSLATION_REMOVED) {
|
Chris@0
|
573 throw new \InvalidArgumentException("The entity object refers to a removed translation ({$this->activeLangcode}) and cannot be manipulated.");
|
Chris@0
|
574 }
|
Chris@0
|
575 // Populate $this->fields to speed-up further look-ups and to keep track of
|
Chris@0
|
576 // fields objects, possibly holding changes to field values.
|
Chris@0
|
577 if (!isset($this->fields[$name][$langcode])) {
|
Chris@0
|
578 $definition = $this->getFieldDefinition($name);
|
Chris@0
|
579 if (!$definition) {
|
Chris@0
|
580 throw new \InvalidArgumentException("Field $name is unknown.");
|
Chris@0
|
581 }
|
Chris@0
|
582 // Non-translatable fields are always stored with
|
Chris@0
|
583 // LanguageInterface::LANGCODE_DEFAULT as key.
|
Chris@0
|
584
|
Chris@0
|
585 $default = $langcode == LanguageInterface::LANGCODE_DEFAULT;
|
Chris@0
|
586 if (!$default && !$definition->isTranslatable()) {
|
Chris@0
|
587 if (!isset($this->fields[$name][LanguageInterface::LANGCODE_DEFAULT])) {
|
Chris@0
|
588 $this->fields[$name][LanguageInterface::LANGCODE_DEFAULT] = $this->getTranslatedField($name, LanguageInterface::LANGCODE_DEFAULT);
|
Chris@0
|
589 }
|
Chris@0
|
590 $this->fields[$name][$langcode] = &$this->fields[$name][LanguageInterface::LANGCODE_DEFAULT];
|
Chris@0
|
591 }
|
Chris@0
|
592 else {
|
Chris@0
|
593 $value = NULL;
|
Chris@0
|
594 if (isset($this->values[$name][$langcode])) {
|
Chris@0
|
595 $value = $this->values[$name][$langcode];
|
Chris@0
|
596 }
|
Chris@0
|
597 $field = \Drupal::service('plugin.manager.field.field_type')->createFieldItemList($this->getTranslation($langcode), $name, $value);
|
Chris@0
|
598 if ($default) {
|
Chris@0
|
599 // $this->defaultLangcode might not be set if we are initializing the
|
Chris@0
|
600 // default language code cache, in which case there is no valid
|
Chris@0
|
601 // langcode to assign.
|
Chris@0
|
602 $field_langcode = isset($this->defaultLangcode) ? $this->defaultLangcode : LanguageInterface::LANGCODE_NOT_SPECIFIED;
|
Chris@0
|
603 }
|
Chris@0
|
604 else {
|
Chris@0
|
605 $field_langcode = $langcode;
|
Chris@0
|
606 }
|
Chris@0
|
607 $field->setLangcode($field_langcode);
|
Chris@0
|
608 $this->fields[$name][$langcode] = $field;
|
Chris@0
|
609 }
|
Chris@0
|
610 }
|
Chris@0
|
611 return $this->fields[$name][$langcode];
|
Chris@0
|
612 }
|
Chris@0
|
613
|
Chris@0
|
614 /**
|
Chris@0
|
615 * {@inheritdoc}
|
Chris@0
|
616 */
|
Chris@0
|
617 public function set($name, $value, $notify = TRUE) {
|
Chris@0
|
618 // Assign the value on the child and overrule notify such that we get
|
Chris@0
|
619 // notified to handle changes afterwards. We can ignore notify as there is
|
Chris@0
|
620 // no parent to notify anyway.
|
Chris@0
|
621 $this->get($name)->setValue($value, TRUE);
|
Chris@0
|
622 return $this;
|
Chris@0
|
623 }
|
Chris@0
|
624
|
Chris@0
|
625 /**
|
Chris@0
|
626 * {@inheritdoc}
|
Chris@0
|
627 */
|
Chris@0
|
628 public function getFields($include_computed = TRUE) {
|
Chris@0
|
629 $fields = [];
|
Chris@0
|
630 foreach ($this->getFieldDefinitions() as $name => $definition) {
|
Chris@0
|
631 if ($include_computed || !$definition->isComputed()) {
|
Chris@0
|
632 $fields[$name] = $this->get($name);
|
Chris@0
|
633 }
|
Chris@0
|
634 }
|
Chris@0
|
635 return $fields;
|
Chris@0
|
636 }
|
Chris@0
|
637
|
Chris@0
|
638 /**
|
Chris@0
|
639 * {@inheritdoc}
|
Chris@0
|
640 */
|
Chris@0
|
641 public function getTranslatableFields($include_computed = TRUE) {
|
Chris@0
|
642 $fields = [];
|
Chris@0
|
643 foreach ($this->getFieldDefinitions() as $name => $definition) {
|
Chris@0
|
644 if (($include_computed || !$definition->isComputed()) && $definition->isTranslatable()) {
|
Chris@0
|
645 $fields[$name] = $this->get($name);
|
Chris@0
|
646 }
|
Chris@0
|
647 }
|
Chris@0
|
648 return $fields;
|
Chris@0
|
649 }
|
Chris@0
|
650
|
Chris@0
|
651 /**
|
Chris@0
|
652 * {@inheritdoc}
|
Chris@0
|
653 */
|
Chris@0
|
654 public function getIterator() {
|
Chris@0
|
655 return new \ArrayIterator($this->getFields());
|
Chris@0
|
656 }
|
Chris@0
|
657
|
Chris@0
|
658 /**
|
Chris@0
|
659 * {@inheritdoc}
|
Chris@0
|
660 */
|
Chris@0
|
661 public function getFieldDefinition($name) {
|
Chris@0
|
662 if (!isset($this->fieldDefinitions)) {
|
Chris@0
|
663 $this->getFieldDefinitions();
|
Chris@0
|
664 }
|
Chris@0
|
665 if (isset($this->fieldDefinitions[$name])) {
|
Chris@0
|
666 return $this->fieldDefinitions[$name];
|
Chris@0
|
667 }
|
Chris@0
|
668 }
|
Chris@0
|
669
|
Chris@0
|
670 /**
|
Chris@0
|
671 * {@inheritdoc}
|
Chris@0
|
672 */
|
Chris@0
|
673 public function getFieldDefinitions() {
|
Chris@0
|
674 if (!isset($this->fieldDefinitions)) {
|
Chris@0
|
675 $this->fieldDefinitions = $this->entityManager()->getFieldDefinitions($this->entityTypeId, $this->bundle());
|
Chris@0
|
676 }
|
Chris@0
|
677 return $this->fieldDefinitions;
|
Chris@0
|
678 }
|
Chris@0
|
679
|
Chris@0
|
680 /**
|
Chris@0
|
681 * {@inheritdoc}
|
Chris@0
|
682 */
|
Chris@0
|
683 public function toArray() {
|
Chris@0
|
684 $values = [];
|
Chris@0
|
685 foreach ($this->getFields() as $name => $property) {
|
Chris@0
|
686 $values[$name] = $property->getValue();
|
Chris@0
|
687 }
|
Chris@0
|
688 return $values;
|
Chris@0
|
689 }
|
Chris@0
|
690
|
Chris@0
|
691 /**
|
Chris@0
|
692 * {@inheritdoc}
|
Chris@0
|
693 */
|
Chris@0
|
694 public function access($operation, AccountInterface $account = NULL, $return_as_object = FALSE) {
|
Chris@0
|
695 if ($operation == 'create') {
|
Chris@0
|
696 return $this->entityManager()
|
Chris@0
|
697 ->getAccessControlHandler($this->entityTypeId)
|
Chris@0
|
698 ->createAccess($this->bundle(), $account, [], $return_as_object);
|
Chris@0
|
699 }
|
Chris@0
|
700 return $this->entityManager()
|
Chris@0
|
701 ->getAccessControlHandler($this->entityTypeId)
|
Chris@0
|
702 ->access($this, $operation, $account, $return_as_object);
|
Chris@0
|
703 }
|
Chris@0
|
704
|
Chris@0
|
705 /**
|
Chris@0
|
706 * {@inheritdoc}
|
Chris@0
|
707 */
|
Chris@0
|
708 public function language() {
|
Chris@0
|
709 $language = NULL;
|
Chris@0
|
710 if ($this->activeLangcode != LanguageInterface::LANGCODE_DEFAULT) {
|
Chris@0
|
711 if (!isset($this->languages[$this->activeLangcode])) {
|
Chris@0
|
712 $this->getLanguages();
|
Chris@0
|
713 }
|
Chris@0
|
714 $language = $this->languages[$this->activeLangcode];
|
Chris@0
|
715 }
|
Chris@0
|
716 else {
|
Chris@0
|
717 // @todo Avoid this check by getting the language from the language
|
Chris@0
|
718 // manager directly in https://www.drupal.org/node/2303877.
|
Chris@0
|
719 if (!isset($this->languages[$this->defaultLangcode])) {
|
Chris@0
|
720 $this->getLanguages();
|
Chris@0
|
721 }
|
Chris@0
|
722 $language = $this->languages[$this->defaultLangcode];
|
Chris@0
|
723 }
|
Chris@0
|
724 return $language;
|
Chris@0
|
725 }
|
Chris@0
|
726
|
Chris@0
|
727 /**
|
Chris@0
|
728 * Populates the local cache for the default language code.
|
Chris@0
|
729 */
|
Chris@0
|
730 protected function setDefaultLangcode() {
|
Chris@0
|
731 // Get the language code if the property exists.
|
Chris@0
|
732 // Try to read the value directly from the list of entity keys which got
|
Chris@0
|
733 // initialized in __construct(). This avoids creating a field item object.
|
Chris@0
|
734 if (isset($this->translatableEntityKeys['langcode'][$this->activeLangcode])) {
|
Chris@0
|
735 $this->defaultLangcode = $this->translatableEntityKeys['langcode'][$this->activeLangcode];
|
Chris@0
|
736 }
|
Chris@0
|
737 elseif ($this->hasField($this->langcodeKey) && ($item = $this->get($this->langcodeKey)) && isset($item->language)) {
|
Chris@0
|
738 $this->defaultLangcode = $item->language->getId();
|
Chris@0
|
739 $this->translatableEntityKeys['langcode'][$this->activeLangcode] = $this->defaultLangcode;
|
Chris@0
|
740 }
|
Chris@0
|
741
|
Chris@0
|
742 if (empty($this->defaultLangcode)) {
|
Chris@0
|
743 // Make sure we return a proper language object, if the entity has a
|
Chris@0
|
744 // langcode field, default to the site's default language.
|
Chris@0
|
745 if ($this->hasField($this->langcodeKey)) {
|
Chris@0
|
746 $this->defaultLangcode = $this->languageManager()->getDefaultLanguage()->getId();
|
Chris@0
|
747 }
|
Chris@0
|
748 else {
|
Chris@0
|
749 $this->defaultLangcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;
|
Chris@0
|
750 }
|
Chris@0
|
751 }
|
Chris@0
|
752
|
Chris@0
|
753 // This needs to be initialized manually as it is skipped when instantiating
|
Chris@0
|
754 // the language field object to avoid infinite recursion.
|
Chris@0
|
755 if (!empty($this->fields[$this->langcodeKey])) {
|
Chris@0
|
756 $this->fields[$this->langcodeKey][LanguageInterface::LANGCODE_DEFAULT]->setLangcode($this->defaultLangcode);
|
Chris@0
|
757 }
|
Chris@0
|
758 }
|
Chris@0
|
759
|
Chris@0
|
760 /**
|
Chris@0
|
761 * Updates language for already instantiated fields.
|
Chris@0
|
762 */
|
Chris@0
|
763 protected function updateFieldLangcodes($langcode) {
|
Chris@0
|
764 foreach ($this->fields as $name => $items) {
|
Chris@0
|
765 if (!empty($items[LanguageInterface::LANGCODE_DEFAULT])) {
|
Chris@0
|
766 $items[LanguageInterface::LANGCODE_DEFAULT]->setLangcode($langcode);
|
Chris@0
|
767 }
|
Chris@0
|
768 }
|
Chris@0
|
769 }
|
Chris@0
|
770
|
Chris@0
|
771 /**
|
Chris@0
|
772 * {@inheritdoc}
|
Chris@0
|
773 */
|
Chris@0
|
774 public function onChange($name) {
|
Chris@0
|
775 // Check if the changed name is the value of an entity key and if the value
|
Chris@0
|
776 // of that is currently cached, if so, reset it. Exclude the bundle from
|
Chris@0
|
777 // that check, as it ready only and must not change, unsetting it could
|
Chris@0
|
778 // lead to recursions.
|
Chris@0
|
779 if ($key = array_search($name, $this->getEntityType()->getKeys())) {
|
Chris@0
|
780 if ($key != 'bundle') {
|
Chris@0
|
781 if (isset($this->entityKeys[$key])) {
|
Chris@0
|
782 unset($this->entityKeys[$key]);
|
Chris@0
|
783 }
|
Chris@0
|
784 elseif (isset($this->translatableEntityKeys[$key][$this->activeLangcode])) {
|
Chris@0
|
785 unset($this->translatableEntityKeys[$key][$this->activeLangcode]);
|
Chris@0
|
786 }
|
Chris@12
|
787 // If the revision identifier field is being populated with the original
|
Chris@12
|
788 // value, we need to make sure the "new revision" flag is reset
|
Chris@12
|
789 // accordingly.
|
Chris@14
|
790 if ($key === 'revision' && $this->getRevisionId() == $this->getLoadedRevisionId() && !$this->isNew()) {
|
Chris@12
|
791 $this->newRevision = FALSE;
|
Chris@12
|
792 }
|
Chris@0
|
793 }
|
Chris@0
|
794 }
|
Chris@0
|
795
|
Chris@0
|
796 switch ($name) {
|
Chris@0
|
797 case $this->langcodeKey:
|
Chris@0
|
798 if ($this->isDefaultTranslation()) {
|
Chris@0
|
799 // Update the default internal language cache.
|
Chris@0
|
800 $this->setDefaultLangcode();
|
Chris@0
|
801 if (isset($this->translations[$this->defaultLangcode])) {
|
Chris@0
|
802 $message = SafeMarkup::format('A translation already exists for the specified language (@langcode).', ['@langcode' => $this->defaultLangcode]);
|
Chris@0
|
803 throw new \InvalidArgumentException($message);
|
Chris@0
|
804 }
|
Chris@0
|
805 $this->updateFieldLangcodes($this->defaultLangcode);
|
Chris@0
|
806 }
|
Chris@0
|
807 else {
|
Chris@0
|
808 // @todo Allow the translation language to be changed. See
|
Chris@0
|
809 // https://www.drupal.org/node/2443989.
|
Chris@0
|
810 $items = $this->get($this->langcodeKey);
|
Chris@0
|
811 if ($items->value != $this->activeLangcode) {
|
Chris@0
|
812 $items->setValue($this->activeLangcode, FALSE);
|
Chris@0
|
813 $message = SafeMarkup::format('The translation language cannot be changed (@langcode).', ['@langcode' => $this->activeLangcode]);
|
Chris@0
|
814 throw new \LogicException($message);
|
Chris@0
|
815 }
|
Chris@0
|
816 }
|
Chris@0
|
817 break;
|
Chris@0
|
818
|
Chris@0
|
819 case $this->defaultLangcodeKey:
|
Chris@0
|
820 // @todo Use a standard method to make the default_langcode field
|
Chris@0
|
821 // read-only. See https://www.drupal.org/node/2443991.
|
Chris@0
|
822 if (isset($this->values[$this->defaultLangcodeKey]) && $this->get($this->defaultLangcodeKey)->value != $this->isDefaultTranslation()) {
|
Chris@0
|
823 $this->get($this->defaultLangcodeKey)->setValue($this->isDefaultTranslation(), FALSE);
|
Chris@0
|
824 $message = SafeMarkup::format('The default translation flag cannot be changed (@langcode).', ['@langcode' => $this->activeLangcode]);
|
Chris@0
|
825 throw new \LogicException($message);
|
Chris@0
|
826 }
|
Chris@0
|
827 break;
|
Chris@14
|
828
|
Chris@14
|
829 case $this->revisionTranslationAffectedKey:
|
Chris@14
|
830 // If the revision translation affected flag is being set then enforce
|
Chris@14
|
831 // its value.
|
Chris@14
|
832 $this->setRevisionTranslationAffectedEnforced(TRUE);
|
Chris@14
|
833 break;
|
Chris@0
|
834 }
|
Chris@0
|
835 }
|
Chris@0
|
836
|
Chris@0
|
837 /**
|
Chris@0
|
838 * {@inheritdoc}
|
Chris@0
|
839 */
|
Chris@0
|
840 public function getTranslation($langcode) {
|
Chris@0
|
841 // Ensure we always use the default language code when dealing with the
|
Chris@0
|
842 // original entity language.
|
Chris@0
|
843 if ($langcode != LanguageInterface::LANGCODE_DEFAULT && $langcode == $this->defaultLangcode) {
|
Chris@0
|
844 $langcode = LanguageInterface::LANGCODE_DEFAULT;
|
Chris@0
|
845 }
|
Chris@0
|
846
|
Chris@0
|
847 // Populate entity translation object cache so it will be available for all
|
Chris@0
|
848 // translation objects.
|
Chris@0
|
849 if ($langcode == $this->activeLangcode) {
|
Chris@0
|
850 $this->translations[$langcode]['entity'] = $this;
|
Chris@0
|
851 }
|
Chris@0
|
852
|
Chris@0
|
853 // If we already have a translation object for the specified language we can
|
Chris@0
|
854 // just return it.
|
Chris@0
|
855 if (isset($this->translations[$langcode]['entity'])) {
|
Chris@0
|
856 $translation = $this->translations[$langcode]['entity'];
|
Chris@0
|
857 }
|
Chris@0
|
858 // Otherwise if an existing translation language was specified we need to
|
Chris@0
|
859 // instantiate the related translation.
|
Chris@0
|
860 elseif (isset($this->translations[$langcode])) {
|
Chris@0
|
861 $translation = $this->initializeTranslation($langcode);
|
Chris@0
|
862 $this->translations[$langcode]['entity'] = $translation;
|
Chris@0
|
863 }
|
Chris@0
|
864
|
Chris@0
|
865 if (empty($translation)) {
|
Chris@0
|
866 throw new \InvalidArgumentException("Invalid translation language ($langcode) specified.");
|
Chris@0
|
867 }
|
Chris@0
|
868
|
Chris@0
|
869 return $translation;
|
Chris@0
|
870 }
|
Chris@0
|
871
|
Chris@0
|
872 /**
|
Chris@0
|
873 * {@inheritdoc}
|
Chris@0
|
874 */
|
Chris@0
|
875 public function getUntranslated() {
|
Chris@0
|
876 return $this->getTranslation(LanguageInterface::LANGCODE_DEFAULT);
|
Chris@0
|
877 }
|
Chris@0
|
878
|
Chris@0
|
879 /**
|
Chris@0
|
880 * Instantiates a translation object for an existing translation.
|
Chris@0
|
881 *
|
Chris@0
|
882 * The translated entity will be a clone of the current entity with the
|
Chris@0
|
883 * specified $langcode. All translations share the same field data structures
|
Chris@0
|
884 * to ensure that all of them deal with fresh data.
|
Chris@0
|
885 *
|
Chris@0
|
886 * @param string $langcode
|
Chris@0
|
887 * The language code for the requested translation.
|
Chris@0
|
888 *
|
Chris@0
|
889 * @return \Drupal\Core\Entity\EntityInterface
|
Chris@0
|
890 * The translation object. The content properties of the translation object
|
Chris@0
|
891 * are stored as references to the main entity.
|
Chris@0
|
892 */
|
Chris@0
|
893 protected function initializeTranslation($langcode) {
|
Chris@0
|
894 // If the requested translation is valid, clone it with the current language
|
Chris@0
|
895 // as the active language. The $translationInitialize flag triggers a
|
Chris@0
|
896 // shallow (non-recursive) clone.
|
Chris@0
|
897 $this->translationInitialize = TRUE;
|
Chris@0
|
898 $translation = clone $this;
|
Chris@0
|
899 $this->translationInitialize = FALSE;
|
Chris@0
|
900
|
Chris@0
|
901 $translation->activeLangcode = $langcode;
|
Chris@0
|
902
|
Chris@0
|
903 // Ensure that changes to fields, values and translations are propagated
|
Chris@0
|
904 // to all the translation objects.
|
Chris@0
|
905 // @todo Consider converting these to ArrayObject.
|
Chris@0
|
906 $translation->values = &$this->values;
|
Chris@0
|
907 $translation->fields = &$this->fields;
|
Chris@0
|
908 $translation->translations = &$this->translations;
|
Chris@0
|
909 $translation->enforceIsNew = &$this->enforceIsNew;
|
Chris@0
|
910 $translation->newRevision = &$this->newRevision;
|
Chris@0
|
911 $translation->entityKeys = &$this->entityKeys;
|
Chris@0
|
912 $translation->translatableEntityKeys = &$this->translatableEntityKeys;
|
Chris@0
|
913 $translation->translationInitialize = FALSE;
|
Chris@0
|
914 $translation->typedData = NULL;
|
Chris@0
|
915 $translation->loadedRevisionId = &$this->loadedRevisionId;
|
Chris@0
|
916 $translation->isDefaultRevision = &$this->isDefaultRevision;
|
Chris@14
|
917 $translation->enforceRevisionTranslationAffected = &$this->enforceRevisionTranslationAffected;
|
Chris@0
|
918
|
Chris@0
|
919 return $translation;
|
Chris@0
|
920 }
|
Chris@0
|
921
|
Chris@0
|
922 /**
|
Chris@0
|
923 * {@inheritdoc}
|
Chris@0
|
924 */
|
Chris@0
|
925 public function hasTranslation($langcode) {
|
Chris@0
|
926 if ($langcode == $this->defaultLangcode) {
|
Chris@0
|
927 $langcode = LanguageInterface::LANGCODE_DEFAULT;
|
Chris@0
|
928 }
|
Chris@0
|
929 return !empty($this->translations[$langcode]['status']);
|
Chris@0
|
930 }
|
Chris@0
|
931
|
Chris@0
|
932 /**
|
Chris@0
|
933 * {@inheritdoc}
|
Chris@0
|
934 */
|
Chris@0
|
935 public function isNewTranslation() {
|
Chris@0
|
936 return $this->translations[$this->activeLangcode]['status'] == static::TRANSLATION_CREATED;
|
Chris@0
|
937 }
|
Chris@0
|
938
|
Chris@0
|
939 /**
|
Chris@0
|
940 * {@inheritdoc}
|
Chris@0
|
941 */
|
Chris@0
|
942 public function addTranslation($langcode, array $values = []) {
|
Chris@0
|
943 // Make sure we do not attempt to create a translation if an invalid
|
Chris@0
|
944 // language is specified or the entity cannot be translated.
|
Chris@0
|
945 $this->getLanguages();
|
Chris@0
|
946 if (!isset($this->languages[$langcode]) || $this->hasTranslation($langcode) || $this->languages[$langcode]->isLocked()) {
|
Chris@0
|
947 throw new \InvalidArgumentException("Invalid translation language ($langcode) specified.");
|
Chris@0
|
948 }
|
Chris@0
|
949 if ($this->languages[$this->defaultLangcode]->isLocked()) {
|
Chris@0
|
950 throw new \InvalidArgumentException("The entity cannot be translated since it is language neutral ({$this->defaultLangcode}).");
|
Chris@0
|
951 }
|
Chris@0
|
952
|
Chris@0
|
953 // Initialize the translation object.
|
Chris@0
|
954 /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
|
Chris@0
|
955 $storage = $this->entityManager()->getStorage($this->getEntityTypeId());
|
Chris@0
|
956 $this->translations[$langcode]['status'] = !isset($this->translations[$langcode]['status_existed']) ? static::TRANSLATION_CREATED : static::TRANSLATION_EXISTING;
|
Chris@0
|
957 return $storage->createTranslation($this, $langcode, $values);
|
Chris@0
|
958 }
|
Chris@0
|
959
|
Chris@0
|
960 /**
|
Chris@0
|
961 * {@inheritdoc}
|
Chris@0
|
962 */
|
Chris@0
|
963 public function removeTranslation($langcode) {
|
Chris@0
|
964 if (isset($this->translations[$langcode]) && $langcode != LanguageInterface::LANGCODE_DEFAULT && $langcode != $this->defaultLangcode) {
|
Chris@0
|
965 foreach ($this->getFieldDefinitions() as $name => $definition) {
|
Chris@0
|
966 if ($definition->isTranslatable()) {
|
Chris@0
|
967 unset($this->values[$name][$langcode]);
|
Chris@0
|
968 unset($this->fields[$name][$langcode]);
|
Chris@0
|
969 }
|
Chris@0
|
970 }
|
Chris@0
|
971 // If removing a translation which has not been saved yet, then we have
|
Chris@0
|
972 // to remove it completely so that ::getTranslationStatus returns the
|
Chris@0
|
973 // proper status.
|
Chris@0
|
974 if ($this->translations[$langcode]['status'] == static::TRANSLATION_CREATED) {
|
Chris@0
|
975 unset($this->translations[$langcode]);
|
Chris@0
|
976 }
|
Chris@0
|
977 else {
|
Chris@0
|
978 if ($this->translations[$langcode]['status'] == static::TRANSLATION_EXISTING) {
|
Chris@0
|
979 $this->translations[$langcode]['status_existed'] = TRUE;
|
Chris@0
|
980 }
|
Chris@0
|
981 $this->translations[$langcode]['status'] = static::TRANSLATION_REMOVED;
|
Chris@0
|
982 }
|
Chris@0
|
983 }
|
Chris@0
|
984 else {
|
Chris@0
|
985 throw new \InvalidArgumentException("The specified translation ($langcode) cannot be removed.");
|
Chris@0
|
986 }
|
Chris@0
|
987 }
|
Chris@0
|
988
|
Chris@0
|
989 /**
|
Chris@0
|
990 * {@inheritdoc}
|
Chris@0
|
991 */
|
Chris@0
|
992 public function getTranslationStatus($langcode) {
|
Chris@0
|
993 if ($langcode == $this->defaultLangcode) {
|
Chris@0
|
994 $langcode = LanguageInterface::LANGCODE_DEFAULT;
|
Chris@0
|
995 }
|
Chris@0
|
996 return isset($this->translations[$langcode]) ? $this->translations[$langcode]['status'] : NULL;
|
Chris@0
|
997 }
|
Chris@0
|
998
|
Chris@0
|
999 /**
|
Chris@0
|
1000 * {@inheritdoc}
|
Chris@0
|
1001 */
|
Chris@0
|
1002 public function getTranslationLanguages($include_default = TRUE) {
|
Chris@0
|
1003 $translations = array_filter($this->translations, function ($translation) {
|
Chris@0
|
1004 return $translation['status'];
|
Chris@0
|
1005 });
|
Chris@0
|
1006 unset($translations[LanguageInterface::LANGCODE_DEFAULT]);
|
Chris@0
|
1007
|
Chris@0
|
1008 if ($include_default) {
|
Chris@0
|
1009 $translations[$this->defaultLangcode] = TRUE;
|
Chris@0
|
1010 }
|
Chris@0
|
1011
|
Chris@0
|
1012 // Now load language objects based upon translation langcodes.
|
Chris@0
|
1013 return array_intersect_key($this->getLanguages(), $translations);
|
Chris@0
|
1014 }
|
Chris@0
|
1015
|
Chris@0
|
1016 /**
|
Chris@0
|
1017 * Updates the original values with the interim changes.
|
Chris@0
|
1018 */
|
Chris@0
|
1019 public function updateOriginalValues() {
|
Chris@0
|
1020 if (!$this->fields) {
|
Chris@0
|
1021 return;
|
Chris@0
|
1022 }
|
Chris@0
|
1023 foreach ($this->getFieldDefinitions() as $name => $definition) {
|
Chris@0
|
1024 if (!$definition->isComputed() && !empty($this->fields[$name])) {
|
Chris@0
|
1025 foreach ($this->fields[$name] as $langcode => $item) {
|
Chris@0
|
1026 $item->filterEmptyItems();
|
Chris@0
|
1027 $this->values[$name][$langcode] = $item->getValue();
|
Chris@0
|
1028 }
|
Chris@0
|
1029 }
|
Chris@0
|
1030 }
|
Chris@0
|
1031 }
|
Chris@0
|
1032
|
Chris@0
|
1033 /**
|
Chris@0
|
1034 * Implements the magic method for getting object properties.
|
Chris@0
|
1035 *
|
Chris@0
|
1036 * @todo: A lot of code still uses non-fields (e.g. $entity->content in view
|
Chris@0
|
1037 * builders) by reference. Clean that up.
|
Chris@0
|
1038 */
|
Chris@0
|
1039 public function &__get($name) {
|
Chris@0
|
1040 // If this is an entity field, handle it accordingly. We first check whether
|
Chris@0
|
1041 // a field object has been already created. If not, we create one.
|
Chris@0
|
1042 if (isset($this->fields[$name][$this->activeLangcode])) {
|
Chris@0
|
1043 return $this->fields[$name][$this->activeLangcode];
|
Chris@0
|
1044 }
|
Chris@0
|
1045 // Inline getFieldDefinition() to speed things up.
|
Chris@0
|
1046 if (!isset($this->fieldDefinitions)) {
|
Chris@0
|
1047 $this->getFieldDefinitions();
|
Chris@0
|
1048 }
|
Chris@0
|
1049 if (isset($this->fieldDefinitions[$name])) {
|
Chris@0
|
1050 $return = $this->getTranslatedField($name, $this->activeLangcode);
|
Chris@0
|
1051 return $return;
|
Chris@0
|
1052 }
|
Chris@0
|
1053 // Else directly read/write plain values. That way, non-field entity
|
Chris@0
|
1054 // properties can always be accessed directly.
|
Chris@0
|
1055 if (!isset($this->values[$name])) {
|
Chris@0
|
1056 $this->values[$name] = NULL;
|
Chris@0
|
1057 }
|
Chris@0
|
1058 return $this->values[$name];
|
Chris@0
|
1059 }
|
Chris@0
|
1060
|
Chris@0
|
1061 /**
|
Chris@0
|
1062 * Implements the magic method for setting object properties.
|
Chris@0
|
1063 *
|
Chris@0
|
1064 * Uses default language always.
|
Chris@0
|
1065 */
|
Chris@0
|
1066 public function __set($name, $value) {
|
Chris@0
|
1067 // Inline getFieldDefinition() to speed things up.
|
Chris@0
|
1068 if (!isset($this->fieldDefinitions)) {
|
Chris@0
|
1069 $this->getFieldDefinitions();
|
Chris@0
|
1070 }
|
Chris@0
|
1071 // Handle Field API fields.
|
Chris@0
|
1072 if (isset($this->fieldDefinitions[$name])) {
|
Chris@0
|
1073 // Support setting values via property objects.
|
Chris@0
|
1074 if ($value instanceof TypedDataInterface) {
|
Chris@0
|
1075 $value = $value->getValue();
|
Chris@0
|
1076 }
|
Chris@0
|
1077 // If a FieldItemList object already exists, set its value.
|
Chris@0
|
1078 if (isset($this->fields[$name][$this->activeLangcode])) {
|
Chris@0
|
1079 $this->fields[$name][$this->activeLangcode]->setValue($value);
|
Chris@0
|
1080 }
|
Chris@0
|
1081 // If not, create one.
|
Chris@0
|
1082 else {
|
Chris@0
|
1083 $this->getTranslatedField($name, $this->activeLangcode)->setValue($value);
|
Chris@0
|
1084 }
|
Chris@0
|
1085 }
|
Chris@0
|
1086 // The translations array is unset when cloning the entity object, we just
|
Chris@0
|
1087 // need to restore it.
|
Chris@0
|
1088 elseif ($name == 'translations') {
|
Chris@0
|
1089 $this->translations = $value;
|
Chris@0
|
1090 }
|
Chris@0
|
1091 // Directly write non-field values.
|
Chris@0
|
1092 else {
|
Chris@0
|
1093 $this->values[$name] = $value;
|
Chris@0
|
1094 }
|
Chris@0
|
1095 }
|
Chris@0
|
1096
|
Chris@0
|
1097 /**
|
Chris@0
|
1098 * Implements the magic method for isset().
|
Chris@0
|
1099 */
|
Chris@0
|
1100 public function __isset($name) {
|
Chris@0
|
1101 // "Official" Field API fields are always set. For non-field properties,
|
Chris@0
|
1102 // check the internal values.
|
Chris@0
|
1103 return $this->hasField($name) ? TRUE : isset($this->values[$name]);
|
Chris@0
|
1104 }
|
Chris@0
|
1105
|
Chris@0
|
1106 /**
|
Chris@0
|
1107 * Implements the magic method for unset().
|
Chris@0
|
1108 */
|
Chris@0
|
1109 public function __unset($name) {
|
Chris@0
|
1110 // Unsetting a field means emptying it.
|
Chris@0
|
1111 if ($this->hasField($name)) {
|
Chris@0
|
1112 $this->get($name)->setValue([]);
|
Chris@0
|
1113 }
|
Chris@0
|
1114 // For non-field properties, unset the internal value.
|
Chris@0
|
1115 else {
|
Chris@0
|
1116 unset($this->values[$name]);
|
Chris@0
|
1117 }
|
Chris@0
|
1118 }
|
Chris@0
|
1119
|
Chris@0
|
1120 /**
|
Chris@0
|
1121 * {@inheritdoc}
|
Chris@0
|
1122 */
|
Chris@0
|
1123 public function createDuplicate() {
|
Chris@0
|
1124 if ($this->translations[$this->activeLangcode]['status'] == static::TRANSLATION_REMOVED) {
|
Chris@0
|
1125 throw new \InvalidArgumentException("The entity object refers to a removed translation ({$this->activeLangcode}) and cannot be manipulated.");
|
Chris@0
|
1126 }
|
Chris@0
|
1127
|
Chris@0
|
1128 $duplicate = clone $this;
|
Chris@0
|
1129 $entity_type = $this->getEntityType();
|
Chris@14
|
1130 if ($entity_type->hasKey('id')) {
|
Chris@14
|
1131 $duplicate->{$entity_type->getKey('id')}->value = NULL;
|
Chris@14
|
1132 }
|
Chris@0
|
1133 $duplicate->enforceIsNew();
|
Chris@0
|
1134
|
Chris@0
|
1135 // Check if the entity type supports UUIDs and generate a new one if so.
|
Chris@0
|
1136 if ($entity_type->hasKey('uuid')) {
|
Chris@0
|
1137 $duplicate->{$entity_type->getKey('uuid')}->value = $this->uuidGenerator()->generate();
|
Chris@0
|
1138 }
|
Chris@0
|
1139
|
Chris@0
|
1140 // Check whether the entity type supports revisions and initialize it if so.
|
Chris@0
|
1141 if ($entity_type->isRevisionable()) {
|
Chris@0
|
1142 $duplicate->{$entity_type->getKey('revision')}->value = NULL;
|
Chris@0
|
1143 $duplicate->loadedRevisionId = NULL;
|
Chris@0
|
1144 }
|
Chris@0
|
1145
|
Chris@0
|
1146 return $duplicate;
|
Chris@0
|
1147 }
|
Chris@0
|
1148
|
Chris@0
|
1149 /**
|
Chris@0
|
1150 * Magic method: Implements a deep clone.
|
Chris@0
|
1151 */
|
Chris@0
|
1152 public function __clone() {
|
Chris@0
|
1153 // Avoid deep-cloning when we are initializing a translation object, since
|
Chris@0
|
1154 // it will represent the same entity, only with a different active language.
|
Chris@0
|
1155 if ($this->translationInitialize) {
|
Chris@0
|
1156 return;
|
Chris@0
|
1157 }
|
Chris@0
|
1158
|
Chris@0
|
1159 // The translation is a different object, and needs its own TypedData
|
Chris@0
|
1160 // adapter object.
|
Chris@0
|
1161 $this->typedData = NULL;
|
Chris@0
|
1162 $definitions = $this->getFieldDefinitions();
|
Chris@0
|
1163
|
Chris@0
|
1164 // The translation cache has to be cleared before cloning the fields
|
Chris@0
|
1165 // below so that the call to getTranslation() does not re-use the
|
Chris@0
|
1166 // translation objects of the old entity but instead creates new
|
Chris@0
|
1167 // translation objects from the newly cloned entity. Otherwise the newly
|
Chris@0
|
1168 // cloned field item lists would hold references to the old translation
|
Chris@0
|
1169 // objects in their $parent property after the call to setContext().
|
Chris@0
|
1170 $this->clearTranslationCache();
|
Chris@0
|
1171
|
Chris@0
|
1172 // Because the new translation objects that are created below are
|
Chris@0
|
1173 // themselves created by *cloning* the newly cloned entity we need to
|
Chris@0
|
1174 // make sure that the references to property values are properly cloned
|
Chris@0
|
1175 // before cloning the fields. Otherwise calling
|
Chris@0
|
1176 // $items->getEntity()->isNew(), for example, would return the
|
Chris@0
|
1177 // $enforceIsNew value of the old entity.
|
Chris@0
|
1178
|
Chris@0
|
1179 // Ensure the translations array is actually cloned by overwriting the
|
Chris@0
|
1180 // original reference with one pointing to a copy of the array.
|
Chris@0
|
1181 $translations = $this->translations;
|
Chris@0
|
1182 $this->translations = &$translations;
|
Chris@0
|
1183
|
Chris@0
|
1184 // Ensure that the following properties are actually cloned by
|
Chris@0
|
1185 // overwriting the original references with ones pointing to copies of
|
Chris@0
|
1186 // them: enforceIsNew, newRevision, loadedRevisionId, fields, entityKeys,
|
Chris@14
|
1187 // translatableEntityKeys, values, isDefaultRevision and
|
Chris@14
|
1188 // enforceRevisionTranslationAffected.
|
Chris@0
|
1189 $enforce_is_new = $this->enforceIsNew;
|
Chris@0
|
1190 $this->enforceIsNew = &$enforce_is_new;
|
Chris@0
|
1191
|
Chris@0
|
1192 $new_revision = $this->newRevision;
|
Chris@0
|
1193 $this->newRevision = &$new_revision;
|
Chris@0
|
1194
|
Chris@0
|
1195 $original_revision_id = $this->loadedRevisionId;
|
Chris@0
|
1196 $this->loadedRevisionId = &$original_revision_id;
|
Chris@0
|
1197
|
Chris@0
|
1198 $fields = $this->fields;
|
Chris@0
|
1199 $this->fields = &$fields;
|
Chris@0
|
1200
|
Chris@0
|
1201 $entity_keys = $this->entityKeys;
|
Chris@0
|
1202 $this->entityKeys = &$entity_keys;
|
Chris@0
|
1203
|
Chris@0
|
1204 $translatable_entity_keys = $this->translatableEntityKeys;
|
Chris@0
|
1205 $this->translatableEntityKeys = &$translatable_entity_keys;
|
Chris@0
|
1206
|
Chris@0
|
1207 $values = $this->values;
|
Chris@0
|
1208 $this->values = &$values;
|
Chris@0
|
1209
|
Chris@0
|
1210 $default_revision = $this->isDefaultRevision;
|
Chris@0
|
1211 $this->isDefaultRevision = &$default_revision;
|
Chris@0
|
1212
|
Chris@14
|
1213 $is_revision_translation_affected_enforced = $this->enforceRevisionTranslationAffected;
|
Chris@14
|
1214 $this->enforceRevisionTranslationAffected = &$is_revision_translation_affected_enforced;
|
Chris@14
|
1215
|
Chris@0
|
1216 foreach ($this->fields as $name => $fields_by_langcode) {
|
Chris@0
|
1217 $this->fields[$name] = [];
|
Chris@0
|
1218 // Untranslatable fields may have multiple references for the same field
|
Chris@0
|
1219 // object keyed by language. To avoid creating different field objects
|
Chris@0
|
1220 // we retain just the original value, as references will be recreated
|
Chris@0
|
1221 // later as needed.
|
Chris@0
|
1222 if (!$definitions[$name]->isTranslatable() && count($fields_by_langcode) > 1) {
|
Chris@0
|
1223 $fields_by_langcode = array_intersect_key($fields_by_langcode, [LanguageInterface::LANGCODE_DEFAULT => TRUE]);
|
Chris@0
|
1224 }
|
Chris@0
|
1225 foreach ($fields_by_langcode as $langcode => $items) {
|
Chris@0
|
1226 $this->fields[$name][$langcode] = clone $items;
|
Chris@0
|
1227 $this->fields[$name][$langcode]->setContext($name, $this->getTranslation($langcode)->getTypedData());
|
Chris@0
|
1228 }
|
Chris@0
|
1229 }
|
Chris@0
|
1230 }
|
Chris@0
|
1231
|
Chris@0
|
1232 /**
|
Chris@0
|
1233 * {@inheritdoc}
|
Chris@0
|
1234 */
|
Chris@0
|
1235 public function label() {
|
Chris@0
|
1236 $label = NULL;
|
Chris@0
|
1237 $entity_type = $this->getEntityType();
|
Chris@0
|
1238 if (($label_callback = $entity_type->getLabelCallback()) && is_callable($label_callback)) {
|
Chris@0
|
1239 $label = call_user_func($label_callback, $this);
|
Chris@0
|
1240 }
|
Chris@0
|
1241 elseif (($label_key = $entity_type->getKey('label'))) {
|
Chris@0
|
1242 $label = $this->getEntityKey('label');
|
Chris@0
|
1243 }
|
Chris@0
|
1244 return $label;
|
Chris@0
|
1245 }
|
Chris@0
|
1246
|
Chris@0
|
1247 /**
|
Chris@0
|
1248 * {@inheritdoc}
|
Chris@0
|
1249 */
|
Chris@0
|
1250 public function referencedEntities() {
|
Chris@0
|
1251 $referenced_entities = [];
|
Chris@0
|
1252
|
Chris@0
|
1253 // Gather a list of referenced entities.
|
Chris@0
|
1254 foreach ($this->getFields() as $field_items) {
|
Chris@0
|
1255 foreach ($field_items as $field_item) {
|
Chris@0
|
1256 // Loop over all properties of a field item.
|
Chris@0
|
1257 foreach ($field_item->getProperties(TRUE) as $property) {
|
Chris@0
|
1258 if ($property instanceof EntityReference && $entity = $property->getValue()) {
|
Chris@0
|
1259 $referenced_entities[] = $entity;
|
Chris@0
|
1260 }
|
Chris@0
|
1261 }
|
Chris@0
|
1262 }
|
Chris@0
|
1263 }
|
Chris@0
|
1264
|
Chris@0
|
1265 return $referenced_entities;
|
Chris@0
|
1266 }
|
Chris@0
|
1267
|
Chris@0
|
1268 /**
|
Chris@0
|
1269 * Gets the value of the given entity key, if defined.
|
Chris@0
|
1270 *
|
Chris@0
|
1271 * @param string $key
|
Chris@0
|
1272 * Name of the entity key, for example id, revision or bundle.
|
Chris@0
|
1273 *
|
Chris@0
|
1274 * @return mixed
|
Chris@0
|
1275 * The value of the entity key, NULL if not defined.
|
Chris@0
|
1276 */
|
Chris@0
|
1277 protected function getEntityKey($key) {
|
Chris@0
|
1278 // If the value is known already, return it.
|
Chris@0
|
1279 if (isset($this->entityKeys[$key])) {
|
Chris@0
|
1280 return $this->entityKeys[$key];
|
Chris@0
|
1281 }
|
Chris@0
|
1282 if (isset($this->translatableEntityKeys[$key][$this->activeLangcode])) {
|
Chris@0
|
1283 return $this->translatableEntityKeys[$key][$this->activeLangcode];
|
Chris@0
|
1284 }
|
Chris@0
|
1285
|
Chris@0
|
1286 // Otherwise fetch the value by creating a field object.
|
Chris@0
|
1287 $value = NULL;
|
Chris@0
|
1288 if ($this->getEntityType()->hasKey($key)) {
|
Chris@0
|
1289 $field_name = $this->getEntityType()->getKey($key);
|
Chris@0
|
1290 $definition = $this->getFieldDefinition($field_name);
|
Chris@0
|
1291 $property = $definition->getFieldStorageDefinition()->getMainPropertyName();
|
Chris@0
|
1292 $value = $this->get($field_name)->$property;
|
Chris@0
|
1293
|
Chris@0
|
1294 // Put it in the right array, depending on whether it is translatable.
|
Chris@0
|
1295 if ($definition->isTranslatable()) {
|
Chris@0
|
1296 $this->translatableEntityKeys[$key][$this->activeLangcode] = $value;
|
Chris@0
|
1297 }
|
Chris@0
|
1298 else {
|
Chris@0
|
1299 $this->entityKeys[$key] = $value;
|
Chris@0
|
1300 }
|
Chris@0
|
1301 }
|
Chris@0
|
1302 else {
|
Chris@0
|
1303 $this->entityKeys[$key] = $value;
|
Chris@0
|
1304 }
|
Chris@0
|
1305 return $value;
|
Chris@0
|
1306 }
|
Chris@0
|
1307
|
Chris@0
|
1308 /**
|
Chris@0
|
1309 * {@inheritdoc}
|
Chris@0
|
1310 */
|
Chris@0
|
1311 public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
|
Chris@0
|
1312 $fields = [];
|
Chris@0
|
1313 if ($entity_type->hasKey('id')) {
|
Chris@0
|
1314 $fields[$entity_type->getKey('id')] = BaseFieldDefinition::create('integer')
|
Chris@0
|
1315 ->setLabel(new TranslatableMarkup('ID'))
|
Chris@0
|
1316 ->setReadOnly(TRUE)
|
Chris@0
|
1317 ->setSetting('unsigned', TRUE);
|
Chris@0
|
1318 }
|
Chris@0
|
1319 if ($entity_type->hasKey('uuid')) {
|
Chris@0
|
1320 $fields[$entity_type->getKey('uuid')] = BaseFieldDefinition::create('uuid')
|
Chris@0
|
1321 ->setLabel(new TranslatableMarkup('UUID'))
|
Chris@0
|
1322 ->setReadOnly(TRUE);
|
Chris@0
|
1323 }
|
Chris@0
|
1324 if ($entity_type->hasKey('revision')) {
|
Chris@0
|
1325 $fields[$entity_type->getKey('revision')] = BaseFieldDefinition::create('integer')
|
Chris@0
|
1326 ->setLabel(new TranslatableMarkup('Revision ID'))
|
Chris@0
|
1327 ->setReadOnly(TRUE)
|
Chris@0
|
1328 ->setSetting('unsigned', TRUE);
|
Chris@0
|
1329 }
|
Chris@0
|
1330 if ($entity_type->hasKey('langcode')) {
|
Chris@0
|
1331 $fields[$entity_type->getKey('langcode')] = BaseFieldDefinition::create('language')
|
Chris@0
|
1332 ->setLabel(new TranslatableMarkup('Language'))
|
Chris@0
|
1333 ->setDisplayOptions('view', [
|
Chris@0
|
1334 'region' => 'hidden',
|
Chris@0
|
1335 ])
|
Chris@0
|
1336 ->setDisplayOptions('form', [
|
Chris@0
|
1337 'type' => 'language_select',
|
Chris@0
|
1338 'weight' => 2,
|
Chris@0
|
1339 ]);
|
Chris@0
|
1340 if ($entity_type->isRevisionable()) {
|
Chris@0
|
1341 $fields[$entity_type->getKey('langcode')]->setRevisionable(TRUE);
|
Chris@0
|
1342 }
|
Chris@0
|
1343 if ($entity_type->isTranslatable()) {
|
Chris@0
|
1344 $fields[$entity_type->getKey('langcode')]->setTranslatable(TRUE);
|
Chris@0
|
1345 }
|
Chris@0
|
1346 }
|
Chris@0
|
1347 if ($entity_type->hasKey('bundle')) {
|
Chris@0
|
1348 if ($bundle_entity_type_id = $entity_type->getBundleEntityType()) {
|
Chris@0
|
1349 $fields[$entity_type->getKey('bundle')] = BaseFieldDefinition::create('entity_reference')
|
Chris@0
|
1350 ->setLabel($entity_type->getBundleLabel())
|
Chris@0
|
1351 ->setSetting('target_type', $bundle_entity_type_id)
|
Chris@0
|
1352 ->setRequired(TRUE)
|
Chris@0
|
1353 ->setReadOnly(TRUE);
|
Chris@0
|
1354 }
|
Chris@0
|
1355 else {
|
Chris@0
|
1356 $fields[$entity_type->getKey('bundle')] = BaseFieldDefinition::create('string')
|
Chris@0
|
1357 ->setLabel($entity_type->getBundleLabel())
|
Chris@0
|
1358 ->setRequired(TRUE)
|
Chris@0
|
1359 ->setReadOnly(TRUE);
|
Chris@0
|
1360 }
|
Chris@0
|
1361 }
|
Chris@0
|
1362
|
Chris@0
|
1363 return $fields;
|
Chris@0
|
1364 }
|
Chris@0
|
1365
|
Chris@0
|
1366 /**
|
Chris@0
|
1367 * {@inheritdoc}
|
Chris@0
|
1368 */
|
Chris@0
|
1369 public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
|
Chris@0
|
1370 return [];
|
Chris@0
|
1371 }
|
Chris@0
|
1372
|
Chris@0
|
1373 /**
|
Chris@0
|
1374 * Returns an array of field names to skip in ::hasTranslationChanges.
|
Chris@0
|
1375 *
|
Chris@0
|
1376 * @return array
|
Chris@0
|
1377 * An array of field names.
|
Chris@0
|
1378 */
|
Chris@0
|
1379 protected function getFieldsToSkipFromTranslationChangesCheck() {
|
Chris@14
|
1380 return $this->traitGetFieldsToSkipFromTranslationChangesCheck($this);
|
Chris@0
|
1381 }
|
Chris@0
|
1382
|
Chris@0
|
1383 /**
|
Chris@0
|
1384 * {@inheritdoc}
|
Chris@0
|
1385 */
|
Chris@0
|
1386 public function hasTranslationChanges() {
|
Chris@0
|
1387 if ($this->isNew()) {
|
Chris@0
|
1388 return TRUE;
|
Chris@0
|
1389 }
|
Chris@0
|
1390
|
Chris@0
|
1391 // $this->original only exists during save. See
|
Chris@0
|
1392 // \Drupal\Core\Entity\EntityStorageBase::save(). If it exists we re-use it
|
Chris@0
|
1393 // here for performance reasons.
|
Chris@0
|
1394 /** @var \Drupal\Core\Entity\ContentEntityBase $original */
|
Chris@0
|
1395 $original = $this->original ? $this->original : NULL;
|
Chris@0
|
1396
|
Chris@0
|
1397 if (!$original) {
|
Chris@0
|
1398 $id = $this->getOriginalId() !== NULL ? $this->getOriginalId() : $this->id();
|
Chris@0
|
1399 $original = $this->entityManager()->getStorage($this->getEntityTypeId())->loadUnchanged($id);
|
Chris@0
|
1400 }
|
Chris@0
|
1401
|
Chris@0
|
1402 // If the current translation has just been added, we have a change.
|
Chris@0
|
1403 $translated = count($this->translations) > 1;
|
Chris@0
|
1404 if ($translated && !$original->hasTranslation($this->activeLangcode)) {
|
Chris@0
|
1405 return TRUE;
|
Chris@0
|
1406 }
|
Chris@0
|
1407
|
Chris@0
|
1408 // Compare field item current values with the original ones to determine
|
Chris@0
|
1409 // whether we have changes. If a field is not translatable and the entity is
|
Chris@0
|
1410 // translated we skip it because, depending on the use case, it would make
|
Chris@0
|
1411 // sense to mark all translations as changed or none of them. We skip also
|
Chris@0
|
1412 // computed fields as comparing them with their original values might not be
|
Chris@0
|
1413 // possible or be meaningless.
|
Chris@0
|
1414 /** @var \Drupal\Core\Entity\ContentEntityBase $translation */
|
Chris@0
|
1415 $translation = $original->getTranslation($this->activeLangcode);
|
Chris@0
|
1416
|
Chris@0
|
1417 // The list of fields to skip from the comparision.
|
Chris@0
|
1418 $skip_fields = $this->getFieldsToSkipFromTranslationChangesCheck();
|
Chris@0
|
1419
|
Chris@14
|
1420 // We also check untranslatable fields, so that a change to those will mark
|
Chris@14
|
1421 // all translations as affected, unless they are configured to only affect
|
Chris@14
|
1422 // the default translation.
|
Chris@14
|
1423 $skip_untranslatable_fields = !$this->isDefaultTranslation() && $this->isDefaultTranslationAffectedOnly();
|
Chris@14
|
1424
|
Chris@0
|
1425 foreach ($this->getFieldDefinitions() as $field_name => $definition) {
|
Chris@0
|
1426 // @todo Avoid special-casing the following fields. See
|
Chris@0
|
1427 // https://www.drupal.org/node/2329253.
|
Chris@14
|
1428 if (in_array($field_name, $skip_fields, TRUE) || ($skip_untranslatable_fields && !$definition->isTranslatable())) {
|
Chris@0
|
1429 continue;
|
Chris@0
|
1430 }
|
Chris@0
|
1431 $field = $this->get($field_name);
|
Chris@0
|
1432 // When saving entities in the user interface, the changed timestamp is
|
Chris@0
|
1433 // automatically incremented by ContentEntityForm::submitForm() even if
|
Chris@0
|
1434 // nothing was actually changed. Thus, the changed time needs to be
|
Chris@0
|
1435 // ignored when determining whether there are any actual changes in the
|
Chris@0
|
1436 // entity.
|
Chris@0
|
1437 if (!($field instanceof ChangedFieldItemList) && !$definition->isComputed()) {
|
Chris@0
|
1438 $items = $field->filterEmptyItems();
|
Chris@0
|
1439 $original_items = $translation->get($field_name)->filterEmptyItems();
|
Chris@0
|
1440 if (!$items->equals($original_items)) {
|
Chris@0
|
1441 return TRUE;
|
Chris@0
|
1442 }
|
Chris@0
|
1443 }
|
Chris@0
|
1444 }
|
Chris@0
|
1445
|
Chris@0
|
1446 return FALSE;
|
Chris@0
|
1447 }
|
Chris@0
|
1448
|
Chris@14
|
1449 /**
|
Chris@14
|
1450 * {@inheritdoc}
|
Chris@14
|
1451 */
|
Chris@14
|
1452 public function isDefaultTranslationAffectedOnly() {
|
Chris@14
|
1453 $bundle_name = $this->bundle();
|
Chris@14
|
1454 $bundle_info = \Drupal::service('entity_type.bundle.info')
|
Chris@14
|
1455 ->getBundleInfo($this->getEntityTypeId());
|
Chris@14
|
1456 return !empty($bundle_info[$bundle_name]['untranslatable_fields.default_translation_affected']);
|
Chris@14
|
1457 }
|
Chris@14
|
1458
|
Chris@0
|
1459 }
|