protected function ContentEntityBase::setDefaultLangcode

protected ContentEntityBase::setDefaultLangcode()

Populates the local cache for the default language code.

File

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

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);
  }
}

© 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::setDefaultLangcode/8.1.x