Newer
Older
<?php
/**
* @file
* Add language handling functionality to Drupal.
*/
use Drupal\Core\PhpStorage\PhpStorageFactory;
use Drupal\Core\Language\Language;
use Drupal\language\ConfigurableLanguageManager;
use Drupal\language\ConfigurableLanguageManagerInterface;
use Drupal\language\Entity\Language as LanguageEntity;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUI;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrlFallback;
use Drupal\node\NodeTypeInterface;
/**
* Implements hook_help().
*/
function language_help($path, $arg) {
switch ($path) {
case 'admin/help#language':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Language module allows you to maintain a list of languages used on your Drupal site for providing language information for content and for interface translation (using the Locale module). For more information, see the online handbook entry for <a href="@language">Language module</a>.', array('@language' => 'http://drupal.org/documentation/modules/language')) . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Configuring the list of languages') . '</dt>';
$output .= '<dd>' . t('<a href="@configure-languages">Configure the list of languages</a> either using the built-in language list or providing any custom languages you wish.', array('@configure-languages' => url('admin/config/regional/language'))) . '</dd>';
Dries Buytaert
committed
$output .= '<dt>' . t('Configuring a multilingual site') . '</dt>';
$output .= '<dd>' . t("Language negotiation allows your site to automatically change language based on the domain or path used for each request. Users may (optionally) select their preferred language on their <em>My account</em> page, and your site can be configured to honor a web browser's preferred language settings. Site content can be translated using the <a href='@content-help'>Content Translation module</a>.", array('@content-help' => url('admin/help/translation'))) . '</dd>';
$output .= '</dl>';
return $output;
case 'admin/config/regional/language':
Dries Buytaert
committed
return '<p>' . t('With multiple languages enabled, registered users may select their preferred language and authors can assign a specific language to content. The selection of what language is used to display page elements is made depending on the detection menthod settings in the <a href="@detection">Detection and Selection</a> tab.', array('@detection' => url('admin/config/regional/language/detection'))) . '</p>';
case 'admin/config/regional/language/add':
return '<p>' . t('Add a language to be supported by your site. If your desired language is not available, pick <em>Custom language...</em> at the end and provide a language code and other details manually.') . '</p>';
Dries Buytaert
committed
case 'admin/config/regional/language/detection':
Dries Buytaert
committed
$output = '<p>' . t('Define how to decide which language is used to display page elements (primarily text provided by Drupal and modules, such as field labels and help text). This decision is made by evaluating a series of detection methods for languages; the first detection method that gets a result will determine which language is used for that type of text. Be aware that some language negotiation methods are unreliable under certain conditions, such as browser detection when page-caching is enabled and a user is not currently logged in. Define the order of evaluation of language detection methods on this page. Default language can be changed at the <a href="@region-settings">Regional settings</a> page.', array('@region-settings' => url('admin/config/regional/settings'))) . '</p>';
Dries Buytaert
committed
return $output;
case 'admin/config/regional/language/detection/session':
$output = '<p>' . t('Determine the language from a request/session parameter. Example: "http://example.com?language=de" sets language to German based on the use of "de" within the "language" parameter.') . '</p>';
return $output;
Angie Byron
committed
case 'admin/config/regional/language/detection/browser':
$output = '<p>' . t('Browsers use different language codes to refer to the same languages. You can add and edit mappings from browser language codes to the <a href="@configure-languages">languages used by Drupal</a>.', array('@configure-languages' => url('admin/config/regional/language'))) . '</p>';
return $output;
Dries Buytaert
committed
case 'admin/config/regional/language/detection/selected':
$output = '<p>' . t('Changing the selected language here (and leaving this option as the last among the detection and selection options) is the easiest way to change the fallback language for the website, if you need to change how your site works by default (eg. when using an empty path prefix or using the default domain). <a href="@admin-change-language">Changing the site\'s default language</a> itself might have other undesired side effects.
', array('@admin-change-language' => url('admin/config/regional/language'))) . '</p>';
return $output;
Angie Byron
committed
case 'admin/structure/block/manage/%':
case 'admin/structure/block/add/%/%':
Dries Buytaert
committed
if ($arg[4] == 'language' && $arg[5] == 'language_interface') {
Angie Byron
committed
return '<p>' . t('With multiple languages enabled, registered users can select their preferred language and authors can assign a specific language to content.') . '</p>';
Dries Buytaert
committed
}
break;
Angie Byron
committed
case 'admin/config/regional/content-language':
return t('Change language settings for <em>content types</em>, <em>taxonomy vocabularies</em>, <em>user profiles</em>, or any other supported element on your site. By default, language settings hide the language selector and the language is the site\'s default language.');
}
}
Angie Byron
committed
/**
* Editing or deleting locked languages should not be possible.
*
* @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0.
* Use \Drupal\language\LanguageAccessController.
Angie Byron
committed
*/
function language_access_language_edit_or_delete($language) {
return !$language->locked && \Drupal::currentUser()->hasPermission('administer languages');
Angie Byron
committed
}
/**
* Implements hook_permission().
*/
function language_permission() {
return array(
'administer languages' => array(
'title' => t('Administer languages'),
),
);
}
/**
* Implements hook_theme().
*/
function language_theme() {
return array(
Dries Buytaert
committed
'language_negotiation_configure_form' => array(
'render element' => 'form',
'file' => 'language.admin.inc',
Dries Buytaert
committed
),
Angie Byron
committed
'language_negotiation_configure_browser_form_table' => array(
'render element' => 'form',
'file' => 'language.admin.inc',
),
Angie Byron
committed
'language_content_settings_table' => array(
'render element' => 'element',
'file' => 'language.admin.inc',
),
);
}
Angie Byron
committed
/**
* Returns a list of supported entity types.
*
* @return array
* An array of entity type names.
*/
function language_entity_supported() {
$supported = array();
Alex Pott
committed
foreach (\Drupal::entityManager()->getDefinitions() as $entity_type_id => $entity_type) {
if ($entity_type->isTranslatable()) {
$supported[$entity_type_id] = $entity_type_id;
Angie Byron
committed
}
}
return $supported;
}
/**
* Implements hook_element_info_alter().
*/
function language_element_info_alter(&$type) {
// Alter the language_select element so that it will be rendered like a select
// field.
if (isset($type['language_select'])) {
if (!isset($type['language_select']['#process'])) {
$type['language_select']['#process'] = array();
}
if (!isset($type['language_select']['#theme_wrappers'])) {
$type['language_select']['#theme_wrappers'] = array();
}
$type['language_select']['#process'] = array_merge($type['language_select']['#process'], array('language_process_language_select', 'form_process_select', 'ajax_process_form'));
$type['language_select']['#theme'] = 'select';
$type['language_select']['#theme_wrappers'] = array_merge($type['language_select']['#theme_wrappers'], array('form_element'));
$type['language_select']['#languages'] = Language::STATE_CONFIGURABLE;
$type['language_select']['#multiple'] = FALSE;
}
}
/**
* Processes a language select list form element.
*
* @param array $element
* The form element to process.
*
* @return array $element
* 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'] = array();
foreach (\Drupal::languageManager()->getLanguages($element['#languages']) as $langcode => $language) {
$element['#options'][$langcode] = $language->locked ? t('- @name -', array('@name' => $language->name)) : $language->name;
}
}
// Add "Built-in English" language to the select when the default value is
// set to English but it does not exist in the options list.
//
// Drupal core includes configuration shipped in English, including default
// views, content types, user roles, filter formats, etc. To keep the Drupal
// software update-able, as well as translations update-able, we keep these
// configuration files in English even when installed in a foreign language.
// However, administrators can remove English, in which case editing such a
// configuration would lead to the language settings being changed on it. We
// avoid that by including this option and letting administrators keep it
// in English.
if (isset($element['#default_value']) && $element['#default_value'] == 'en' && !isset($element['#options']['en'])) {
// Prepend the default language at the beginning of the list.
$element['#options'] = array('en' => t('Built-in English')) + $element['#options'];
}
return $element;
}
Angie Byron
committed
/**
* Implements hook_element_info().
*/
function language_element_info() {
$types['language_configuration'] = array(
'#input' => TRUE,
'#tree' => TRUE,
'#process' => array('language_configuration_element_process'),
);
return $types;
}
/**
* Returns the default options for the language configuration form element.
*
* @return array
* An array containing the default options.
*/
function language_configuration_element_default_options() {
Angie Byron
committed
$language_options = array(
'site_default' => t("Site's default language (!language)", array('!language' => \Drupal::languageManager()->getDefaultLanguage()->name)),
Angie Byron
committed
'current_interface' => t('Current interface language'),
'authors_default' => t("Author's preferred language"),
);
Angie Byron
committed
$languages = \Drupal::languageManager()->getLanguages(Language::STATE_ALL);
Angie Byron
committed
foreach ($languages as $langcode => $language) {
$language_options[$langcode] = $language->locked ? t('- @name -', array('@name' => $language->name)) : $language->name;
}
return $language_options;
}
/**
* Process handler for the language_configuration form element.
*/
function language_configuration_element_process($element, &$form_state, &$form) {
$options = isset($element['#options']) ? $element['#options'] : array();
// Avoid validation failure since we are moving the '#options' key in the
// nested 'language' select element.
unset($element['#options']);
$element['langcode'] = array(
'#type' => 'select',
'#title' => t('Default language'),
'#options' => $options + language_configuration_element_default_options(),
'#description' => t('Explanation of the language options is found on the <a href="@languages_list_page">languages list page</a>.', array('@languages_list_page' => url('admin/config/regional/language'))),
'#default_value' => isset($element['#default_value']['langcode']) ? $element['#default_value']['langcode'] : NULL,
);
Dries Buytaert
committed
$element['language_show'] = array(
Angie Byron
committed
'#type' => 'checkbox',
Dries Buytaert
committed
'#title' => t('Show language selector on create and edit pages'),
'#default_value' => isset($element['#default_value']['language_show']) ? $element['#default_value']['language_show'] : NULL,
Angie Byron
committed
);
// Add the entity type and bundle information to the form if they are set.
// They will be used, in the submit handler, to generate the names of the
// variables that will store the settings and are a way to uniquely identify
// the entity.
if (!isset($form_state['language'])) {
$form_state['language'] = array();
}
$form_state['language'] += array(
$element['#name'] => array(
'entity_type' => $element['#entity_information']['entity_type'],
'bundle' => $element['#entity_information']['bundle'],
),
);
// Do not add the submit callback for the language content settings page,
// which is handled separately.
if (array_search('language_content_settings_form_submit', $form['#submit']) === FALSE) {
// Determine where to attach the language_configuration element submit handler.
// @todo Form API: Allow form widgets/sections to declare #submit handlers.
if (isset($form['actions']['submit']['#submit']) && array_search('language_configuration_element_submit', $form['actions']['submit']['#submit']) === FALSE) {
$form['actions']['submit']['#submit'][] = 'language_configuration_element_submit';
}
elseif (array_search('language_configuration_element_submit', $form['#submit']) === FALSE) {
$form['#submit'][] = 'language_configuration_element_submit';
}
}
Angie Byron
committed
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
return $element;
}
/**
* Submit handler for the forms that have a language_configuration element.
*/
function language_configuration_element_submit(&$form, &$form_state) {
// Iterate through all the language_configuration elements and save their
// values.
if (isset($form_state['language'])) {
foreach ($form_state['language'] as $element_name => $values) {
language_save_default_configuration($values['entity_type'], $values['bundle'], $form_state['values'][$element_name]);
}
}
}
/**
* Saves a language configuration that is attached to an entity type and bundle.
*
* @param string $entity_type
* A string representing the entity type.
* @param string $bundle
* A string representing the bundle.
* @param array $values
* An array holding the values to be saved having the following keys:
* - langcode: the language code.
Dries Buytaert
committed
* - language_show: if the language element should be hidden or not.
Angie Byron
committed
*/
function language_save_default_configuration($entity_type, $bundle, $values = array()) {
\Drupal::config('language.settings')->set(language_get_default_configuration_settings_key($entity_type, $bundle), array('langcode' => $values['langcode'], 'language_show' => $values['language_show']))->save();
Angie Byron
committed
}
/**
* Returns the language configuration stored for an entity type and bundle.
*
* @param string $entity_type
* A string representing the entity type.
* @param string $bundle
* A string representing the bundle.
*
* @return array
* An array with the following keys:
* - langcode: the language code.
Dries Buytaert
committed
* - language_show: if the language element is hidden or not.
Angie Byron
committed
*/
function language_get_default_configuration($entity_type, $bundle) {
$configuration = \Drupal::config('language.settings')->get(language_get_default_configuration_settings_key($entity_type, $bundle));
Angie Byron
committed
if (is_null($configuration)) {
$configuration = array();
}
Dries Buytaert
committed
$configuration += array('langcode' => 'site_default', 'language_show' => FALSE);
Angie Byron
committed
return $configuration;
}
/**
* Clears the default language configuration for an entity type and bundle.
*
* @param string $entity_type
* A string representing the entity type.
* @param string $bundle
* A string representing the bundle.
*/
function language_clear_default_configuration($entity_type, $bundle) {
\Drupal::config('language.settings')->clear(language_get_default_configuration_settings_key($entity_type, $bundle))->save();
Angie Byron
committed
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
}
/**
* Returns the root name of the variables used to store the configuration.
*
* Based on the entity type and bundle, the variables used to store the
* configuration will have a common root name.
*
* @param string $entity_type
* A string representing the entity type.
* @param string $bundle
* A string representing the bundle.
*
* @return string
* The root name of the variables.
*/
function language_get_default_configuration_settings_key($entity_type, $bundle) {
// Replace all the characters that are not letters, numbers or "_" with "_".
$entity_type = preg_replace('/[^0-9a-zA-Z_]/', "_", $entity_type);
$bundle = preg_replace('/[^0-9a-zA-Z_]/', "_", $bundle);
return $entity_type . '.' . $bundle . '.language.default_configuration';
}
/**
* Implements hook_node_type_update().
*/
function language_node_type_update(NodeTypeInterface $type) {
if ($type->original->id() != $type->id()) {
language_save_default_configuration('node', $type->id(), language_get_default_configuration('node', $type->original->id()));
language_clear_default_configuration('node', $type->original->id());
Angie Byron
committed
}
}
/**
* 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 = language_get_default_configuration($entity_type, $bundle);
if (!isset($configuration['langcode'])) {
$configuration['langcode'] = 'site_default';
}
$default_value = NULL;
$language_interface = \Drupal::languageManager()->getCurrentLanguage();
Angie Byron
committed
switch ($configuration['langcode']) {
case 'site_default':
$default_value = \Drupal::languageManager()->getDefaultLanguage()->id;
Angie Byron
committed
break;
case 'current_interface':
$default_value = $language_interface->id;
Angie Byron
committed
break;
case 'authors_default':
$user = \Drupal::currentUser();
$language_code = $user->getPreferredLangcode();
if (!empty($language_code)) {
$default_value = $language_code;
Angie Byron
committed
}
else {
$default_value = $language_interface->id;
Angie Byron
committed
}
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['langcode'];
}
/**
* API function to add or update a language.
*
* @param $language
* Language object with properties corresponding to the 'language'
* configuration properties.
*/
function language_save($language) {
$language_entity = entity_load('language_entity', $language->id);
if (!$language_entity) {
$language_entity = entity_create('language_entity', array(
'id' => $language->id,
));
}
$language->is_new = $language_entity->isNew();
// Assign language properties to language entity.
$language_entity->label = isset($language->name) ? $language->name : '';
$language_entity->direction = isset($language->direction) ? $language->direction : '0';
$language_entity->locked = !empty($language->locked);
$language_entity->weight = isset($language->weight) ? $language->weight : 0;
// Save the record and inform others about the change.
$multilingual = \Drupal::languageManager()->isMultilingual();
$language_entity->save();
$t_args = array('%language' => $language->name, '%langcode' => $language->id);
if ($language->is_new) {
watchdog('language', 'The %language (%langcode) language has been created.', $t_args);
}
else {
watchdog('language', 'The %language (%langcode) language has been updated.', $t_args);
}
if (!empty($language->default)) {
catch
committed
// Update the config. Saving the configuration fires and event that causes
// the container to be rebuilt.
\Drupal::config('system.site')->set('langcode', $language->id)->save();
\Drupal::service('language.default')->set($language);
}
$language_manager = \Drupal::languageManager();
$language_manager->reset();
if ($language_manager instanceof ConfigurableLanguageManagerInterface) {
$language_manager->updateLockedLanguageWeights();
}
// Update URL Prefixes for all languages after the new default language is
// propagated and the LanguageManagerInterface::getLanguages() cache is
// flushed.
language_negotiation_url_prefixes_update();
// If after adding this language the site will become multilingual, we need to
// rebuild language services.
if (!$multilingual && $language->is_new) {
ConfigurableLanguageManager::rebuildServices();
}
return $language;
}
/**
* Delete a language.
*
* @param $langcode
* Language code of the language to be deleted.
Angie Byron
committed
*
* @return
* TRUE if language is successfully deleted. Otherwise FALSE.
*/
function language_delete($langcode) {
$languages = \Drupal::languageManager()->getLanguages(Language::STATE_ALL);
Angie Byron
committed
if (isset($languages[$langcode]) && !$languages[$langcode]->locked) {
$language = $languages[$langcode];
// Remove the language.
entity_delete_multiple('language_entity', array($language->id));
$language_manager = \Drupal::languageManager();
$language_manager->reset();
if ($language_manager instanceof ConfigurableLanguageManagerInterface) {
$language_manager->updateLockedLanguageWeights();
}
// If after deleting this language the site will become monolingual, we need
// to rebuild language services.
if (!\Drupal::languageManager()->isMultilingual()) {
ConfigurableLanguageManager::rebuildServices();
}
Dries Buytaert
committed
$t_args = array('%language' => $language->name, '%langcode' => $language->id);
watchdog('language', 'The %language (%langcode) language has been removed.', $t_args);
return TRUE;
}
return FALSE;
}
Dries Buytaert
committed
Dries Buytaert
committed
/**
* Implements hook_language_types_info().
*
* Defines the three core language types:
* - Interface language is the only configurable language type in core. It is
* used by t() as the default language if none is specified.
* - Content language is by default non-configurable and inherits the interface
* language negotiated value. It is used by the Field API to determine the
* display language for fields if no explicit value is specified.
* - URL language is by default non-configurable and is determined through the
* URL language negotiation method or the URL fallback language negotiation
* method if no language can be detected. It is used by l() as the default
* language if none is specified.
*/
function language_language_types_info() {
return array(
Language::TYPE_INTERFACE => array(
Dries Buytaert
committed
'name' => t('User interface text'),
'description' => t('Order of language detection methods for user interface text. If a translation of user interface text is available in the detected language, it will be displayed.'),
'locked' => TRUE,
Dries Buytaert
committed
),
Language::TYPE_CONTENT => array(
Dries Buytaert
committed
'name' => t('Content'),
'description' => t('Order of language detection methods for content. If a version of content is available in the detected language, it will be displayed.'),
'fixed' => array(LanguageNegotiationUI::METHOD_ID),
'locked' => TRUE,
Dries Buytaert
committed
),
Language::TYPE_URL => array(
'fixed' => array(LanguageNegotiationUrl::METHOD_ID, LanguageNegotiationUrlFallback::METHOD_ID),
'locked' => TRUE,
Dries Buytaert
committed
),
);
}
/**
* Reads language prefixes and uses the langcode if no prefix is set.
Dries Buytaert
committed
*/
function language_negotiation_url_prefixes() {
return \Drupal::config('language.negotiation')->get('url.prefixes');
}
Dries Buytaert
committed
/**
* Update the list of prefixes from the installed languages.
*/
function language_negotiation_url_prefixes_update() {
$prefixes = language_negotiation_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->id])) {
// For the default language, set the prefix to the empty string,
// otherwise use the langcode.
$prefixes[$language->id] = !empty($language->default) ? '' : $language->id;
}
// Otherwise we keep the configured prefix.
}
language_negotiation_url_prefixes_save($prefixes);
}
Dries Buytaert
committed
/**
* Saves language prefix settings.
*/
function language_negotiation_url_prefixes_save(array $prefixes) {
\Drupal::config('language.negotiation')
->set('url.prefixes', $prefixes)
->save();
}
Dries Buytaert
committed
/**
* Reads language domains.
*/
function language_negotiation_url_domains() {
return \Drupal::config('language.negotiation')->get('url.domains');
Dries Buytaert
committed
}
/**
* Saves the language domain settings.
Dries Buytaert
committed
*/
function language_negotiation_url_domains_save(array $domains) {
\Drupal::config('language.negotiation')
->set('url.domains', $domains)
->save();
Dries Buytaert
committed
}
/**
Dries Buytaert
committed
* Implements hook_modules_installed().
Dries Buytaert
committed
*/
Dries Buytaert
committed
function language_modules_installed($modules) {
if (!in_array('language', $modules)) {
$negotiator = \Drupal::service('language_negotiator');
$negotiator->updateConfiguration(array());
$negotiator->purgeConfiguration();
}
Dries Buytaert
committed
}
/**
Dries Buytaert
committed
* Implements hook_modules_uninstalled().
Dries Buytaert
committed
*/
Dries Buytaert
committed
function language_modules_uninstalled($modules) {
language_modules_installed($modules);
Dries Buytaert
committed
}
/**
* Implements hook_ENTITY_TYPE_insert() for 'language_entity'.
Dries Buytaert
committed
*/
function language_language_entity_insert(LanguageEntity $language) {
Angie Byron
committed
if (!empty($language->locked)) {
return;
}
Dries Buytaert
committed
// Add language to the list of language domains.
$domains = language_negotiation_url_domains();
$domains[$language->id()] = '';
Dries Buytaert
committed
language_negotiation_url_domains_save($domains);
}
/**
* Implements hook_ENTITY_TYPE_delete() for 'language_entity'.
Dries Buytaert
committed
*/
function language_language_entity_delete(LanguageEntity $language) {
Dries Buytaert
committed
// Remove language from language prefix list.
$prefixes = language_negotiation_url_prefixes();
unset($prefixes[$language->id()]);
Dries Buytaert
committed
language_negotiation_url_prefixes_save($prefixes);
// Remove language from language domain list.
$domains = language_negotiation_url_domains();
unset($domains[$language->id()]);
Dries Buytaert
committed
language_negotiation_url_domains_save($domains);
}
/**
Angie Byron
committed
* Implements hook_preprocess_HOOK() for block templates.
Dries Buytaert
committed
*/
function language_preprocess_block(&$variables) {
Alex Pott
committed
if ($variables['configuration']['provider'] == 'language') {
Dries Buytaert
committed
$variables['attributes']['role'] = 'navigation';
Dries Buytaert
committed
}
}
Angie Byron
committed
/**
* 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');
Angie Byron
committed
if ($config->isNew()) {
return array();
}
return $config->get();
}
/**
* Stores language mappings between browser and Drupal language codes.
*
* @param array $mappings
* An array containing browser language codes as keys with corresponding
* Drupal language codes as values.
*/
function language_set_browser_drupal_langcode_mappings($mappings) {
$config = \Drupal::config('language.mappings');
Angie Byron
committed
$config->setData($mappings);
$config->save();
}
Dries Buytaert
committed
Dries Buytaert
committed
/**
* Implements hook_form_FORM_ID_alter for system_regional_settings().
*
* @see language_system_regional_settings_form_submit()
*/
function language_form_system_regional_settings_alter(&$form, &$form_state) {
$languages = \Drupal::languageManager()->getLanguages();
$default = \Drupal::languageManager()->getDefaultLanguage();
Dries Buytaert
committed
foreach ($languages as $key => $language) {
$language_options[$key] = $language->name;
}
$form['locale']['site_default_language'] = array(
'#type' => 'select',
'#title' => t('Default language'),
'#default_value' => $default->id,
Dries Buytaert
committed
'#options' => $language_options,
'#description' => t('It is not recommended to change the default language on a working site. <a href="@language-detection">Configure the Selected language</a> setting on the detection and selection page to change the fallback language for language selection.', array('@language-detection' => url('admin/config/regional/language/detection'))),
'#weight' => -1,
);
// Add submit handler to save default language.
$form['#submit'][] = 'language_system_regional_settings_form_submit';
}
/**
* Form submission handler for system_regional_settings().
*
* @see language_form_system_regional_settings_alter()
*/
function language_system_regional_settings_form_submit($form, &$form_state) {
$languages = \Drupal::languageManager()->getLanguages();
Dries Buytaert
committed
$language = $languages[$form_state['values']['site_default_language']];
$language->default = TRUE;
language_save($language);
}