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