2011-12-22 11:26:12 +00:00
<?php
/**
* @file
* Add language handling functionality to Drupal.
*/
2014-07-31 00:50:42 +00:00
use Drupal\Core\Form\FormStateInterface;
2014-04-07 08:14:46 +00:00
use Drupal\Core\PhpStorage\PhpStorageFactory;
2014-06-10 16:52:58 +00:00
use Drupal\Core\Language\LanguageInterface;
2014-06-30 03:33:08 +00:00
use Drupal\Core\Routing\RouteMatchInterface;
Issue #1862202 by plach, Berdir, katbailey, ParisLiakos, alexpott, chx, sun, larowlan, Gábor Hojtsy, cosmicdreams, vijaycs85, YesCT, penyaskito, andypost, Albert Volkman, joelpitett: Objectify the language system.
2014-01-15 16:27:37 +00:00
use Drupal\language\ConfigurableLanguageManager;
use Drupal\language\ConfigurableLanguageManagerInterface;
2014-03-25 19:51:00 +00:00
use Drupal\language\Entity\Language as LanguageEntity;
Issue #1862202 by plach, Berdir, katbailey, ParisLiakos, alexpott, chx, sun, larowlan, Gábor Hojtsy, cosmicdreams, vijaycs85, YesCT, penyaskito, andypost, Albert Volkman, joelpitett: Objectify the language system.
2014-01-15 16:27:37 +00:00
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUI;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrlFallback;
use Drupal\node\NodeTypeInterface;
2013-05-25 20:12:45 +00:00
2011-12-22 11:26:12 +00:00
/**
* Implements hook_help().
*/
2014-06-30 03:33:08 +00:00
function language_help($route_name, RouteMatchInterface $route_match) {
2014-05-07 02:04:53 +00:00
switch ($route_name) {
case 'help.page.language':
2011-12-22 11:26:12 +00:00
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
2014-06-02 17:16:37 +00:00
$output .= '<p>' . t('The Language module allows you to configure the languages used on your site, and provides information for the for <a href="!content">Content Translation</a>, <a href="!interface">Interface Translation</a>, and <a href="!configuration">Configuration Translation</a> modules, if they are enabled. For more information, see <a href="!doc_url">the online documentation for the Language module</a>.', array('!doc_url' => 'https://drupal.org/documentation/modules/language', '!content' => (\Drupal::moduleHandler()->moduleExists('content_translation')) ? \Drupal::url('help.page', array('name' => 'content_translation')) : '#', '!interface' => (\Drupal::moduleHandler()->moduleExists('locale')) ? \Drupal::url('help.page', array('name' => 'locale')) : '#', '!configuration' => (\Drupal::moduleHandler()->moduleExists('config_translation')) ? \Drupal::url('help.page', array('name' => 'config_translation')) : '#')) . '</p>';
2011-12-22 11:26:12 +00:00
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
2014-06-02 17:16:37 +00:00
$output .= '<dt>' . t('Adding languages') . '</dt>';
$output .= '<dd>' . t('You can add languages on the <a href="!language_list">Languages</a> page by selecting <em>Add language</em> and choosing a language from the drop-down menu. This language is then displayed in the languages list, where it can be configured further. If the <a href="!interface">Interface translation module</a> is enabled, and the <em>translation server</em> is set as a translation source, then the interface translation for this language is automatically downloaded as well.' , array('!language_list' => \Drupal::url('language.admin_overview'), '!interface' => (\Drupal::moduleHandler()->moduleExists('locale')) ? \Drupal::url('help.page', array('name' => 'locale')) : '#')) . '</dd>';
$output .= '<dt>' . t('Adding custom languages') . '</dt>';
$output .= '<dd>' . t('You can add a language that is not provided in the drop-down list by choosing <em>Custom language</em> at the end of the list. You then have to configure its language code, name, and direction in the form provided.') . '</dd>';
$output .= '<dt>' . t('Configuring content languages') . '</dt>';
$output .= '<dd>' . t('By default, content is created in the site\'s default language and no language selector is displayed on content creation pages. On the <a href="!content_language">Content language</a> page you can customize the language configuration for any supported content entity on your site (for example for content types or menu links). After choosing an entity, you are provided with a drop-down menu to set the default language and a check-box to display language selectors.', array('!content_language' => \Drupal::url('language.content_settings_page'))) . '</dd>';
$output .= '<dt>' . t('Adding a language switcher block') . '</dt>';
$output .= '<dd>' . t('If the Block module is enabled, then you can add a language switcher block on the <a href="!blocks">Block layout</a> page to allow users to switch between languages.', array('!blocks' => (\Drupal::moduleHandler()->moduleExists('block')) ? \Drupal::url('block.admin_display') : '#')) . '</dd>';
$output .= '<dt>' . t('Making a block visible per language') . '</dt>';
$output .= '<dd>' . t('If the Block module is enabled, then the Language module allows you to set the visibility of a block based on selected languages on the <a href="!blocks">Block layout</a> page.', array('!blocks' => (\Drupal::moduleHandler()->moduleExists('block')) ? \Drupal::url('block.admin_display') : '#')) . '</dd>';
$output .= '<dt>' . t('Choosing user languages') . '</dt>';
$output .= '<dd>' . t('Users can choose a <em>Site language</em> on their profile page. This language is used for email messages, and can be used by modules to determine a user\'s language. It can also be used for interface text, if the <em>User</em> method is enabled as a <em>Detection and selection</em> method (see below). Administrative users can choose a separate <em>Administration pages language</em> for the interface text on administration pages. This configuration is only available on the user\'s profile page if the <em>Account administration pages</em> method is enabled (see below).') . '</dd>';
$output .= '<dt>' . t('Language detection and selection') . '</dt>';
$output .= '<dd>' . t('The <a href="!detection">Detection and selection</a> page provides several methods for deciding which language is used for displaying interface text. When a method detects and selects an interface language, then the following methods in the list are not applied. You can order them by importance, with your preferred method at the top of the list, followed by one or several fall-back methods.', array('!detection' => \Drupal::url('language.negotiation')));
$output .= '<ul><li>' . t('<em>URL</em> sets the interface language based on a path prefix or domain (for example specifying <em>de</em> for German would result in URLs like <em>example.com/de/contact</em>). The default language does not require a path prefix, but can have one assigned as well. If the language detection is done by domain name, a domain needs to be specified for each language.') . '</li>';
$output .= '<li>' . t('<em>Session</em> determines the interface language from a request or session parameter (for example <em>example.com?language=de</em> would set the interface language to German based on the use of <em>de</em> as the <em>language</em> parameter).') . '</li>';
$output .= '<li>' . t('<em>User</em> follows the language configuration set on the user\'s profile page.') . '</li>';
$output .= '<li>' . t('<em>Browser</em> sets the interface language based on the browser\'s language settings. Since browsers use different language codes to refer to the same languages, you can add and edit languages codes to map the browser language codes to the <a href="!language_list">language codes</a> used on your site.', array('!language_list' => \Drupal::url('language.admin_overview'))) . '</li>';
$output .= '<li>' . t('<em>Account administration pages</em> follows the configuration set as <em>Administration pages language</em> on the profile page of an administrative user. This method is similar to the <em>User</em> method, but only sets the interface text language on administration pages, independent of the interface text language on other pages.') . '</li>';
$output .= '<li>' . t('<em>Selected language</em> allows you to specify the site\'s default language or a specific language as the fall-back language. This method should be listed last.') . '</li></ul></dd>';
2011-12-22 11:26:12 +00:00
$output .= '</dl>';
return $output;
2014-05-07 02:04:53 +00:00
case 'language.admin_overview':
2014-04-10 16:47:42 +00:00
return '<p>' . t('Reorder the added languages to set their order in the language switcher block and, when editing content, in the list of selectable languages. This ordering does not impact <a href="@detection">detection and selection</a>.', array('@detection' => url('admin/config/regional/language/detection'))) . '</p>';
2011-12-22 11:26:12 +00:00
2014-05-07 02:04:53 +00:00
case 'language.add':
2011-12-22 11:26:12 +00:00
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>';
2012-04-11 14:47:06 +00:00
2014-05-07 02:04:53 +00:00
case 'language.negotiation':
2014-06-02 17:16:37 +00:00
$output = '<p>' . t('Define how to decide which language is used to display page elements (primarily text provided by 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. The default language can be changed at the <a href="!region-settings">Regional settings</a> page.', array('!region-settings' => \Drupal::url('system.regional_settings'))) . '</p>';
2012-04-11 14:47:06 +00:00
return $output;
2014-05-07 02:04:53 +00:00
case 'language.negotiation_session':
2012-04-11 14:47:06 +00:00
$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;
2014-05-07 02:04:53 +00:00
case 'language.negotiation_browser':
2014-06-02 17:16:37 +00:00
$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</a>.', array('!configure-languages' => \Drupal::url('language.admin_overview'))) . '</p>';
2012-09-06 05:48:34 +00:00
return $output;
2014-05-07 02:04:53 +00:00
case 'language.negotiation_selected':
2014-06-02 17:16:37 +00:00
$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 (e.g., 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' => \Drupal::url('language.admin_overview'))) . '</p>';
2012-10-09 16:11:09 +00:00
return $output;
2014-08-04 16:55:19 +00:00
case 'entity.block.edit_form':
2014-06-30 03:33:08 +00:00
if (($block = $route_match->getParameter('block')) && $block->get('plugin') == 'language_block:language_interface') {
2014-05-04 17:19:57 +00:00
return '<p>' . t('With multiple languages added, registered users can select their preferred language and authors can assign a specific language to content.') . '</p>';
2012-04-11 14:47:06 +00:00
}
break;
2012-12-29 08:13:54 +00:00
2014-05-07 02:04:53 +00:00
case 'block.admin_add':
2014-06-30 03:33:08 +00:00
if ($route_match->getParameter('plugin_id') == 'language_block:language_interface') {
2014-05-07 02:04:53 +00:00
return '<p>' . t('With multiple languages added, registered users can select their preferred language and authors can assign a specific language to content.') . '</p>';
}
break;
case 'language.content_settings_page':
2012-12-29 08:13:54 +00:00
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.');
2011-12-22 11:26:12 +00:00
}
}
/**
* Implements hook_permission().
*/
function language_permission() {
return array(
'administer languages' => array(
'title' => t('Administer languages'),
),
);
}
/**
* Implements hook_theme().
*/
function language_theme() {
return array(
2012-04-11 14:47:06 +00:00
'language_negotiation_configure_form' => array(
'render element' => 'form',
2014-01-02 19:42:44 +00:00
'file' => 'language.admin.inc',
2014-06-09 23:04:36 +00:00
'template' => 'language-negotiation-configure-form',
2012-04-11 14:47:06 +00:00
),
2012-09-06 05:48:34 +00:00
'language_negotiation_configure_browser_form_table' => array(
'render element' => 'form',
'file' => 'language.admin.inc',
),
2012-12-29 08:13:54 +00:00
'language_content_settings_table' => array(
'render element' => 'element',
'file' => 'language.admin.inc',
),
2011-12-22 11:26:12 +00:00
);
}
2012-08-21 23:31:54 +00:00
/**
* 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'));
2014-06-10 16:52:58 +00:00
$type['language_select']['#languages'] = LanguageInterface::STATE_CONFIGURABLE;
2012-08-21 23:31:54 +00:00
$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) {
2012-08-22 19:55:05 +00:00
// Don't set the options if another module (translation for example) already
2012-08-21 23:31:54 +00:00
// set the options.
if (!isset($element['#options'])) {
$element['#options'] = array();
2014-03-31 17:29:01 +00:00
foreach (\Drupal::languageManager()->getLanguages($element['#languages']) as $langcode => $language) {
2012-08-21 23:31:54 +00:00
$element['#options'][$langcode] = $language->locked ? t('- @name -', array('@name' => $language->name)) : $language->name;
}
}
2013-10-04 10:29:32 +00:00
// 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'];
}
2012-08-21 23:31:54 +00:00
return $element;
}
2012-10-18 08:59:12 +00:00
/**
* 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() {
2013-11-28 09:07:11 +00:00
$language_options = array(
2014-03-31 17:29:01 +00:00
'site_default' => t("Site's default language (!language)", array('!language' => \Drupal::languageManager()->getDefaultLanguage()->name)),
2013-11-28 09:07:11 +00:00
'current_interface' => t('Current interface language'),
'authors_default' => t("Author's preferred language"),
);
2012-10-18 08:59:12 +00:00
2014-06-10 16:52:58 +00:00
$languages = \Drupal::languageManager()->getLanguages(LanguageInterface::STATE_ALL);
2012-10-18 08:59:12 +00:00
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.
*/
2014-07-31 00:50:42 +00:00
function language_configuration_element_process($element, FormStateInterface $form_state, &$form) {
2012-10-18 08:59:12 +00:00
$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,
);
2013-01-16 01:11:30 +00:00
$element['language_show'] = array(
2012-10-18 08:59:12 +00:00
'#type' => 'checkbox',
2013-01-16 01:11:30 +00:00
'#title' => t('Show language selector on create and edit pages'),
'#default_value' => isset($element['#default_value']['language_show']) ? $element['#default_value']['language_show'] : NULL,
2012-10-18 08:59:12 +00:00
);
// 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'],
),
);
2013-06-27 05:48:51 +00:00
// Do not add the submit callback for the language content settings page,
// which is handled separately.
Issue #1987726 by vijaycs85, disasm, YesCT, pdrake, tstoeckler, dawehner, Xano, likin, stella, andypost, prateek479, Gábor Hojtsy, kgoel: Convert language content page related callback to new style controller.
2014-05-23 12:34:47 +00:00
if ($form['#form_id'] != 'language_content_settings_form') {
2013-06-27 05:48:51 +00:00
// 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';
}
}
2012-10-18 08:59:12 +00:00
return $element;
}
/**
* Submit handler for the forms that have a language_configuration element.
*/
2014-07-31 00:50:42 +00:00
function language_configuration_element_submit(&$form, FormStateInterface $form_state) {
2012-10-18 08:59:12 +00:00
// Iterate through all the language_configuration elements and save their
// values.
if (isset($form_state['language'])) {
foreach ($form_state['language'] as $element_name => $values) {
2014-08-08 16:25:54 +00:00
language_save_default_configuration($values['entity_type'], $values['bundle'], $form_state->getValue($element_name));
2012-10-18 08:59:12 +00:00
}
}
}
/**
* 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.
2013-01-16 01:11:30 +00:00
* - language_show: if the language element should be hidden or not.
2012-10-18 08:59:12 +00:00
*/
function language_save_default_configuration($entity_type, $bundle, $values = array()) {
2013-09-16 03:58:06 +00:00
\Drupal::config('language.settings')->set(language_get_default_configuration_settings_key($entity_type, $bundle), array('langcode' => $values['langcode'], 'language_show' => $values['language_show']))->save();
2012-10-18 08:59:12 +00:00
}
/**
* 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.
2013-01-16 01:11:30 +00:00
* - language_show: if the language element is hidden or not.
2012-10-18 08:59:12 +00:00
*/
function language_get_default_configuration($entity_type, $bundle) {
2013-09-16 03:58:06 +00:00
$configuration = \Drupal::config('language.settings')->get(language_get_default_configuration_settings_key($entity_type, $bundle));
2012-10-18 08:59:12 +00:00
if (is_null($configuration)) {
$configuration = array();
}
2013-01-16 01:11:30 +00:00
$configuration += array('langcode' => 'site_default', 'language_show' => FALSE);
2012-10-18 08:59:12 +00:00
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) {
2013-09-16 03:58:06 +00:00
\Drupal::config('language.settings')->clear(language_get_default_configuration_settings_key($entity_type, $bundle))->save();
2012-10-18 08:59:12 +00:00
}
/**
* 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);
2014-05-06 10:42:01 +00:00
return 'entities.' . $entity_type . '.' . $bundle . '.language.default_configuration';
2012-10-18 08:59:12 +00:00
}
/**
2014-07-11 12:04:53 +00:00
* Implements hook_ENTITY_TYPE_update() for node_type entities.
2012-10-18 08:59:12 +00:00
*/
2013-06-27 05:48:51 +00:00
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());
2012-10-18 08:59:12 +00:00
}
}
/**
* 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;
2014-02-26 19:16:54 +00:00
$language_interface = \Drupal::languageManager()->getCurrentLanguage();
2012-10-18 08:59:12 +00:00
switch ($configuration['langcode']) {
case 'site_default':
2014-03-31 17:29:01 +00:00
$default_value = \Drupal::languageManager()->getDefaultLanguage()->id;
2012-10-18 08:59:12 +00:00
break;
case 'current_interface':
2013-06-29 10:56:53 +00:00
$default_value = $language_interface->id;
2012-10-18 08:59:12 +00:00
break;
case 'authors_default':
2013-10-21 09:24:14 +00:00
$user = \Drupal::currentUser();
2013-07-09 13:46:44 +00:00
$language_code = $user->getPreferredLangcode();
if (!empty($language_code)) {
$default_value = $language_code;
2012-10-18 08:59:12 +00:00
}
else {
2013-06-29 10:56:53 +00:00
$default_value = $language_interface->id;
2012-10-18 08:59:12 +00:00
}
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'];
}
2011-12-22 11:26:12 +00:00
/**
* API function to add or update a language.
*
* @param $language
2013-06-29 10:56:53 +00:00
* Language object with properties corresponding to the 'language'
* configuration properties.
2011-12-22 11:26:12 +00:00
*/
function language_save($language) {
2013-06-29 10:56:53 +00:00
$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();
2011-12-22 11:26:12 +00:00
2013-06-29 10:56:53 +00:00
// Assign language properties to language entity.
$language_entity->label = isset($language->name) ? $language->name : '';
$language_entity->direction = isset($language->direction) ? $language->direction : '0';
2013-10-04 11:01:09 +00:00
$language_entity->locked = !empty($language->locked);
$language_entity->weight = isset($language->weight) ? $language->weight : 0;
2014-04-22 18:17:41 +00:00
$language_entity->setDefault(!empty($language->default));
2013-06-29 10:56:53 +00:00
$language_entity->save();
$t_args = array('%language' => $language->name, '%langcode' => $language->id);
2011-12-22 11:26:12 +00:00
if ($language->is_new) {
2014-05-24 07:52:35 +00:00
// Install any available language configuration overrides for the language.
\Drupal::service('language.config_factory_override')->installLanguageOverrides($language->getId());
2014-06-26 18:55:12 +00:00
\Drupal::logger('language')->notice('The %language (%langcode) language has been created.', $t_args);
2011-12-22 11:26:12 +00:00
}
else {
2014-06-26 18:55:12 +00:00
\Drupal::logger('language')->notice('The %language (%langcode) language has been updated.', $t_args);
2011-12-22 11:26:12 +00:00
}
Issue #1862202 by plach, Berdir, katbailey, ParisLiakos, alexpott, chx, sun, larowlan, Gábor Hojtsy, cosmicdreams, vijaycs85, YesCT, penyaskito, andypost, Albert Volkman, joelpitett: Objectify the language system.
2014-01-15 16:27:37 +00:00
return $language;
2012-08-21 23:31:54 +00:00
}
2011-12-22 11:26:12 +00:00
/**
* Delete a language.
*
* @param $langcode
* Language code of the language to be deleted.
2012-10-18 08:59:12 +00:00
*
2011-12-22 11:26:12 +00:00
* @return
* TRUE if language is successfully deleted. Otherwise FALSE.
*/
function language_delete($langcode) {
2014-06-10 16:52:58 +00:00
$languages = \Drupal::languageManager()->getLanguages(LanguageInterface::STATE_ALL);
2012-06-15 17:03:15 +00:00
if (isset($languages[$langcode]) && !$languages[$langcode]->locked) {
2011-12-22 11:26:12 +00:00
$language = $languages[$langcode];
// Remove the language.
2013-06-29 10:56:53 +00:00
entity_delete_multiple('language_entity', array($language->id));
2011-12-22 11:26:12 +00:00
Issue #1862202 by plach, Berdir, katbailey, ParisLiakos, alexpott, chx, sun, larowlan, Gábor Hojtsy, cosmicdreams, vijaycs85, YesCT, penyaskito, andypost, Albert Volkman, joelpitett: Objectify the language system.
2014-01-15 16:27:37 +00:00
$language_manager = \Drupal::languageManager();
$language_manager->reset();
if ($language_manager instanceof ConfigurableLanguageManagerInterface) {
$language_manager->updateLockedLanguageWeights();
}
2011-12-22 11:26:12 +00:00
Issue #1862202 by plach, Berdir, katbailey, ParisLiakos, alexpott, chx, sun, larowlan, Gábor Hojtsy, cosmicdreams, vijaycs85, YesCT, penyaskito, andypost, Albert Volkman, joelpitett: Objectify the language system.
2014-01-15 16:27:37 +00:00
// If after deleting this language the site will become monolingual, we need
// to rebuild language services.
if (!\Drupal::languageManager()->isMultilingual()) {
ConfigurableLanguageManager::rebuildServices();
}
2013-01-16 13:41:49 +00:00
2013-06-29 10:56:53 +00:00
$t_args = array('%language' => $language->name, '%langcode' => $language->id);
2014-06-26 18:55:12 +00:00
\Drupal::logger('language')->notice('The %language (%langcode) language has been removed.', $t_args);
2011-12-22 11:26:12 +00:00
return TRUE;
}
return FALSE;
}
2012-01-23 02:18:20 +00:00
2012-04-11 14:47:06 +00:00
/**
* 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(
2014-06-10 16:52:58 +00:00
LanguageInterface::TYPE_INTERFACE => array(
2012-04-11 14:47:06 +00:00
'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.'),
2013-07-03 12:48:55 +00:00
'locked' => TRUE,
2012-04-11 14:47:06 +00:00
),
2014-06-10 16:52:58 +00:00
LanguageInterface::TYPE_CONTENT => array(
2012-04-11 14:47:06 +00:00
'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.'),
Issue #1862202 by plach, Berdir, katbailey, ParisLiakos, alexpott, chx, sun, larowlan, Gábor Hojtsy, cosmicdreams, vijaycs85, YesCT, penyaskito, andypost, Albert Volkman, joelpitett: Objectify the language system.
2014-01-15 16:27:37 +00:00
'fixed' => array(LanguageNegotiationUI::METHOD_ID),
2013-07-03 12:48:55 +00:00
'locked' => TRUE,
2012-04-11 14:47:06 +00:00
),
2014-06-10 16:52:58 +00:00
LanguageInterface::TYPE_URL => array(
Issue #1862202 by plach, Berdir, katbailey, ParisLiakos, alexpott, chx, sun, larowlan, Gábor Hojtsy, cosmicdreams, vijaycs85, YesCT, penyaskito, andypost, Albert Volkman, joelpitett: Objectify the language system.
2014-01-15 16:27:37 +00:00
'fixed' => array(LanguageNegotiationUrl::METHOD_ID, LanguageNegotiationUrlFallback::METHOD_ID),
2013-07-03 12:48:55 +00:00
'locked' => TRUE,
2012-04-11 14:47:06 +00:00
),
);
}
/**
Issue #1862202 by plach, Berdir, katbailey, ParisLiakos, alexpott, chx, sun, larowlan, Gábor Hojtsy, cosmicdreams, vijaycs85, YesCT, penyaskito, andypost, Albert Volkman, joelpitett: Objectify the language system.
2014-01-15 16:27:37 +00:00
* Reads language prefixes and uses the langcode if no prefix is set.
2012-04-11 14:47:06 +00:00
*/
Issue #1862202 by plach, Berdir, katbailey, ParisLiakos, alexpott, chx, sun, larowlan, Gábor Hojtsy, cosmicdreams, vijaycs85, YesCT, penyaskito, andypost, Albert Volkman, joelpitett: Objectify the language system.
2014-01-15 16:27:37 +00:00
function language_negotiation_url_prefixes() {
return \Drupal::config('language.negotiation')->get('url.prefixes');
}
2012-04-11 14:47:06 +00:00
Issue #1862202 by plach, Berdir, katbailey, ParisLiakos, alexpott, chx, sun, larowlan, Gábor Hojtsy, cosmicdreams, vijaycs85, YesCT, penyaskito, andypost, Albert Volkman, joelpitett: Objectify the language system.
2014-01-15 16:27:37 +00:00
/**
* Update the list of prefixes from the installed languages.
*/
function language_negotiation_url_prefixes_update() {
$prefixes = language_negotiation_url_prefixes();
2014-03-31 17:29:01 +00:00
foreach (\Drupal::languageManager()->getLanguages() as $language) {
Issue #1862202 by plach, Berdir, katbailey, ParisLiakos, alexpott, chx, sun, larowlan, Gábor Hojtsy, cosmicdreams, vijaycs85, YesCT, penyaskito, andypost, Albert Volkman, joelpitett: Objectify the language system.
2014-01-15 16:27:37 +00:00
// 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);
}
2012-04-11 14:47:06 +00:00
Issue #1862202 by plach, Berdir, katbailey, ParisLiakos, alexpott, chx, sun, larowlan, Gábor Hojtsy, cosmicdreams, vijaycs85, YesCT, penyaskito, andypost, Albert Volkman, joelpitett: Objectify the language system.
2014-01-15 16:27:37 +00:00
/**
* Saves language prefix settings.
*/
function language_negotiation_url_prefixes_save(array $prefixes) {
\Drupal::config('language.negotiation')
->set('url.prefixes', $prefixes)
->save();
}
2012-08-31 15:23:58 +00:00
Issue #1862202 by plach, Berdir, katbailey, ParisLiakos, alexpott, chx, sun, larowlan, Gábor Hojtsy, cosmicdreams, vijaycs85, YesCT, penyaskito, andypost, Albert Volkman, joelpitett: Objectify the language system.
2014-01-15 16:27:37 +00:00
/**
* Reads language domains.
*/
function language_negotiation_url_domains() {
return \Drupal::config('language.negotiation')->get('url.domains');
2012-04-11 14:47:06 +00:00
}
/**
Issue #1862202 by plach, Berdir, katbailey, ParisLiakos, alexpott, chx, sun, larowlan, Gábor Hojtsy, cosmicdreams, vijaycs85, YesCT, penyaskito, andypost, Albert Volkman, joelpitett: Objectify the language system.
2014-01-15 16:27:37 +00:00
* Saves the language domain settings.
2012-04-11 14:47:06 +00:00
*/
Issue #1862202 by plach, Berdir, katbailey, ParisLiakos, alexpott, chx, sun, larowlan, Gábor Hojtsy, cosmicdreams, vijaycs85, YesCT, penyaskito, andypost, Albert Volkman, joelpitett: Objectify the language system.
2014-01-15 16:27:37 +00:00
function language_negotiation_url_domains_save(array $domains) {
\Drupal::config('language.negotiation')
->set('url.domains', $domains)
->save();
2012-04-11 14:47:06 +00:00
}
/**
2013-09-19 16:22:53 +00:00
* Implements hook_modules_installed().
2012-04-11 14:47:06 +00:00
*/
2013-09-19 16:22:53 +00:00
function language_modules_installed($modules) {
Issue #1862202 by plach, Berdir, katbailey, ParisLiakos, alexpott, chx, sun, larowlan, Gábor Hojtsy, cosmicdreams, vijaycs85, YesCT, penyaskito, andypost, Albert Volkman, joelpitett: Objectify the language system.
2014-01-15 16:27:37 +00:00
if (!in_array('language', $modules)) {
$negotiator = \Drupal::service('language_negotiator');
$negotiator->updateConfiguration(array());
$negotiator->purgeConfiguration();
}
2012-04-11 14:47:06 +00:00
}
/**
2013-09-19 16:22:53 +00:00
* Implements hook_modules_uninstalled().
2012-04-11 14:47:06 +00:00
*/
2013-09-19 16:22:53 +00:00
function language_modules_uninstalled($modules) {
language_modules_installed($modules);
2012-04-11 14:47:06 +00:00
}
/**
2014-03-25 19:51:00 +00:00
* Implements hook_ENTITY_TYPE_insert() for 'language_entity'.
2012-04-11 14:47:06 +00:00
*/
2014-03-25 19:51:00 +00:00
function language_language_entity_insert(LanguageEntity $language) {
2012-06-15 17:03:15 +00:00
if (!empty($language->locked)) {
return;
}
2012-04-11 14:47:06 +00:00
// Add language to the list of language domains.
$domains = language_negotiation_url_domains();
2014-03-25 19:51:00 +00:00
$domains[$language->id()] = '';
2012-04-11 14:47:06 +00:00
language_negotiation_url_domains_save($domains);
}
/**
2014-03-25 19:51:00 +00:00
* Implements hook_ENTITY_TYPE_delete() for 'language_entity'.
2012-04-11 14:47:06 +00:00
*/
2014-03-25 19:51:00 +00:00
function language_language_entity_delete(LanguageEntity $language) {
2012-04-11 14:47:06 +00:00
// Remove language from language prefix list.
$prefixes = language_negotiation_url_prefixes();
2014-03-25 19:51:00 +00:00
unset($prefixes[$language->id()]);
2012-04-11 14:47:06 +00:00
language_negotiation_url_prefixes_save($prefixes);
// Remove language from language domain list.
$domains = language_negotiation_url_domains();
2014-03-25 19:51:00 +00:00
unset($domains[$language->id()]);
2012-04-11 14:47:06 +00:00
language_negotiation_url_domains_save($domains);
}
/**
2013-10-03 20:55:34 +00:00
* Implements hook_preprocess_HOOK() for block templates.
2012-04-11 14:47:06 +00:00
*/
function language_preprocess_block(&$variables) {
2014-04-01 21:14:13 +00:00
if ($variables['configuration']['provider'] == 'language') {
2012-08-03 15:31:18 +00:00
$variables['attributes']['role'] = 'navigation';
2012-04-11 14:47:06 +00:00
}
}
2012-09-06 05:48:34 +00:00
/**
* 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() {
2013-09-16 03:58:06 +00:00
$config = \Drupal::config('language.mappings');
2012-09-06 05:48:34 +00:00
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) {
2013-09-16 03:58:06 +00:00
$config = \Drupal::config('language.mappings');
2012-09-06 05:48:34 +00:00
$config->setData($mappings);
$config->save();
}
2013-01-16 13:41:49 +00:00
2013-02-09 01:34:07 +00:00
/**
* Implements hook_form_FORM_ID_alter for system_regional_settings().
*
* @see language_system_regional_settings_form_submit()
*/
2014-07-31 00:50:42 +00:00
function language_form_system_regional_settings_alter(&$form, FormStateInterface $form_state) {
2014-03-31 17:29:01 +00:00
$languages = \Drupal::languageManager()->getLanguages();
$default = \Drupal::languageManager()->getDefaultLanguage();
2013-02-09 01:34:07 +00:00
foreach ($languages as $key => $language) {
$language_options[$key] = $language->name;
}
$form['locale']['site_default_language'] = array(
'#type' => 'select',
'#title' => t('Default language'),
2013-06-29 10:56:53 +00:00
'#default_value' => $default->id,
2013-02-09 01:34:07 +00:00
'#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()
*/
2014-07-31 00:50:42 +00:00
function language_system_regional_settings_form_submit($form, FormStateInterface $form_state) {
2014-03-31 17:29:01 +00:00
$languages = \Drupal::languageManager()->getLanguages();
2014-08-08 16:25:54 +00:00
$language = $languages[$form_state->getValue('site_default_language')];
2013-02-09 01:34:07 +00:00
$language->default = TRUE;
language_save($language);
}