function ContentEntityBase::getTranslatedField

Same name in other branches
  1. 9 core/lib/Drupal/Core/Entity/ContentEntityBase.php \Drupal\Core\Entity\ContentEntityBase::getTranslatedField()
  2. 8.9.x core/lib/Drupal/Core/Entity/ContentEntityBase.php \Drupal\Core\Entity\ContentEntityBase::getTranslatedField()
  3. 11.x core/lib/Drupal/Core/Entity/ContentEntityBase.php \Drupal\Core\Entity\ContentEntityBase::getTranslatedField()

Gets a translated field.

Return value

\Drupal\Core\Field\FieldItemListInterface

2 calls to ContentEntityBase::getTranslatedField()
ContentEntityBase::get in core/lib/Drupal/Core/Entity/ContentEntityBase.php
ContentEntityBase::__get in core/lib/Drupal/Core/Entity/ContentEntityBase.php
Implements the magic method for getting object properties.

File

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

Class

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

Namespace

Drupal\Core\Entity

Code

protected function getTranslatedField($name, $langcode) {
    if ($this->translations[$this->activeLangcode]['status'] == static::TRANSLATION_REMOVED) {
        throw new \InvalidArgumentException("The entity object refers to a removed translation ({$this->activeLangcode}) and cannot be manipulated.");
    }
    // Populate $this->fields to speed-up further look-ups and to keep track of
    // fields objects, possibly holding changes to field values.
    if (!isset($this->fields[$name][$langcode])) {
        $definition = $this->getFieldDefinition($name);
        if (!$definition) {
            throw new \InvalidArgumentException("Field {$name} is unknown.");
        }
        // Non-translatable fields are always stored with
        // LanguageInterface::LANGCODE_DEFAULT as key.
        $default = $langcode == LanguageInterface::LANGCODE_DEFAULT;
        if (!$default && !$definition->isTranslatable()) {
            if (!isset($this->fields[$name][LanguageInterface::LANGCODE_DEFAULT])) {
                $this->fields[$name][LanguageInterface::LANGCODE_DEFAULT] = $this->getTranslatedField($name, LanguageInterface::LANGCODE_DEFAULT);
            }
            $this->fields[$name][$langcode] =& $this->fields[$name][LanguageInterface::LANGCODE_DEFAULT];
        }
        else {
            $value = NULL;
            if (isset($this->values[$name][$langcode])) {
                $value = $this->values[$name][$langcode];
            }
            $field = \Drupal::service('plugin.manager.field.field_type')->createFieldItemList($this->getTranslation($langcode), $name, $value);
            if ($default) {
                // $this->defaultLangcode might not be set if we are initializing the
                // default language code cache, in which case there is no valid
                // langcode to assign.
                $field_langcode = $this->defaultLangcode ?? LanguageInterface::LANGCODE_NOT_SPECIFIED;
            }
            else {
                $field_langcode = $langcode;
            }
            $field->setLangcode($field_langcode);
            $this->fields[$name][$langcode] = $field;
        }
    }
    return $this->fields[$name][$langcode];
}

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