You are here

class ContentTranslationMetadataWrapper in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/content_translation/src/ContentTranslationMetadataWrapper.php \Drupal\content_translation\ContentTranslationMetadataWrapper

Base class for content translation metadata wrappers.

Hierarchy

Expanded class hierarchy of ContentTranslationMetadataWrapper

File

core/modules/content_translation/src/ContentTranslationMetadataWrapper.php, line 11

Namespace

Drupal\content_translation
View source
class ContentTranslationMetadataWrapper implements ContentTranslationMetadataWrapperInterface {

  /**
   * The wrapped entity translation.
   *
   * @var \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 \Drupal\Core\Entity\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) {
    $field_name = $this->translation
      ->hasField('content_translation_uid') ? 'content_translation_uid' : 'uid';
    $this
      ->setFieldOnlyIfTranslatable($field_name, $account
      ->id());
    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
      ->setFieldOnlyIfTranslatable($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
      ->setFieldOnlyIfTranslatable($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
      ->setFieldOnlyIfTranslatable($field_name, $timestamp);
    return $this;
  }

  /**
   * Updates a field value, only if the field is translatable.
   *
   * @param string $field_name
   *   The name of the field.
   * @param mixed $value
   *   The field value to be set.
   */
  protected function setFieldOnlyIfTranslatable($field_name, $value) {
    if ($this->translation
      ->getFieldDefinition($field_name)
      ->isTranslatable()) {
      $this->translation
        ->set($field_name, $value);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentTranslationMetadataWrapper::$handler protected property The content translation handler.
ContentTranslationMetadataWrapper::$translation protected property The wrapped entity translation.
ContentTranslationMetadataWrapper::getAuthor public function Returns the translation author. Overrides ContentTranslationMetadataWrapperInterface::getAuthor
ContentTranslationMetadataWrapper::getChangedTime public function Returns the timestamp of the last entity change from current translation. Overrides ContentTranslationMetadataWrapperInterface::getChangedTime
ContentTranslationMetadataWrapper::getCreatedTime public function Returns the translation creation timestamp. Overrides ContentTranslationMetadataWrapperInterface::getCreatedTime
ContentTranslationMetadataWrapper::getSource public function Retrieves the source language for this translation. Overrides ContentTranslationMetadataWrapperInterface::getSource
ContentTranslationMetadataWrapper::isOutdated public function Returns the translation outdated status. Overrides ContentTranslationMetadataWrapperInterface::isOutdated
ContentTranslationMetadataWrapper::isPublished public function Returns the translation published status. Overrides ContentTranslationMetadataWrapperInterface::isPublished
ContentTranslationMetadataWrapper::setAuthor public function Sets the translation author. Overrides ContentTranslationMetadataWrapperInterface::setAuthor
ContentTranslationMetadataWrapper::setChangedTime public function Sets the translation modification timestamp. Overrides ContentTranslationMetadataWrapperInterface::setChangedTime
ContentTranslationMetadataWrapper::setCreatedTime public function Sets the translation creation timestamp. Overrides ContentTranslationMetadataWrapperInterface::setCreatedTime
ContentTranslationMetadataWrapper::setFieldOnlyIfTranslatable protected function Updates a field value, only if the field is translatable.
ContentTranslationMetadataWrapper::setOutdated public function Sets the translation outdated status. Overrides ContentTranslationMetadataWrapperInterface::setOutdated
ContentTranslationMetadataWrapper::setPublished public function Sets the translation published status. Overrides ContentTranslationMetadataWrapperInterface::setPublished
ContentTranslationMetadataWrapper::setSource public function Sets the source language for this translation. Overrides ContentTranslationMetadataWrapperInterface::setSource
ContentTranslationMetadataWrapper::__construct public function Initializes an instance of the content translation metadata handler.