function ContentTranslationController::prepareTranslation

Same name in other branches
  1. 9 core/modules/content_translation/src/Controller/ContentTranslationController.php \Drupal\content_translation\Controller\ContentTranslationController::prepareTranslation()
  2. 8.9.x 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 77

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\ContentEntityStorageInterface $storage */
    $storage = $this->entityTypeManager()
        ->getStorage($entity->getEntityTypeId());
    // Once translations from the default revision are added, there may be
    // additional draft translations that don't exist in the default revision.
    // Add those translations too so that they aren't deleted when the new
    // translation is saved.
    
    /** @var \Drupal\Core\Entity\ContentEntityInterface $default_revision */
    $default_revision = $storage->load($entity->id());
    // Check the entity isn't missing any translations.
    $languages = $this->languageManager()
        ->getLanguages();
    foreach ($languages as $language) {
        $langcode = $language->getId();
        if ($entity->hasTranslation($langcode) || $target->getId() === $langcode) {
            continue;
        }
        $latest_revision_id = $storage->getLatestTranslationAffectedRevisionId($entity->id(), $langcode);
        if ($latest_revision_id) {
            if ($default_revision->hasTranslation($langcode)) {
                $existing_translation = $default_revision->getTranslation($langcode);
                $existing_translation->setNewRevision(FALSE);
                $existing_translation->isDefaultRevision(FALSE);
                $existing_translation->setRevisionTranslationAffected(FALSE);
                $entity->addTranslation($langcode, $existing_translation->toArray());
            }
        }
    }
    
    /** @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($this->time
        ->getRequestTime());
    $metadata->setSource($source_langcode);
}

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