function ContentEntityBase::setDefaultLangcode

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

Populates the local cache for the default language code.

2 calls to ContentEntityBase::setDefaultLangcode()
ContentEntityBase::onChange in core/lib/Drupal/Core/Entity/ContentEntityBase.php
ContentEntityBase::__construct in core/lib/Drupal/Core/Entity/ContentEntityBase.php
Constructs an Entity object.

File

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

Class

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

Namespace

Drupal\Core\Entity

Code

protected function setDefaultLangcode() {
    // Get the language code if the property exists.
    // Try to read the value directly from the list of entity keys which got
    // initialized in __construct(). This avoids creating a field item object.
    if (isset($this->translatableEntityKeys['langcode'][$this->activeLangcode])) {
        $this->defaultLangcode = $this->translatableEntityKeys['langcode'][$this->activeLangcode];
    }
    elseif ($this->hasField($this->langcodeKey) && ($item = $this->get($this->langcodeKey)) && isset($item->language)) {
        $this->defaultLangcode = $item->language
            ->getId();
        $this->translatableEntityKeys['langcode'][$this->activeLangcode] = $this->defaultLangcode;
    }
    if (empty($this->defaultLangcode)) {
        // Make sure we return a proper language object, if the entity has a
        // langcode field, default to the site's default language.
        if ($this->hasField($this->langcodeKey)) {
            $this->defaultLangcode = $this->languageManager()
                ->getDefaultLanguage()
                ->getId();
        }
        else {
            $this->defaultLangcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;
        }
    }
    // This needs to be initialized manually as it is skipped when instantiating
    // the language field object to avoid infinite recursion.
    if (!empty($this->fields[$this->langcodeKey])) {
        $this->fields[$this->langcodeKey][LanguageInterface::LANGCODE_DEFAULT]
            ->setLangcode($this->defaultLangcode);
    }
}

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