ContentTranslationMetadataWrapper.php 3.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
<?php

/**
 * @file
 * Contains \Drupal\content_translation\ContentTranslationMetadata.
 */

namespace Drupal\content_translation;

use Drupal\Core\Entity\EntityInterface;
use Drupal\user\UserInterface;

/**
 * Base class for content translation metadata wrappers.
 */
class ContentTranslationMetadataWrapper implements ContentTranslationMetadataWrapperInterface {

  /**
   * The wrapped entity translation.
   *
   * @var \Drupal\Core\Entity\EntityInterface|\Drupal\Core\Entity\FieldableEntityInterface|\Drupal\Core\TypedData\TranslatableInterface
   */
  protected $translation;

  /**
   * The content translation handler.
   *
   * @var \Drupal\content_translation\ContentTranslationHandlerInterface
   */
  protected $handler;

  /**
   * Initializes an instance of the content translation metadata handler.
   *
   * @param EntityInterface $translation
   *   The entity translation to be wrapped.
   * @param ContentTranslationHandlerInterface $handler
   *   The content translation handler.
   */
  public function __construct(EntityInterface $translation, ContentTranslationHandlerInterface $handler) {
    $this->translation = $translation;
    $this->handler = $handler;
  }

  /**
   * {@inheritdoc}
   */
  public function getSource() {
    return $this->translation->get('content_translation_source')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setSource($source) {
    $this->translation->set('content_translation_source', $source);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function isOutdated() {
    return (bool) $this->translation->get('content_translation_outdated')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setOutdated($outdated) {
    $this->translation->set('content_translation_outdated', $outdated);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getAuthor() {
    return $this->translation->hasField('content_translation_uid') ? $this->translation->get('content_translation_uid')->entity : $this->translation->getOwner();
  }

  /**
   * {@inheritdoc}
   */
  public function setAuthor(UserInterface $account) {
    if ($this->translation->hasField('content_translation_uid')) {
      $this->translation->set('content_translation_uid', $account->id());
    }
    else {
      $this->translation->setOwner($account);
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function isPublished() {
    $field_name = $this->translation->hasField('content_translation_status') ? 'content_translation_status' : 'status';
    return (bool) $this->translation->get($field_name)->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setPublished($published) {
    $field_name = $this->translation->hasField('content_translation_status') ? 'content_translation_status' : 'status';
    $this->translation->set($field_name, $published);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getCreatedTime() {
    $field_name = $this->translation->hasField('content_translation_created') ? 'content_translation_created' : 'created';
    return $this->translation->get($field_name)->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setCreatedTime($timestamp) {
    $field_name = $this->translation->hasField('content_translation_created') ? 'content_translation_created' : 'created';
    $this->translation->set($field_name, $timestamp);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getChangedTime() {
    return $this->translation->hasField('content_translation_changed') ? $this->translation->get('content_translation_changed')->value : $this->translation->getChangedTime();
  }

  /**
   * {@inheritdoc}
   */
  public function setChangedTime($timestamp) {
    $field_name = $this->translation->hasField('content_translation_changed') ? 'content_translation_changed' : 'changed';
    $this->translation->set($field_name, $timestamp);
    return $this;
  }

}