function ContentEntityStorageBase::createTranslation

Same name in other branches
  1. 9 core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php \Drupal\Core\Entity\ContentEntityStorageBase::createTranslation()
  2. 8.9.x core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php \Drupal\Core\Entity\ContentEntityStorageBase::createTranslation()
  3. 10 core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php \Drupal\Core\Entity\ContentEntityStorageBase::createTranslation()

Overrides TranslatableStorageInterface::createTranslation

File

core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php, line 352

Class

ContentEntityStorageBase
Base class for content entity storage handlers.

Namespace

Drupal\Core\Entity

Code

public function createTranslation(ContentEntityInterface $entity, $langcode, array $values = []) {
    $translation = $entity->getTranslation($langcode);
    $definitions = array_filter($translation->getFieldDefinitions(), function (FieldDefinitionInterface $definition) {
        return $definition->isTranslatable();
    });
    $field_names = array_map(function (FieldDefinitionInterface $definition) {
        return $definition->getName();
    }, $definitions);
    $values[$this->langcodeKey] = $langcode;
    $values[$this->getEntityType()
        ->getKey('default_langcode')] = FALSE;
    $this->initFieldValues($translation, $values, $field_names);
    $this->invokeHook('translation_create', $translation);
    return $translation;
}

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