language.module
Same filename in other branches
File
-
core/
modules/ language/ language.module
View source
<?php
/**
* @file
*/
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\language\Entity\ContentLanguageSettings;
/**
* Processes a language select list form element.
*
* @param array $element
* The form element to process.
*
* @return array
* The processed form element.
*/
function language_process_language_select($element) {
// Don't set the options if another module (translation for example) already
// set the options.
if (!isset($element['#options'])) {
$element['#options'] = [];
foreach (\Drupal::languageManager()->getLanguages($element['#languages']) as $langcode => $language) {
$element['#options'][$langcode] = $language->isLocked() ? t('- @name -', [
'@name' => $language->getName(),
]) : $language->getName();
}
}
return $element;
}
/**
* Submit handler for the forms that have a language_configuration element.
*/
function language_configuration_element_submit(&$form, FormStateInterface $form_state) {
// Iterate through all the language_configuration elements and save their
// values.
// In case we are editing a bundle, we must check the new bundle name,
// because e.g. hook_ENTITY_update fired before.
if ($language = $form_state->get('language')) {
foreach ($language as $element_name => $values) {
$entity_type_id = $values['entity_type'];
$bundle = $values['bundle'];
$form_object = $form_state->getFormObject();
if ($form_object instanceof EntityFormInterface) {
/** @var \Drupal\Core\Entity\EntityFormInterface $form_object */
$entity = $form_object->getEntity();
if ($entity->getEntityType()
->getBundleOf()) {
$bundle = $entity->id();
$language[$element_name]['bundle'] = $bundle;
}
}
$config = ContentLanguageSettings::loadByEntityTypeBundle($entity_type_id, $bundle);
$config->setDefaultLangcode($form_state->getValue([
$element_name,
'langcode',
]));
$config->setLanguageAlterable($form_state->getValue([
$element_name,
'language_alterable',
]));
$config->save();
// Set the form_state language with the updated bundle.
$form_state->set('language', $language);
}
}
}
/**
* Returns the default language code assigned to an entity type and a bundle.
*
* @param string $entity_type
* The entity type.
* @param string $bundle
* The bundle name.
*
* @return string
* The language 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();
}
/**
* Update the list of prefixes from the installed languages.
*/
function language_negotiation_url_prefixes_update() {
$config = \Drupal::configFactory()->getEditable('language.negotiation');
$prefixes = $config->get('url.prefixes');
foreach (\Drupal::languageManager()->getLanguages() as $language) {
// The prefix for this language should be updated if it's not assigned yet
// or the prefix is set to the empty string.
if (empty($prefixes[$language->getId()])) {
// For the default language, set the prefix to the empty string,
// otherwise use the langcode.
$prefixes[$language->getId()] = $language->isDefault() ? '' : $language->getId();
}
// Otherwise we keep the configured prefix.
}
$config->set('url.prefixes', $prefixes)
->save(TRUE);
}
/**
* Implements hook_preprocess_HOOK() for block templates.
*/
function language_preprocess_block(&$variables) {
if ($variables['configuration']['provider'] == 'language') {
$variables['attributes']['role'] = 'navigation';
}
}
/**
* Returns language mappings between browser and Drupal language codes.
*
* @return array
* An array containing browser language codes as keys with corresponding
* Drupal language codes as values.
*/
function language_get_browser_drupal_langcode_mappings() {
$config = \Drupal::config('language.mappings');
if ($config->isNew()) {
return [];
}
return $config->get('map');
}
Functions
Title | Deprecated | Summary |
---|---|---|
language_configuration_element_submit | Submit handler for the forms that have a language_configuration element. | |
language_get_browser_drupal_langcode_mappings | Returns language mappings between browser and Drupal language codes. | |
language_get_default_langcode | Returns the default language code assigned to an entity type and a bundle. | |
language_negotiation_url_prefixes_update | Update the list of prefixes from the installed languages. | |
language_preprocess_block | Implements hook_preprocess_HOOK() for block templates. | |
language_process_language_select | Processes a language select list form element. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.