3 namespace Drupal\content_translation;
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\user\UserInterface;
9 * Base class for content translation metadata wrappers.
11 class ContentTranslationMetadataWrapper implements ContentTranslationMetadataWrapperInterface {
14 * The wrapped entity translation.
16 * @var \Drupal\Core\Entity\EntityInterface|\Drupal\Core\Entity\FieldableEntityInterface|\Drupal\Core\TypedData\TranslatableInterface
18 protected $translation;
21 * The content translation handler.
23 * @var \Drupal\content_translation\ContentTranslationHandlerInterface
28 * Initializes an instance of the content translation metadata handler.
30 * @param EntityInterface $translation
31 * The entity translation to be wrapped.
32 * @param ContentTranslationHandlerInterface $handler
33 * The content translation handler.
35 public function __construct(EntityInterface $translation, ContentTranslationHandlerInterface $handler) {
36 $this->translation = $translation;
37 $this->handler = $handler;
43 public function getSource() {
44 return $this->translation->get('content_translation_source')->value;
50 public function setSource($source) {
51 $this->translation->set('content_translation_source', $source);
58 public function isOutdated() {
59 return (bool) $this->translation->get('content_translation_outdated')->value;
65 public function setOutdated($outdated) {
66 $this->translation->set('content_translation_outdated', $outdated);
73 public function getAuthor() {
74 return $this->translation->hasField('content_translation_uid') ? $this->translation->get('content_translation_uid')->entity : $this->translation->getOwner();
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());
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;
97 public function setPublished($published) {
98 $field_name = $this->translation->hasField('content_translation_status') ? 'content_translation_status' : 'status';
99 $this->setFieldOnlyIfTranslatable($field_name, $published);
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;
114 public function setCreatedTime($timestamp) {
115 $field_name = $this->translation->hasField('content_translation_created') ? 'content_translation_created' : 'created';
116 $this->setFieldOnlyIfTranslatable($field_name, $timestamp);
123 public function getChangedTime() {
124 return $this->translation->hasField('content_translation_changed') ? $this->translation->get('content_translation_changed')->value : $this->translation->getChangedTime();
130 public function setChangedTime($timestamp) {
131 $field_name = $this->translation->hasField('content_translation_changed') ? 'content_translation_changed' : 'changed';
132 $this->setFieldOnlyIfTranslatable($field_name, $timestamp);
137 * Updates a field value, only if the field is translatable.
139 * @param string $field_name
140 * The name of the field.
141 * @param mixed $value
142 * The field value to be set.
144 protected function setFieldOnlyIfTranslatable($field_name, $value) {
145 if ($this->translation->getFieldDefinition($field_name)->isTranslatable()) {
146 $this->translation->set($field_name, $value);