You are here

public function AddressItem::initializeLangcode in Address 8

Initializes and returns the langcode property for the current field.

Some countries use separate address formats for the local language VS other languages. For example, China uses major-to-minor ordering when the address is entered in Chinese, and minor-to-major when the address is entered in other languages. This means that the address must remember which language it was entered in, to ensure consistent formatting later on.

  • For translatable entities this information comes from the field langcode.
  • Non-translatable entities have no way to provide this information, since the field langcode never changes. In this case the field must store the interface language at the time of address creation.
  • It is also possible to override the used language via field settings, in case the language is always known (e.g. a field storing the "english address" on a chinese article).

The langcode property is interpreted by getLocale(), and in case it's NULL, the field langcode is returned instead (indicating a non-multilingual site or a translatable parent entity).

Return value

string|null The langcode, or NULL if the field langcode should be used instead.

File

src/Plugin/Field/FieldType/AddressItem.php, line 291

Class

AddressItem
Plugin implementation of the 'address' field type.

Namespace

Drupal\address\Plugin\Field\FieldType

Code

public function initializeLangcode() {
  $this->langcode = NULL;
  $language_manager = \Drupal::languageManager();
  if (!$language_manager
    ->isMultilingual()) {
    return;
  }
  if ($override = $this
    ->getSetting('langcode_override')) {
    $this->langcode = $override;
  }
  elseif (!$this
    ->getEntity()
    ->isTranslatable()) {

    // The getCurrentLanguage fallback is a workaround for core bug #2684873.
    $language = $language_manager
      ->getConfigOverrideLanguage() ?: $language_manager
      ->getCurrentLanguage();
    $this->langcode = $language
      ->getId();
  }
  return $this->langcode;
}