diff --git a/core/modules/language/config/language.negotiation.yml b/core/modules/language/config/language.negotiation.yml index 8ebea15..e5dcccf 100644 --- a/core/modules/language/config/language.negotiation.yml +++ b/core/modules/language/config/language.negotiation.yml @@ -6,3 +6,5 @@ url: en: '' domains: en: '' +customized_language_negotiation: + language_interface: true diff --git a/core/modules/language/language.admin.inc b/core/modules/language/language.admin.inc index bdba951..2b3fda3 100644 --- a/core/modules/language/language.admin.inc +++ b/core/modules/language/language.admin.inc @@ -389,6 +389,18 @@ function language_negotiation_configure_form_table(&$form, $type) { '#show_operations' => FALSE, 'weight' => array('#tree' => TRUE), ); + if ($type != LANGUAGE_TYPE_INTERFACE) { + $customizations = config('language.negotiation')->get('customized_language_negotiation'); + $table_form['customized'] = 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' => empty($customizations[$type]) ? FALSE : $customizations[$type], + '#attributes' => array('class' => array('language-customization-checkbox')), + '#attached' => array( + 'js' => array(drupal_get_path('module', 'language') . '/language.admin.js' => array('type' => 'file')), + ), + ); + } $negotiation_info = $form['#language_negotiation_info']; $enabled_methods = variable_get("language_negotiation_$type", array()); @@ -519,12 +531,13 @@ function theme_language_negotiation_configure_form($variables) { 'rows' => $rows, 'attributes' => array('id' => "language-negotiation-methods-$type"), ); - $table = theme('table', $variables); + $table = drupal_render($form[$type]['customized']); + $table .= theme('table', $variables); $table .= drupal_render_children($form[$type]); drupal_add_tabledrag("language-negotiation-methods-$type", 'order', 'sibling', "language-method-weight-$type"); - $output .= '
' . $title . $description . $table . '
'; + $output .= '
' . $title . $description . $table . '
'; } $output .= drupal_render_children($form); @@ -537,11 +550,16 @@ function theme_language_negotiation_configure_form($variables) { function language_negotiation_configure_form_submit($form, &$form_state) { $configurable_types = $form['#language_types']; + $customizations = config('language.negotiation')->get('customized_language_negotiation'); + foreach ($configurable_types as $type) { $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 ($type != LANGUAGE_TYPE_INTERFACE) { + $customizations[$type] = !empty($form_state['values'][$type]['customized']); + } foreach ($method_weights_input as $method_id => $weight) { if ($enabled_methods[$method_id]) { @@ -552,6 +570,12 @@ function language_negotiation_configure_form_submit($form, &$form_state) { language_negotiation_set($type, $method_weights); variable_set("language_negotiation_methods_weight_$type", $method_weights_input); } + // Always set the default language type LANGUAGE_TYPE_INTERFACE + $customizations[LANGUAGE_TYPE_INTERFACE] = TRUE; + config('language.negotiation') + ->set('customized_language_negotiation', $customizations) + ->save(); + // Update non-configurable language types and the related language negotiation // configuration. diff --git a/core/modules/language/language.admin.js b/core/modules/language/language.admin.js new file mode 100644 index 0000000..9acb57f --- /dev/null +++ b/core/modules/language/language.admin.js @@ -0,0 +1,48 @@ +(function ($) { + +"use strict"; + +/** + * Makes language negotiation inherit user interface negotiation. + */ +Drupal.behaviors.negotiationLanguage = { + attach: function (context) { + var $context = $(context); + // Given a customization checkbox + var toggleTable = function ($checkbox) { + // Derive the language type being changed. + var langtype = $checkbox.attr('name').replace('[customized]', ''); + var $table = $('.table-' + langtype + '-wrapper'); + var $iface_weight = $(':input[name="' + langtype + '[weight][language-interface]"]', $table); + // If this is the first time save the original language-interface weight for toggling. + if (typeof($iface_weight.data('initial-value')) === 'undefined') { + $iface_weight.data('initial-value', $iface_weight.val()); + }; + + $(':input[name^="' + langtype + '[weight]"]', $table).each(function (index, element) { $(element).val(index - 19); }); + if ($checkbox.is(':checked')) { + $iface_weight.val($iface_weight.data('initial-value')); + $table.find('table, .tabledrag-toggle-weight-wrapper').show(); + } + else { + // Make sure language-interface gets selected. + $(':input[name="' + langtype + '[enabled][language-interface]"]').prop('checked', true); + // Make sure the language-interface is at the top of the list. + // Get the min weight to set language-interface below that. + var weights = $(':input[name^="' + langtype + '[weight]"]', $table).map(function (index, element) { return parseInt($(element).val()); }).get(); + $iface_weight.val(Math.min.apply(null, weights) - 1); + $table.find('table, .tabledrag-toggle-weight-wrapper').hide(); + } + }; + // Bind hide/show + rearrange to customization checkboxes. + $('body').once('negotiation-language-admin-bind').on('click', '#language-negotiation-configure-form :input.language-customization-checkbox', function (e) { + toggleTable($(e.target)); + }); + // Initial hide not customized language types. + $('#language-negotiation-configure-form :input.language-customization-checkbox:not(:checked)').each(function (index, element) { + toggleTable($(element)); + }); + } +}; + +})(jQuery);