function ContentEntityBase::getTranslation

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Entity/ContentEntityBase.php \Drupal\Core\Entity\ContentEntityBase::getTranslation()
  2. 10 core/lib/Drupal/Core/Entity/ContentEntityBase.php \Drupal\Core\Entity\ContentEntityBase::getTranslation()
  3. 11.x core/lib/Drupal/Core/Entity/ContentEntityBase.php \Drupal\Core\Entity\ContentEntityBase::getTranslation()
5 calls to ContentEntityBase::getTranslation()
ContentEntityBase::getTranslatedField in core/lib/Drupal/Core/Entity/ContentEntityBase.php
Gets a translated field.
ContentEntityBase::getUntranslated in core/lib/Drupal/Core/Entity/ContentEntityBase.php
Media::postSave in core/modules/media/src/Entity/Media.php
Acts on a saved entity before the insert or update hook is invoked.
Media::prepareSave in core/modules/media/src/Entity/Media.php
Sets the media entity's field values from the source's metadata.
Node::preSave in core/modules/node/src/Entity/Node.php
Acts on an entity before the presave hook is invoked.

File

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

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 (!isset($this->translations[$this->activeLangcode]['entity'])) {
        $this->translations[$this->activeLangcode]['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'];
    }
    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;
}

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