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);
  }
}
doc_Drupal
2016-10-29 08:57:00
Comments
Leave a Comment

Please login to continue.