language_get_default_langcode

language_get_default_langcode($entity_type, $bundle)

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.

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();
}
doc_Drupal
2016-10-29 09:23:02
Comments
Leave a Comment

Please login to continue.