function ContentTranslationController::prepareTranslation

Same name in other branches
  1. 8.9.x core/modules/content_translation/src/Controller/ContentTranslationController.php \Drupal\content_translation\Controller\ContentTranslationController::prepareTranslation()
  2. 10 core/modules/content_translation/src/Controller/ContentTranslationController.php \Drupal\content_translation\Controller\ContentTranslationController::prepareTranslation()
  3. 11.x core/modules/content_translation/src/Controller/ContentTranslationController.php \Drupal\content_translation\Controller\ContentTranslationController::prepareTranslation()

Populates target values with the source values.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity being translated.

\Drupal\Core\Language\LanguageInterface $source: The language to be used as source.

\Drupal\Core\Language\LanguageInterface $target: The language to be used as target.

1 call to ContentTranslationController::prepareTranslation()
ContentTranslationController::add in core/modules/content_translation/src/Controller/ContentTranslationController.php
Builds an add translation page.

File

core/modules/content_translation/src/Controller/ContentTranslationController.php, line 69

Class

ContentTranslationController
Base class for entity translation controllers.

Namespace

Drupal\content_translation\Controller

Code

public function prepareTranslation(ContentEntityInterface $entity, LanguageInterface $source, LanguageInterface $target) {
    $source_langcode = $source->getId();
    
    /** @var \Drupal\Core\Entity\ContentEntityInterface $source_translation */
    $source_translation = $entity->getTranslation($source_langcode);
    $target_translation = $entity->addTranslation($target->getId(), $source_translation->toArray());
    // Make sure we do not inherit the affected status from the source values.
    if ($entity->getEntityType()
        ->isRevisionable()) {
        $target_translation->setRevisionTranslationAffected(NULL);
    }
    
    /** @var \Drupal\user\UserInterface $user */
    $user = $this->entityTypeManager()
        ->getStorage('user')
        ->load($this->currentUser()
        ->id());
    $metadata = $this->manager
        ->getTranslationMetadata($target_translation);
    // Update the translation author to current user, as well the translation
    // creation time.
    $metadata->setAuthor($user);
    $metadata->setCreatedTime(REQUEST_TIME);
    $metadata->setSource($source_langcode);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.