language.module 27 KB

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