public function ContentEntityBase::getTranslation

public ContentEntityBase::getTranslation($langcode)

Gets a translation of the data.

The returned translation has to be of the same type than this typed data object.

Parameters

$langcode: The language code of the translation to get or LanguageInterface::LANGCODE_DEFAULT to get the data in default language.

Return value

$this A typed data object for the translated data.

Throws

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

Overrides TranslatableInterface::getTranslation

File

core/lib/Drupal/Core/Entity/ContentEntityBase.php, line 719

Class

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

Namespace

Drupal\Core\Entity

Code

public function getTranslation($langcode) {
  // Ensure we always use the default language code when dealing with the
  // original entity language.
  if ($langcode != LanguageInterface::LANGCODE_DEFAULT && $langcode == $this->defaultLangcode) {
    $langcode = LanguageInterface::LANGCODE_DEFAULT;
  }

  // Populate entity translation object cache so it will be available for all
  // translation objects.
  if ($langcode == $this->activeLangcode) {
    $this->translations[$langcode]['entity'] = $this;
  }

  // If we already have a translation object for the specified language we can
  // just return it.
  if (isset($this->translations[$langcode]['entity'])) {
    $translation = $this->translations[$langcode]['entity'];
  }
  // Otherwise if an existing translation language was specified we need to
  // instantiate the related translation.
  elseif (isset($this->translations[$langcode])) {
    $translation = $this->initializeTranslation($langcode);
    $this->translations[$langcode]['entity'] = $translation;
  }

  if (empty($translation)) {
    throw new \InvalidArgumentException("Invalid translation language ($langcode) specified.");
  }

  return $translation;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!ContentEntityBase.php/function/ContentEntityBase::getTranslation/8.1.x