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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.