You are here

function language_get_default_langcode in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/language/language.module \language_get_default_langcode()
  2. 9 core/modules/language/language.module \language_get_default_langcode()

Returns the default language code assigned to an entity type and a bundle.

Parameters

string $entity_type: The entity type.

string $bundle: The bundle name.

Return value

string The language code.

4 calls to language_get_default_langcode()
DefaultLanguageItem::getDefaultLangcode in core/modules/language/src/DefaultLanguageItem.php
Provides default language code of given entity.
LanguageConfigurationElementTest::buildForm in core/modules/language/tests/language_elements_test/src/Form/LanguageConfigurationElementTest.php
LanguageConfigurationElementTest::testDefaultLangcode in core/modules/language/tests/src/Functional/LanguageConfigurationElementTest.php
Tests that the language_get_default_langcode() returns the correct values.
NodeFieldMultilingualTest::testMultilingualNodeForm in core/modules/node/tests/src/Functional/NodeFieldMultilingualTest.php
Tests whether field languages are correctly set through the node form.

File

core/modules/language/language.module, line 236
Add language handling functionality to Drupal.

Code

function language_get_default_langcode($entity_type, $bundle) {
  $configuration = ContentLanguageSettings::loadByEntityTypeBundle($entity_type, $bundle);
  $default_value = NULL;
  $language_interface = \Drupal::languageManager()
    ->getCurrentLanguage();
  switch ($configuration
    ->getDefaultLangcode()) {
    case LanguageInterface::LANGCODE_SITE_DEFAULT:
      $default_value = \Drupal::languageManager()
        ->getDefaultLanguage()
        ->getId();
      break;
    case 'current_interface':
      $default_value = $language_interface
        ->getId();
      break;
    case 'authors_default':
      $user = \Drupal::currentUser();
      $language_code = $user
        ->getPreferredLangcode();
      if (!empty($language_code)) {
        $default_value = $language_code;
      }
      else {
        $default_value = $language_interface
          ->getId();
      }
      break;
  }
  if ($default_value) {
    return $default_value;
  }

  // If we still do not have a default value, just return the value stored in
  // the configuration; it has to be an actual language code.
  return $configuration
    ->getDefaultLangcode();
}