diff --git a/core/modules/language/language.admin.inc b/core/modules/language/language.admin.inc index 3ebaab1..b1d7711 100644 --- a/core/modules/language/language.admin.inc +++ b/core/modules/language/language.admin.inc @@ -25,265 +25,6 @@ function language_admin_predefined_list() { } /** - * Builds the configuration form for language negotiation. - */ -function language_negotiation_configure_form() { - language_negotiation_include(); - - $form = array( - '#submit' => array('language_negotiation_configure_form_submit'), - '#theme' => 'language_negotiation_configure_form', - '#language_types_info' => language_types_info(), - '#language_negotiation_info' => language_negotiation_info(), - ); - $form['#language_types'] = array(); - $configurable = Drupal::config('system.language.types')->get('configurable'); - foreach ($form['#language_types_info'] as $type => $info) { - // Show locked language types only if they are configurable. - if (empty($info['locked']) || in_array($type, $configurable)) { - $form['#language_types'][] = $type; - } - } - foreach ($form['#language_types'] as $type) { - language_negotiation_configure_form_table($form, $type); - } - - $form['actions'] = array('#type' => 'actions'); - $form['actions']['submit'] = array( - '#type' => 'submit', - '#value' => t('Save settings'), - ); - - return $form; -} - -/** - * Builds a language negotiation method configuration table. - */ -function language_negotiation_configure_form_table(&$form, $type) { - $info = $form['#language_types_info'][$type]; - - $table_form = array( - '#title' => t('@type language detection', array('@type' => $info['name'])), - '#tree' => TRUE, - '#description' => $info['description'], - '#language_negotiation_info' => array(), - '#show_operations' => FALSE, - 'weight' => array('#tree' => TRUE), - ); - // Only show configurability checkbox for the unlocked language types. - if (empty($info['locked'])) { - $configurable = Drupal::config('system.language.types')->get('configurable'); - $table_form['configurable'] = array( - '#type' => 'checkbox', - '#title' => t('Customize %language_name language detection to differ from User interface text language detection settings.', array('%language_name' => $info['name'])), - '#default_value' => in_array($type, $configurable), - '#attributes' => array('class' => array('language-customization-checkbox')), - '#attached' => array( - 'library' => array( - array('language', 'language.admin') - ), - ), - ); - } - - $negotiation_info = $form['#language_negotiation_info']; - $enabled_methods = variable_get("language_negotiation_$type", array()); - $methods_weight = variable_get("language_negotiation_methods_weight_$type", array()); - - // Add missing data to the methods lists. - foreach ($negotiation_info as $method_id => $method) { - if (!isset($methods_weight[$method_id])) { - $methods_weight[$method_id] = isset($method['weight']) ? $method['weight'] : 0; - } - } - - // Order methods list by weight. - asort($methods_weight); - - foreach ($methods_weight as $method_id => $weight) { - // A language method might be no more available if the defining module has - // been disabled after the last configuration saving. - if (!isset($negotiation_info[$method_id])) { - continue; - } - - $enabled = isset($enabled_methods[$method_id]); - $method = $negotiation_info[$method_id]; - - // List the method only if the current type is defined in its 'types' key. - // If it is not defined default to all the configurable language types. - $types = array_flip(isset($method['types']) ? $method['types'] : $form['#language_types']); - - if (isset($types[$type])) { - $table_form['#language_negotiation_info'][$method_id] = $method; - $method_name = check_plain($method['name']); - - $table_form['weight'][$method_id] = array( - '#type' => 'weight', - '#title' => t('Weight for !title language detection method', array('!title' => drupal_strtolower($method_name))), - '#title_display' => 'invisible', - '#default_value' => $weight, - '#attributes' => array('class' => array("language-method-weight-$type")), - '#delta' => 20, - ); - - $table_form['title'][$method_id] = array('#markup' => $method_name); - - $table_form['enabled'][$method_id] = array( - '#type' => 'checkbox', - '#title' => t('Enable !title language detection method', array('!title' => drupal_strtolower($method_name))), - '#title_display' => 'invisible', - '#default_value' => $enabled, - ); - if ($method_id === LANGUAGE_NEGOTIATION_SELECTED) { - $table_form['enabled'][$method_id]['#default_value'] = TRUE; - $table_form['enabled'][$method_id]['#attributes'] = array('disabled' => 'disabled'); - } - - $table_form['description'][$method_id] = array('#markup' => filter_xss_admin($method['description'])); - - $config_op = array(); - if (isset($method['config'])) { - $config_op['configure'] = array( - 'title' => t('Configure'), - 'href' => $method['config'], - ); - // If there is at least one operation enabled show the operation column. - $table_form['#show_operations'] = TRUE; - } - $table_form['operation'][$method_id] = array( - '#type' => 'operations', - '#links' => $config_op, - ); - } - } - - $form[$type] = $table_form; -} - -/** - * Returns HTML for the language negotiation configuration form. - * - * @param $variables - * An associative array containing: - * - form: A render element representing the form. - * - * @ingroup themeable - */ -function theme_language_negotiation_configure_form($variables) { - $form = $variables['form']; - $output = ''; - - foreach ($form['#language_types'] as $type) { - $rows = array(); - $title = '

' . $form[$type]['#title'] . '

'; - $description = '
' . $form[$type]['#description'] . '
'; - - foreach ($form[$type]['title'] as $id => $element) { - // Do not take form control structures. - if (is_array($element) && element_child($id)) { - $row = array( - 'data' => array( - '' . drupal_render($form[$type]['title'][$id]) . '', - drupal_render($form[$type]['description'][$id]), - drupal_render($form[$type]['enabled'][$id]), - drupal_render($form[$type]['weight'][$id]), - ), - 'class' => array('draggable'), - ); - if ($form[$type]['#show_operations']) { - $row['data'][] = drupal_render($form[$type]['operation'][$id]); - } - $rows[] = $row; - } - } - - $header = array( - array('data' => t('Detection method')), - array('data' => t('Description')), - array('data' => t('Enabled')), - array('data' => t('Weight')), - ); - - // If there is at least one operation enabled show the operation column. - if ($form[$type]['#show_operations']) { - $header[] = array('data' => t('Operations')); - } - - $build = array( - '#theme' => 'table', - '#header' => $header, - '#rows' => $rows, - '#attributes' => array('id' => "language-negotiation-methods-$type"), - ); - $table = drupal_render($form[$type]['configurable']); - $table .= drupal_render($build); - $table .= drupal_render_children($form[$type]); - - drupal_add_tabledrag("language-negotiation-methods-$type", 'order', 'sibling', "language-method-weight-$type"); - - $output .= '
' . $title . $description . $table . '
'; - } - - $output .= drupal_render_children($form); - return $output; -} - -/** - * Submit handler for language negotiation settings. - */ -function language_negotiation_configure_form_submit($form, &$form_state) { - $configurable_types = $form['#language_types']; - - $stored_values = Drupal::config('system.language.types')->get('configurable'); - $customized = array(); - $method_weights_type = array(); - - foreach ($configurable_types as $type) { - $customized[$type] = in_array($type, $stored_values); - $method_weights = array(); - $enabled_methods = $form_state['values'][$type]['enabled']; - $enabled_methods[LANGUAGE_NEGOTIATION_SELECTED] = TRUE; - $method_weights_input = $form_state['values'][$type]['weight']; - if (isset($form_state['values'][$type]['configurable'])) { - $customized[$type] = !empty($form_state['values'][$type]['configurable']); - } - - foreach ($method_weights_input as $method_id => $weight) { - if ($enabled_methods[$method_id]) { - $method_weights[$method_id] = $weight; - } - } - - $method_weights_type[$type] = $method_weights; - variable_set("language_negotiation_methods_weight_$type", $method_weights_input); - } - - // Update non-configurable language types and the related language negotiation - // configuration. - language_types_set(array_keys(array_filter($customized))); - - // Update the language negotiations after setting the configurability. - foreach ($method_weights_type as $type => $method_weights) { - language_negotiation_set($type, $method_weights); - } - - // Clear block definitions cache since the available blocks and their names - // may have been changed based on the configurable types. - if (module_exists('block')) { - // If there is an active language switcher for a language type that has been - // made not configurable, deactivate it first. - $non_configurable = array_keys(array_diff($customized, array_filter($customized))); - _language_disable_language_switcher($non_configurable); - Drupal::service('plugin.manager.block')->clearCachedDefinitions(); - } - - $form_state['redirect'] = 'admin/config/regional/language/detection'; - drupal_set_message(t('Language negotiation configuration saved.')); -} - -/** * Theme browser configuration form as table. * * @param $variables @@ -503,21 +244,3 @@ function language_content_settings_form_submit(array $form, array &$form_state) } drupal_set_message(t('Settings successfully updated.')); } - -/** - * Helper function to disable the language switcher blocks. - * - * @param array $language_types - * Array containing all language types whose language switchers need to be - * disabled. - */ -function _language_disable_language_switcher(array $language_types) { - $blocks = _block_rehash(); - foreach ($language_types as $language_type) { - foreach ($blocks as $block) { - if (strpos($block->id, 'language_switcher_' . substr($language_type, 9)) !== FALSE) { - $block->delete(); - } - } - } -} diff --git a/core/modules/language/language.module b/core/modules/language/language.module index 83f61e9..8069623 100644 --- a/core/modules/language/language.module +++ b/core/modules/language/language.module @@ -92,11 +92,8 @@ function language_menu() { // Language negotiation. $items['admin/config/regional/language/detection'] = array( 'title' => 'Detection and selection', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('language_negotiation_configure_form'), - 'access arguments' => array('administer languages'), + 'route_name' => 'language_negotiation_configure_form', 'weight' => 10, - 'file' => 'language.admin.inc', 'type' => MENU_LOCAL_TASK, ); $items['admin/config/regional/language/detection/url'] = array( @@ -857,3 +854,71 @@ function language_system_regional_settings_form_submit($form, &$form_state) { $language->default = TRUE; language_save($language); } + +/** + * Returns HTML for the language negotiation configuration form. + * + * @param $variables + * An associative array containing: + * - form: A render element representing the form. + * + * @ingroup themeable + */ +function theme_language_negotiation_configure_form($variables) { + $form = $variables['form']; + $output = ''; + + foreach ($form['#language_types'] as $type) { + $rows = array(); + $title = '

' . $form[$type]['#title'] . '

'; + $description = '
' . $form[$type]['#description'] . '
'; + + foreach ($form[$type]['title'] as $id => $element) { + // Do not take form control structures. + if (is_array($element) && element_child($id)) { + $row = array( + 'data' => array( + '' . drupal_render($form[$type]['title'][$id]) . '', + drupal_render($form[$type]['description'][$id]), + drupal_render($form[$type]['enabled'][$id]), + drupal_render($form[$type]['weight'][$id]), + ), + 'class' => array('draggable'), + ); + if ($form[$type]['#show_operations']) { + $row['data'][] = drupal_render($form[$type]['operation'][$id]); + } + $rows[] = $row; + } + } + + $header = array( + array('data' => t('Detection method')), + array('data' => t('Description')), + array('data' => t('Enabled')), + array('data' => t('Weight')), + ); + + // If there is at least one operation enabled show the operation column. + if ($form[$type]['#show_operations']) { + $header[] = array('data' => t('Operations')); + } + + $build = array( + '#theme' => 'table', + '#header' => $header, + '#rows' => $rows, + '#attributes' => array('id' => "language-negotiation-methods-$type"), + ); + $table = drupal_render($form[$type]['configurable']); + $table .= drupal_render($build); + $table .= drupal_render_children($form[$type]); + + drupal_add_tabledrag("language-negotiation-methods-$type", 'order', 'sibling', "language-method-weight-$type"); + + $output .= '
' . $title . $description . $table . '
'; + } + + $output .= drupal_render_children($form); + return $output; +} diff --git a/core/modules/language/language.routing.yml b/core/modules/language/language.routing.yml index c7e8f12..0b8c042 100644 --- a/core/modules/language/language.routing.yml +++ b/core/modules/language/language.routing.yml @@ -56,8 +56,15 @@ language_negotiation_browser: _permission: 'administer languages' language_negotiation_browser_delete: - pattern: 'admin/config/regional/language/detection/browser/delete/{browser_langcode}' + pattern: '/admin/config/regional/language/detection/browser/delete/{browser_langcode}' defaults: _form: '\Drupal\language\Form\NegotiationBrowserDeleteForm' requirements: _permission: 'administer languages' + +language_negotiation_configure_form: + pattern: '/admin/config/regional/language/detection' + defaults: + _form: 'Drupal\language\Form\NegotiationConfigureForm' + requirements: + _permission: 'administer languages' diff --git a/core/modules/language/lib/Drupal/language/Form/NegotiationConfigureForm.php b/core/modules/language/lib/Drupal/language/Form/NegotiationConfigureForm.php new file mode 100644 index 0000000..e8bb023 --- /dev/null +++ b/core/modules/language/lib/Drupal/language/Form/NegotiationConfigureForm.php @@ -0,0 +1,296 @@ +languageTypesConfig = $config_factory->get('system.language.types'); + $this->blockManager = $block_manager; + $this->moduleHandler = $module_handler; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('config.factory'), + $container->get('plugin.manager.block'), + $container->get('module_handler') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormID() { + return 'language_negotiation_configure_form'; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, array &$form_state) { + language_negotiation_include(); + + $configurable = $this->languageTypesConfig->get('configurable'); + + $form = array( + '#theme' => 'language_negotiation_configure_form', + '#language_types_info' => language_types_info(), + '#language_negotiation_info' => language_negotiation_info(), + ); + $form['#language_types'] = array(); + + foreach ($form['#language_types_info'] as $type => $info) { + // Show locked language types only if they are configurable. + if (empty($info['locked']) || in_array($type, $configurable)) { + $form['#language_types'][] = $type; + } + } + + foreach ($form['#language_types'] as $type) { + $this->configureFormTable($form, $type); + } + + $form['actions'] = array('#type' => 'actions'); + $form['actions']['submit'] = array( + '#type' => 'submit', + '#value' => $this->t('Save settings'), + ); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + language_negotiation_include(); + $configurable_types = $form['#language_types']; + + $stored_values = $this->languageTypesConfig->get('configurable'); + $customized = array(); + $method_weights_type = array(); + + foreach ($configurable_types as $type) { + $customized[$type] = in_array($type, $stored_values); + $method_weights = array(); + $enabled_methods = $form_state['values'][$type]['enabled']; + $enabled_methods[LANGUAGE_NEGOTIATION_SELECTED] = TRUE; + $method_weights_input = $form_state['values'][$type]['weight']; + if (isset($form_state['values'][$type]['configurable'])) { + $customized[$type] = !empty($form_state['values'][$type]['configurable']); + } + + foreach ($method_weights_input as $method_id => $weight) { + if ($enabled_methods[$method_id]) { + $method_weights[$method_id] = $weight; + } + } + + $method_weights_type[$type] = $method_weights; + variable_set("language_negotiation_methods_weight_$type", $method_weights_input); + } + + // Update non-configurable language types and the related language + // negotiation configuration. + language_types_set(array_keys(array_filter($customized))); + + // Update the language negotiations after setting the configurability. + foreach ($method_weights_type as $type => $method_weights) { + language_negotiation_set($type, $method_weights); + } + + // Clear block definitions cache since the available blocks and their names + // may have been changed based on the configurable types. + if ($this->moduleHandler->moduleExists('block')) { + // If there is an active language switcher for a language type that has + // been made not configurable, deactivate it first. + $non_configurable = array_keys(array_diff($customized, array_filter($customized))); + $this->disableLanguageSwitcher($non_configurable); + $this->blockManager->clearCachedDefinitions(); + } + + $form_state['redirect'] = 'admin/config/regional/language/detection'; + drupal_set_message($this->t('Language negotiation configuration saved.')); + } + + /** + * Builds a language negotiation method configuration table. + * + * @param array $form + * The language negotiation configuration form. + * @param string $type + * The language type to generate the table for. + */ + protected function configureFormTable(array &$form, $type) { + $info = $form['#language_types_info'][$type]; + + $table_form = array( + '#title' => $this->t('@type language detection', array('@type' => $info['name'])), + '#tree' => TRUE, + '#description' => $info['description'], + '#language_negotiation_info' => array(), + '#show_operations' => FALSE, + 'weight' => array('#tree' => TRUE), + ); + // Only show configurability checkbox for the unlocked language types. + if (empty($info['locked'])) { + $configurable = $this->languageTypesConfig->get('configurable'); + $table_form['configurable'] = array( + '#type' => 'checkbox', + '#title' => $this->t('Customize %language_name language detection to differ from User interface text language detection settings.', array('%language_name' => $info['name'])), + '#default_value' => in_array($type, $configurable), + '#attributes' => array('class' => array('language-customization-checkbox')), + '#attached' => array( + 'library' => array( + array('language', 'language.admin') + ), + ), + ); + } + + $negotiation_info = $form['#language_negotiation_info']; + $enabled_methods = variable_get("language_negotiation_$type", array()); + $methods_weight = variable_get("language_negotiation_methods_weight_$type", array()); + + // Add missing data to the methods lists. + foreach ($negotiation_info as $method_id => $method) { + if (!isset($methods_weight[$method_id])) { + $methods_weight[$method_id] = isset($method['weight']) ? $method['weight'] : 0; + } + } + + // Order methods list by weight. + asort($methods_weight); + + foreach ($methods_weight as $method_id => $weight) { + // A language method might be no more available if the defining module has + // been disabled after the last configuration saving. + if (!isset($negotiation_info[$method_id])) { + continue; + } + + $enabled = isset($enabled_methods[$method_id]); + $method = $negotiation_info[$method_id]; + + // List the method only if the current type is defined in its 'types' key. + // If it is not defined default to all the configurable language types. + $types = array_flip(isset($method['types']) ? $method['types'] : $form['#language_types']); + + if (isset($types[$type])) { + $table_form['#language_negotiation_info'][$method_id] = $method; + $method_name = String::checkPlain($method['name']); + + $table_form['weight'][$method_id] = array( + '#type' => 'weight', + '#title' => $this->t('Weight for !title language detection method', array('!title' => Unicode::strtolower($method_name))), + '#title_display' => 'invisible', + '#default_value' => $weight, + '#attributes' => array('class' => array("language-method-weight-$type")), + '#delta' => 20, + ); + + $table_form['title'][$method_id] = array('#markup' => $method_name); + + $table_form['enabled'][$method_id] = array( + '#type' => 'checkbox', + '#title' => $this->t('Enable !title language detection method', array('!title' => Unicode::strtolower($method_name))), + '#title_display' => 'invisible', + '#default_value' => $enabled, + ); + if ($method_id === LANGUAGE_NEGOTIATION_SELECTED) { + $table_form['enabled'][$method_id]['#default_value'] = TRUE; + $table_form['enabled'][$method_id]['#attributes'] = array('disabled' => 'disabled'); + } + + $table_form['description'][$method_id] = array('#markup' => Xss::filterAdmin($method['description'])); + + $config_op = array(); + if (isset($method['config'])) { + $config_op['configure'] = array( + 'title' => $this->t('Configure'), + 'href' => $method['config'], + ); + // If there is at least one operation enabled show the operation + // column. + $table_form['#show_operations'] = TRUE; + } + $table_form['operation'][$method_id] = array( + '#type' => 'operations', + '#links' => $config_op, + ); + } + } + $form[$type] = $table_form; + } + + /** + * Disables the language switcher blocks. + * + * @param array $language_types + * An array containing all language types whose language switchers need to + * be disabled. + */ + protected function disableLanguageSwitcher(array $language_types) { + $blocks = _block_rehash(); + foreach ($language_types as $language_type) { + foreach ($blocks as $block) { + if (strpos($block->id, 'language_switcher_' . substr($language_type, 9)) !== FALSE) { + $block->delete(); + } + } + } + } + +}