ContentEntityBase::addTranslation

public ContentEntityBase::addTranslation($langcode, array $values = array())

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 818

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);
}
doc_Drupal
2016-10-29 08:56:53
Comments
Leave a Comment

Please login to continue.