annotate core/modules/language/language.module @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /**
Chris@0 4 * @file
Chris@0 5 * Add language handling functionality to Drupal.
Chris@0 6 */
Chris@0 7
Chris@18 8 use Drupal\Core\Url;
Chris@0 9 use Drupal\Core\Access\AccessResult;
Chris@0 10 use Drupal\Core\Entity\ContentEntityFormInterface;
Chris@0 11 use Drupal\Core\Entity\EntityFormInterface;
Chris@0 12 use Drupal\Core\Entity\EntityInterface;
Chris@0 13 use Drupal\Core\Field\FieldDefinitionInterface;
Chris@0 14 use Drupal\Core\Field\FieldItemListInterface;
Chris@0 15 use Drupal\Core\Form\FormStateInterface;
Chris@0 16 use Drupal\Core\Language\LanguageInterface;
Chris@0 17 use Drupal\Core\Routing\RouteMatchInterface;
Chris@0 18 use Drupal\Core\Session\AccountInterface;
Chris@0 19 use Drupal\language\Entity\ContentLanguageSettings;
Chris@0 20 use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUI;
Chris@0 21 use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
Chris@0 22 use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrlFallback;
Chris@0 23
Chris@0 24 /**
Chris@0 25 * Implements hook_help().
Chris@0 26 */
Chris@0 27 function language_help($route_name, RouteMatchInterface $route_match) {
Chris@0 28 switch ($route_name) {
Chris@0 29 case 'help.page.language':
Chris@0 30 $output = '';
Chris@0 31 $output .= '<h3>' . t('About') . '</h3>';
Chris@18 32 $output .= '<p>' . t('The Language module allows you to configure the languages used on your site, and provides information for the <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 the <a href=":doc_url">online documentation for the Language module</a>.', [':doc_url' => 'https://www.drupal.org/documentation/modules/language', ':content' => (\Drupal::moduleHandler()->moduleExists('content_translation')) ? Url::fromRoute('help.page', ['name' => 'content_translation'])->toString() : '#', ':interface' => (\Drupal::moduleHandler()->moduleExists('locale')) ? Url::fromRoute('help.page', ['name' => 'locale'])->toString() : '#', ':configuration' => (\Drupal::moduleHandler()->moduleExists('config_translation')) ? Url::fromRoute('help.page', ['name' => 'config_translation'])->toString() : '#']) . '</p>';
Chris@0 33 $output .= '<h3>' . t('Uses') . '</h3>';
Chris@0 34 $output .= '<dl>';
Chris@0 35 $output .= '<dt>' . t('Adding languages') . '</dt>';
Chris@18 36 $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.', [':language_list' => Url::fromRoute('entity.configurable_language.collection')->toString(), ':interface' => (\Drupal::moduleHandler()->moduleExists('locale')) ? Url::fromRoute('help.page', ['name' => 'locale'])->toString() : '#']) . '</dd>';
Chris@0 37 $output .= '<dt>' . t('Adding custom languages') . '</dt>';
Chris@0 38 $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>';
Chris@0 39 $output .= '<dt>' . t('Configuring content languages') . '</dt>';
Chris@18 40 $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.', [':content_language' => Url::fromRoute('language.content_settings_page')->toString()]) . '</dd>';
Chris@0 41 $output .= '<dt>' . t('Adding a language switcher block') . '</dt>';
Chris@18 42 $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.', [':blocks' => (\Drupal::moduleHandler()->moduleExists('block')) ? Url::fromRoute('block.admin_display')->toString() : '#']) . '</dd>';
Chris@0 43 $output .= '<dt>' . t('Making a block visible per language') . '</dt>';
Chris@18 44 $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.', [':blocks' => (\Drupal::moduleHandler()->moduleExists('block')) ? Url::fromRoute('block.admin_display')->toString() : '#']) . '</dd>';
Chris@0 45 $output .= '<dt>' . t('Choosing user languages') . '</dt>';
Chris@0 46 $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>';
Chris@0 47 $output .= '<dt>' . t('Language detection and selection') . '</dt>';
Chris@18 48 $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.', [':detection' => Url::fromRoute('language.negotiation')->toString()]);
Chris@0 49 $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>';
Chris@0 50 $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>';
Chris@0 51 $output .= '<li>' . t("<em>User</em> follows the language configuration set on the user's profile page.") . '</li>';
Chris@18 52 $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.', [':language_list' => Url::fromRoute('entity.configurable_language.collection')->toString()]) . '</li>';
Chris@0 53 $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>';
Chris@0 54 $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>';
Chris@0 55 $output .= '</dl>';
Chris@0 56 return $output;
Chris@0 57
Chris@0 58 case 'entity.configurable_language.collection':
Chris@18 59 $output = '<p>' . t('Reorder the configured 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>.', [':detection' => Url::fromRoute('language.negotiation')->toString()]) . '</p>';
Chris@18 60 $output .= '<p>' . t('The site default language can also be set. 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.', [':language-detection' => Url::fromRoute('language.negotiation')->toString()]) . '</p>';
Chris@0 61 return $output;
Chris@0 62
Chris@0 63 case 'language.add':
Chris@0 64 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>';
Chris@0 65
Chris@0 66 case 'language.negotiation':
Chris@18 67 $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 detection 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 in the <a href=":admin-change-language">list of languages</a>.', [':admin-change-language' => Url::fromRoute('entity.configurable_language.collection')->toString()]) . '</p>';
Chris@0 68 return $output;
Chris@0 69
Chris@0 70 case 'language.negotiation_session':
Chris@0 71 $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>';
Chris@0 72 return $output;
Chris@0 73
Chris@0 74 case 'language.negotiation_browser':
Chris@18 75 $output = '<p>' . t('Browsers use different language codes to refer to the same languages. Internally, a best effort is made to determine the correct language based on the code that the browser sends. You can add and edit additional mappings from browser language codes to <a href=":configure-languages">site languages</a>.', [':configure-languages' => Url::fromRoute('entity.configurable_language.collection')->toString()]) . '</p>';
Chris@0 76 return $output;
Chris@0 77
Chris@0 78 case 'language.negotiation_selected':
Chris@18 79 $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.', [':admin-change-language' => Url::fromRoute('entity.configurable_language.collection')->toString()]) . '</p>';
Chris@0 80 return $output;
Chris@0 81
Chris@0 82 case 'entity.block.edit_form':
Chris@0 83 if (($block = $route_match->getParameter('block')) && $block->getPluginId() == 'language_block:language_interface') {
Chris@0 84 return '<p>' . t('With multiple languages configured, registered users can select their preferred language and authors can assign a specific language to content.') . '</p>';
Chris@0 85 }
Chris@0 86 break;
Chris@0 87
Chris@0 88 case 'block.admin_add':
Chris@0 89 if ($route_match->getParameter('plugin_id') == 'language_block:language_interface') {
Chris@0 90 return '<p>' . t('With multiple languages configured, registered users can select their preferred language and authors can assign a specific language to content.') . '</p>';
Chris@0 91 }
Chris@0 92 break;
Chris@0 93
Chris@0 94 case 'language.content_settings_page':
Chris@0 95 return '<p>' . 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.") . '</p>';
Chris@0 96 }
Chris@0 97 }
Chris@0 98
Chris@0 99 /**
Chris@0 100 * Implements hook_theme().
Chris@0 101 */
Chris@0 102 function language_theme() {
Chris@0 103 return [
Chris@0 104 'language_negotiation_configure_form' => [
Chris@0 105 'render element' => 'form',
Chris@0 106 'file' => 'language.admin.inc',
Chris@0 107 ],
Chris@0 108 'language_content_settings_table' => [
Chris@0 109 'render element' => 'element',
Chris@0 110 'file' => 'language.admin.inc',
Chris@0 111 ],
Chris@0 112 ];
Chris@0 113 }
Chris@0 114
Chris@0 115 /**
Chris@0 116 * Implements hook_element_info_alter().
Chris@0 117 *
Chris@0 118 * @see \Drupal\Core\Render\Element\LanguageSelect
Chris@0 119 * @see \Drupal\Core\Render\Element\Select
Chris@0 120 */
Chris@0 121 function language_element_info_alter(&$type) {
Chris@0 122 // Alter the language_select element so that it will be rendered like a select
Chris@0 123 // field.
Chris@0 124 if (isset($type['language_select'])) {
Chris@0 125 if (!isset($type['language_select']['#process'])) {
Chris@0 126 $type['language_select']['#process'] = [];
Chris@0 127 }
Chris@0 128 if (!isset($type['language_select']['#theme_wrappers'])) {
Chris@0 129 $type['language_select']['#theme_wrappers'] = [];
Chris@0 130 }
Chris@0 131 $type['language_select']['#process'] = array_merge($type['language_select']['#process'], [
Chris@0 132 'language_process_language_select',
Chris@0 133 ['Drupal\Core\Render\Element\Select', 'processSelect'],
Chris@0 134 ['Drupal\Core\Render\Element\RenderElement', 'processAjaxForm'],
Chris@0 135 ]);
Chris@0 136 $type['language_select']['#theme'] = 'select';
Chris@0 137 $type['language_select']['#theme_wrappers'] = array_merge($type['language_select']['#theme_wrappers'], ['form_element']);
Chris@0 138 $type['language_select']['#languages'] = LanguageInterface::STATE_CONFIGURABLE;
Chris@0 139 $type['language_select']['#multiple'] = FALSE;
Chris@0 140 }
Chris@0 141 }
Chris@0 142
Chris@0 143 /**
Chris@0 144 * Processes a language select list form element.
Chris@0 145 *
Chris@0 146 * @param array $element
Chris@0 147 * The form element to process.
Chris@0 148 *
Chris@0 149 * @return array
Chris@0 150 * The processed form element.
Chris@0 151 */
Chris@0 152 function language_process_language_select($element) {
Chris@0 153 // Don't set the options if another module (translation for example) already
Chris@0 154 // set the options.
Chris@0 155 if (!isset($element['#options'])) {
Chris@0 156 $element['#options'] = [];
Chris@0 157 foreach (\Drupal::languageManager()->getLanguages($element['#languages']) as $langcode => $language) {
Chris@0 158 $element['#options'][$langcode] = $language->isLocked() ? t('- @name -', ['@name' => $language->getName()]) : $language->getName();
Chris@0 159 }
Chris@0 160 }
Chris@0 161 return $element;
Chris@0 162 }
Chris@0 163
Chris@0 164 /**
Chris@0 165 * Implements hook_entity_base_field_info_alter().
Chris@0 166 */
Chris@0 167 function language_entity_base_field_info_alter(&$fields) {
Chris@0 168 foreach ($fields as $definition) {
Chris@0 169 // Set configurable form display for language fields with display options.
Chris@0 170 if ($definition->getType() == 'language') {
Chris@0 171 foreach (['form', 'view'] as $type) {
Chris@0 172 if ($definition->getDisplayOptions($type)) {
Chris@0 173 // The related configurations will be purged manually on Language
Chris@0 174 // module uninstallation. @see language_modules_uninstalled().
Chris@0 175 $definition->setDisplayConfigurable($type, TRUE);
Chris@0 176 }
Chris@0 177 }
Chris@0 178 }
Chris@0 179 }
Chris@0 180 }
Chris@0 181
Chris@0 182 /**
Chris@0 183 * Submit handler for the forms that have a language_configuration element.
Chris@0 184 */
Chris@0 185 function language_configuration_element_submit(&$form, FormStateInterface $form_state) {
Chris@0 186 // Iterate through all the language_configuration elements and save their
Chris@0 187 // values.
Chris@0 188 // In case we are editing a bundle, we must check the new bundle name,
Chris@0 189 // because e.g. hook_ENTITY_update fired before.
Chris@0 190 if ($language = $form_state->get('language')) {
Chris@0 191 foreach ($language as $element_name => $values) {
Chris@0 192 $entity_type_id = $values['entity_type'];
Chris@0 193 $bundle = $values['bundle'];
Chris@0 194 $form_object = $form_state->getFormObject();
Chris@0 195 if ($form_object instanceof EntityFormInterface) {
Chris@12 196 /** @var \Drupal\Core\Entity\EntityFormInterface $form_object */
Chris@0 197 $entity = $form_object->getEntity();
Chris@0 198 if ($entity->getEntityType()->getBundleOf()) {
Chris@0 199 $bundle = $entity->id();
Chris@0 200 $language[$element_name]['bundle'] = $bundle;
Chris@0 201 }
Chris@0 202 }
Chris@0 203 $config = ContentLanguageSettings::loadByEntityTypeBundle($entity_type_id, $bundle);
Chris@0 204 $config->setDefaultLangcode($form_state->getValue([$element_name, 'langcode']));
Chris@0 205 $config->setLanguageAlterable($form_state->getValue([$element_name, 'language_alterable']));
Chris@0 206 $config->save();
Chris@0 207
Chris@17 208 // Set the form_state language with the updated bundle.
Chris@0 209 $form_state->set('language', $language);
Chris@0 210 }
Chris@0 211 }
Chris@0 212 }
Chris@0 213
Chris@0 214 /**
Chris@0 215 * Implements hook_entity_bundle_delete().
Chris@0 216 */
Chris@0 217 function language_entity_bundle_delete($entity_type_id, $bundle) {
Chris@0 218 // Remove the content language settings associated with the bundle.
Chris@0 219 $settings = ContentLanguageSettings::loadByEntityTypeBundle($entity_type_id, $bundle);
Chris@0 220 if (!$settings->isNew()) {
Chris@0 221 $settings->delete();
Chris@0 222 }
Chris@0 223 }
Chris@0 224
Chris@0 225 /**
Chris@0 226 * Returns the default language code assigned to an entity type and a bundle.
Chris@0 227 *
Chris@0 228 * @param string $entity_type
Chris@0 229 * The entity type.
Chris@0 230 * @param string $bundle
Chris@0 231 * The bundle name.
Chris@0 232 *
Chris@0 233 * @return string
Chris@0 234 * The language code.
Chris@0 235 */
Chris@0 236 function language_get_default_langcode($entity_type, $bundle) {
Chris@0 237 $configuration = ContentLanguageSettings::loadByEntityTypeBundle($entity_type, $bundle);
Chris@0 238
Chris@0 239 $default_value = NULL;
Chris@0 240 $language_interface = \Drupal::languageManager()->getCurrentLanguage();
Chris@0 241 switch ($configuration->getDefaultLangcode()) {
Chris@0 242 case LanguageInterface::LANGCODE_SITE_DEFAULT:
Chris@0 243 $default_value = \Drupal::languageManager()->getDefaultLanguage()->getId();
Chris@0 244 break;
Chris@0 245
Chris@0 246 case 'current_interface':
Chris@0 247 $default_value = $language_interface->getId();
Chris@0 248 break;
Chris@0 249
Chris@0 250 case 'authors_default':
Chris@0 251 $user = \Drupal::currentUser();
Chris@0 252 $language_code = $user->getPreferredLangcode();
Chris@0 253 if (!empty($language_code)) {
Chris@0 254 $default_value = $language_code;
Chris@0 255 }
Chris@0 256 else {
Chris@0 257 $default_value = $language_interface->getId();
Chris@0 258 }
Chris@0 259 break;
Chris@0 260 }
Chris@0 261 if ($default_value) {
Chris@0 262 return $default_value;
Chris@0 263 }
Chris@0 264
Chris@0 265 // If we still do not have a default value, just return the value stored in
Chris@0 266 // the configuration; it has to be an actual language code.
Chris@0 267 return $configuration->getDefaultLangcode();
Chris@0 268 }
Chris@0 269
Chris@0 270 /**
Chris@0 271 * Reads language prefixes and uses the langcode if no prefix is set.
Chris@0 272 *
Chris@0 273 * @deprecated in Drupal 8.0.x-dev, will be removed before Drupal 9.0.0.
Chris@0 274 * Use \Drupal::config('language.negotiation')->get('url.prefixes') instead.
Chris@14 275 *
Chris@14 276 * @see https://www.drupal.org/node/2912748
Chris@0 277 */
Chris@0 278 function language_negotiation_url_prefixes() {
Chris@0 279 return \Drupal::config('language.negotiation')->get('url.prefixes');
Chris@0 280 }
Chris@0 281
Chris@0 282 /**
Chris@0 283 * Update the list of prefixes from the installed languages.
Chris@0 284 */
Chris@0 285 function language_negotiation_url_prefixes_update() {
Chris@0 286 $config = \Drupal::configFactory()->getEditable('language.negotiation');
Chris@0 287 $prefixes = $config->get('url.prefixes');
Chris@0 288 foreach (\Drupal::languageManager()->getLanguages() as $language) {
Chris@0 289 // The prefix for this language should be updated if it's not assigned yet
Chris@0 290 // or the prefix is set to the empty string.
Chris@0 291 if (empty($prefixes[$language->getId()])) {
Chris@0 292 // For the default language, set the prefix to the empty string,
Chris@0 293 // otherwise use the langcode.
Chris@0 294 $prefixes[$language->getId()] = $language->isDefault() ? '' : $language->getId();
Chris@0 295 }
Chris@0 296 // Otherwise we keep the configured prefix.
Chris@0 297 }
Chris@18 298 $config->set('url.prefixes', $prefixes)->save(TRUE);
Chris@0 299 }
Chris@0 300
Chris@0 301 /**
Chris@0 302 * Reads language domains.
Chris@0 303 *
Chris@0 304 * @deprecated in Drupal 8.0.x-dev, will be removed before Drupal 9.0.0.
Chris@0 305 * Use \Drupal::config('language.negotiation')->get('url.domains') instead.
Chris@14 306 *
Chris@14 307 * @see https://www.drupal.org/node/2912748
Chris@0 308 */
Chris@0 309 function language_negotiation_url_domains() {
Chris@0 310 return \Drupal::config('language.negotiation')->get('url.domains');
Chris@0 311 }
Chris@0 312
Chris@0 313 /**
Chris@0 314 * Implements hook_modules_installed().
Chris@0 315 */
Chris@0 316 function language_modules_installed($modules) {
Chris@0 317 if (!in_array('language', $modules)) {
Chris@0 318 // Since newly (un)installed modules may change the default settings for
Chris@0 319 // non-locked language types (e.g. content language), we need to resave the
Chris@0 320 // language type configuration.
Chris@0 321 /** @var \Drupal\language\LanguageNegotiatorInterface $negotiator */
Chris@0 322 $negotiator = \Drupal::service('language_negotiator');
Chris@0 323 $configurable = \Drupal::config('language.types')->get('configurable');
Chris@0 324 $negotiator->updateConfiguration($configurable);
Chris@0 325 $negotiator->purgeConfiguration();
Chris@0 326 }
Chris@0 327 else {
Chris@0 328 // In language_entity_base_field_info_alter() we are altering view/form
Chris@0 329 // display definitions to make language fields display configurable. Since
Chris@0 330 // this is not a hard dependency, and thus is not detected by the config
Chris@0 331 // system, we have to clean up the related values manually.
Chris@0 332 foreach (['entity_view_display', 'entity_form_display'] as $key) {
Chris@0 333 $displays = \Drupal::entityManager()->getStorage($key)->loadMultiple();
Chris@0 334 /** @var \Drupal\Core\Entity\Display\EntityDisplayInterface $display */
Chris@0 335 foreach ($displays as $display) {
Chris@0 336 $display->save();
Chris@0 337 }
Chris@0 338 }
Chris@0 339 }
Chris@0 340 }
Chris@0 341
Chris@0 342 /**
Chris@0 343 * Implements hook_modules_uninstalled().
Chris@0 344 */
Chris@0 345 function language_modules_uninstalled($modules) {
Chris@0 346 language_modules_installed($modules);
Chris@0 347 }
Chris@0 348
Chris@0 349 /**
Chris@0 350 * Implements hook_preprocess_HOOK() for block templates.
Chris@0 351 */
Chris@0 352 function language_preprocess_block(&$variables) {
Chris@0 353 if ($variables['configuration']['provider'] == 'language') {
Chris@0 354 $variables['attributes']['role'] = 'navigation';
Chris@0 355 }
Chris@0 356 }
Chris@0 357
Chris@0 358 /**
Chris@0 359 * Returns language mappings between browser and Drupal language codes.
Chris@0 360 *
Chris@0 361 * @return array
Chris@0 362 * An array containing browser language codes as keys with corresponding
Chris@0 363 * Drupal language codes as values.
Chris@0 364 */
Chris@0 365 function language_get_browser_drupal_langcode_mappings() {
Chris@0 366 $config = \Drupal::config('language.mappings');
Chris@0 367 if ($config->isNew()) {
Chris@0 368 return [];
Chris@0 369 }
Chris@0 370 return $config->get('map');
Chris@0 371 }
Chris@0 372
Chris@0 373 /**
Chris@0 374 * Implements hook_form_alter().
Chris@0 375 */
Chris@0 376 function language_form_alter(&$form, FormStateInterface $form_state) {
Chris@0 377 // Content entity forms may have added a langcode field. But content language
Chris@0 378 // configuration should decide if it should be exposed or not in the forms.
Chris@0 379 $form_object = $form_state->getFormObject();
Chris@0 380 if ($form_object instanceof ContentEntityFormInterface && $form_object->getEntity()->getEntityType()->hasKey('langcode')) {
Chris@0 381 /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
Chris@0 382 $entity = $form_object->getEntity();
Chris@0 383 $entity_type = $entity->getEntityType();
Chris@0 384 $langcode_key = $entity_type->getKey('langcode');
Chris@17 385 if (isset($form[$langcode_key]) && $form[$langcode_key]['#access'] !== FALSE) {
Chris@0 386 $language_configuration = ContentLanguageSettings::loadByEntityTypeBundle($entity->getEntityTypeId(), $entity->bundle());
Chris@0 387 $form[$langcode_key]['#access'] = $language_configuration->isLanguageAlterable();
Chris@0 388 }
Chris@0 389 }
Chris@0 390 }
Chris@0 391
Chris@0 392 /**
Chris@0 393 * Implements hook_field_info_alter().
Chris@0 394 */
Chris@0 395 function language_field_info_alter(&$info) {
Chris@0 396 // Change the default behavior of language field.
Chris@0 397 $info['language']['class'] = '\Drupal\language\DefaultLanguageItem';
Chris@0 398 }
Chris@0 399
Chris@0 400 /**
Chris@0 401 * Implements hook_entity_field_access()
Chris@0 402 */
Chris@0 403 function language_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
Chris@0 404 // Only allow edit access on a langcode field if the entity it is attached to
Chris@0 405 // is configured to have an alterable language. Also without items we can not
Chris@0 406 // decide whether or not to allow access.
Chris@0 407 if ($items && $operation == 'edit') {
Chris@0 408 // Check if we are dealing with a langcode field.
Chris@0 409 $langcode_key = $items->getEntity()->getEntityType()->getKey('langcode');
Chris@0 410 if ($field_definition->getName() == $langcode_key) {
Chris@0 411 // Grant access depending on whether the entity language can be altered.
Chris@0 412 $entity = $items->getEntity();
Chris@0 413 $config = ContentLanguageSettings::loadByEntityTypeBundle($entity->getEntityTypeId(), $entity->bundle());
Chris@0 414 return AccessResult::forbiddenIf(!$config->isLanguageAlterable());
Chris@0 415 }
Chris@0 416 }
Chris@0 417 return AccessResult::neutral();
Chris@0 418 }
Chris@0 419
Chris@0 420 /**
Chris@0 421 * Implements hook_tour_tips_alter().
Chris@0 422 */
Chris@0 423 function language_tour_tips_alter(array &$tour_tips, EntityInterface $entity) {
Chris@0 424 foreach ($tour_tips as $tour_tip) {
Chris@0 425 if ($tour_tip->get('id') == 'language-overview') {
Chris@0 426 $additional_overview = '';
Chris@0 427 if (Drupal::service('module_handler')->moduleExists('locale')) {
Chris@0 428 $additional_overview = t("This page also provides an overview of how much of the site's interface has been translated for each configured language.");
Chris@0 429 }
Chris@0 430 else {
Chris@0 431 $additional_overview = t("If the Interface Translation module is enabled, this page will provide an overview of how much of the site's interface has been translated for each configured language.");
Chris@0 432 }
Chris@0 433 $tour_tip->set('body', $tour_tip->get('body') . '<p>' . $additional_overview . '</p>');
Chris@0 434 }
Chris@0 435 elseif ($tour_tip->get('id') == 'language-continue') {
Chris@0 436 $additional_continue = '';
Chris@0 437 $additional_modules = [];
Chris@0 438 if (!Drupal::service('module_handler')->moduleExists('locale')) {
Chris@0 439 $additional_modules[] = Drupal::service('module_handler')->getName('locale');
Chris@0 440 }
Chris@0 441 if (!Drupal::service('module_handler')->moduleExists('content_translation')) {
Chris@0 442 $additional_modules[] = Drupal::service('module_handler')->getName('content_translation');
Chris@0 443 }
Chris@0 444 if (!empty($additional_modules)) {
Chris@0 445 $additional_continue = t('Depending on your site features, additional modules that you might want to enable are:') . '<ul>';
Chris@0 446 foreach ($additional_modules as $additional_module) {
Chris@0 447 $additional_continue .= '<li>' . $additional_module . '</li>';
Chris@0 448 }
Chris@0 449 $additional_continue .= '</ul>';
Chris@0 450 }
Chris@0 451 if (!empty($additional_continue)) {
Chris@0 452 $tour_tip->set('body', $tour_tip->get('body') . '<p>' . $additional_continue . '</p>');
Chris@0 453 }
Chris@0 454 }
Chris@0 455 }
Chris@0 456 }
Chris@0 457
Chris@0 458 /**
Chris@0 459 * Implements hook_language_types_info_alter().
Chris@0 460 *
Chris@0 461 * We can't set the fixed properties in \Drupal\Core\Language\LanguageManager,
Chris@0 462 * where the rest of the properties for the default language types are defined.
Chris@0 463 * The LanguageNegation classes are only loaded when the language module is
Chris@0 464 * enabled and we can't be sure of that in the LanguageManager.
Chris@0 465 */
Chris@0 466 function language_language_types_info_alter(array &$language_types) {
Chris@0 467 $language_types[LanguageInterface::TYPE_CONTENT]['fixed'] = [LanguageNegotiationUI::METHOD_ID];
Chris@0 468 $language_types[LanguageInterface::TYPE_URL]['fixed'] = [LanguageNegotiationUrl::METHOD_ID, LanguageNegotiationUrlFallback::METHOD_ID];
Chris@0 469 }