comparison core/modules/content_translation/src/ContentTranslationMetadataWrapper.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\content_translation;
4
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\user\UserInterface;
7
8 /**
9 * Base class for content translation metadata wrappers.
10 */
11 class ContentTranslationMetadataWrapper implements ContentTranslationMetadataWrapperInterface {
12
13 /**
14 * The wrapped entity translation.
15 *
16 * @var \Drupal\Core\Entity\EntityInterface|\Drupal\Core\Entity\FieldableEntityInterface|\Drupal\Core\TypedData\TranslatableInterface
17 */
18 protected $translation;
19
20 /**
21 * The content translation handler.
22 *
23 * @var \Drupal\content_translation\ContentTranslationHandlerInterface
24 */
25 protected $handler;
26
27 /**
28 * Initializes an instance of the content translation metadata handler.
29 *
30 * @param EntityInterface $translation
31 * The entity translation to be wrapped.
32 * @param ContentTranslationHandlerInterface $handler
33 * The content translation handler.
34 */
35 public function __construct(EntityInterface $translation, ContentTranslationHandlerInterface $handler) {
36 $this->translation = $translation;
37 $this->handler = $handler;
38 }
39
40 /**
41 * {@inheritdoc}
42 */
43 public function getSource() {
44 return $this->translation->get('content_translation_source')->value;
45 }
46
47 /**
48 * {@inheritdoc}
49 */
50 public function setSource($source) {
51 $this->translation->set('content_translation_source', $source);
52 return $this;
53 }
54
55 /**
56 * {@inheritdoc}
57 */
58 public function isOutdated() {
59 return (bool) $this->translation->get('content_translation_outdated')->value;
60 }
61
62 /**
63 * {@inheritdoc}
64 */
65 public function setOutdated($outdated) {
66 $this->translation->set('content_translation_outdated', $outdated);
67 return $this;
68 }
69
70 /**
71 * {@inheritdoc}
72 */
73 public function getAuthor() {
74 return $this->translation->hasField('content_translation_uid') ? $this->translation->get('content_translation_uid')->entity : $this->translation->getOwner();
75 }
76
77 /**
78 * {@inheritdoc}
79 */
80 public function setAuthor(UserInterface $account) {
81 $field_name = $this->translation->hasField('content_translation_uid') ? 'content_translation_uid' : 'uid';
82 $this->setFieldOnlyIfTranslatable($field_name, $account->id());
83 return $this;
84 }
85
86 /**
87 * {@inheritdoc}
88 */
89 public function isPublished() {
90 $field_name = $this->translation->hasField('content_translation_status') ? 'content_translation_status' : 'status';
91 return (bool) $this->translation->get($field_name)->value;
92 }
93
94 /**
95 * {@inheritdoc}
96 */
97 public function setPublished($published) {
98 $field_name = $this->translation->hasField('content_translation_status') ? 'content_translation_status' : 'status';
99 $this->setFieldOnlyIfTranslatable($field_name, $published);
100 return $this;
101 }
102
103 /**
104 * {@inheritdoc}
105 */
106 public function getCreatedTime() {
107 $field_name = $this->translation->hasField('content_translation_created') ? 'content_translation_created' : 'created';
108 return $this->translation->get($field_name)->value;
109 }
110
111 /**
112 * {@inheritdoc}
113 */
114 public function setCreatedTime($timestamp) {
115 $field_name = $this->translation->hasField('content_translation_created') ? 'content_translation_created' : 'created';
116 $this->setFieldOnlyIfTranslatable($field_name, $timestamp);
117 return $this;
118 }
119
120 /**
121 * {@inheritdoc}
122 */
123 public function getChangedTime() {
124 return $this->translation->hasField('content_translation_changed') ? $this->translation->get('content_translation_changed')->value : $this->translation->getChangedTime();
125 }
126
127 /**
128 * {@inheritdoc}
129 */
130 public function setChangedTime($timestamp) {
131 $field_name = $this->translation->hasField('content_translation_changed') ? 'content_translation_changed' : 'changed';
132 $this->setFieldOnlyIfTranslatable($field_name, $timestamp);
133 return $this;
134 }
135
136 /**
137 * Updates a field value, only if the field is translatable.
138 *
139 * @param string $field_name
140 * The name of the field.
141 * @param mixed $value
142 * The field value to be set.
143 */
144 protected function setFieldOnlyIfTranslatable($field_name, $value) {
145 if ($this->translation->getFieldDefinition($field_name)->isTranslatable()) {
146 $this->translation->set($field_name, $value);
147 }
148 }
149
150 }