You are here

public function ContentEntityBase::addTranslation in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Entity/ContentEntityBase.php \Drupal\Core\Entity\ContentEntityBase::addTranslation()

Adds a new translation to the translatable object.

Parameters

string $langcode: The language code identifying the translation.

array $values: (optional) An array of initial values to be assigned to the translatable fields. Defaults to none.

Return value

$this

Throws

\InvalidArgumentException If an invalid or existing translation language is specified.

Overrides TranslatableInterface::addTranslation

File

core/lib/Drupal/Core/Entity/ContentEntityBase.php, line 821
Contains \Drupal\Core\Entity\ContentEntityBase.

Class

ContentEntityBase
Implements Entity Field API specific enhancements to the Entity class.

Namespace

Drupal\Core\Entity

Code

public function addTranslation($langcode, array $values = array()) {

  // Make sure we do not attempt to create a translation if an invalid
  // language is specified or the entity cannot be translated.
  $this
    ->getLanguages();
  if (!isset($this->languages[$langcode]) || $this
    ->hasTranslation($langcode) || $this->languages[$langcode]
    ->isLocked()) {
    throw new \InvalidArgumentException("Invalid translation language ({$langcode}) specified.");
  }
  if ($this->languages[$this->defaultLangcode]
    ->isLocked()) {
    throw new \InvalidArgumentException("The entity cannot be translated since it is language neutral ({$this->defaultLangcode}).");
  }

  // Initialize the translation object.

  /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
  $storage = $this
    ->entityManager()
    ->getStorage($this
    ->getEntityTypeId());
  $this->translations[$langcode]['status'] = static::TRANSLATION_CREATED;
  return $storage
    ->createTranslation($this, $langcode, $values);
}