diff --git a/domain_lang/domain_lang.admin.inc b/domain_lang/domain_lang.admin.inc index e69de29..b056031 100644 --- a/domain_lang/domain_lang.admin.inc +++ b/domain_lang/domain_lang.admin.inc @@ -0,0 +1,57 @@ + 'value', + '#value' => $domain['domain_id'], + ); + + return $form; +} + +/** + * Domain language detection and selection form submit callback. + */ +function domain_lang_detection_selection_form_submit($form, &$form_state) { + $domain_id = (int) $form_state['values']['domain_id']; + $configurable_types = $form['#language_types']; + + foreach ($configurable_types as $type) { + $negotiation = array(); + $enabled_providers = $form_state['values'][$type]['enabled']; + $enabled_providers[LANGUAGE_NEGOTIATION_DEFAULT] = TRUE; + $providers_weight = $form_state['values'][$type]['weight']; + + foreach ($providers_weight as $id => $weight) { + if ($enabled_providers[$id]) { + $provider = $form[$type]['#language_providers'][$id]; + $provider['weight'] = $weight; + $negotiation[$id] = $provider; + } + } + + domain_lang_negotiation_set($domain_id, $type, $negotiation); + domain_conf_variable_set($domain_id, "locale_language_providers_weight_$type", $providers_weight); + } + + // Update non-configurable language types and the related language negotiation + // configuration. + domain_lang_types_set($domain_id); + + drupal_set_message(t('Language negotiation configuration saved.')); +} diff --git a/domain_lang/domain_lang.info b/domain_lang/domain_lang.info index e69de29..7714d1a 100644 --- a/domain_lang/domain_lang.info +++ b/domain_lang/domain_lang.info @@ -0,0 +1,7 @@ +name = Domain Language +description = Adds language detection and selection functionality per domain basis. +package = Domain Access +core = 7.x + +dependencies[] = locale +dependencies[] = domain_conf diff --git a/domain_lang/domain_lang.install b/domain_lang/domain_lang.install index e69de29..63a3eaa 100644 --- a/domain_lang/domain_lang.install +++ b/domain_lang/domain_lang.install @@ -0,0 +1,12 @@ +expression('weight', 'weight + :weight', array(':weight' => 100)) + ->condition('name', 'domain_lang') + ->condition('type', 'module') + ->execute(); +} diff --git a/domain_lang/domain_lang.module b/domain_lang/domain_lang.module index e69de29..235e0b3 100644 --- a/domain_lang/domain_lang.module +++ b/domain_lang/domain_lang.module @@ -0,0 +1,169 @@ + array( + 'title' => 'Language Detect & Select', + 'type' => MENU_LOCAL_TASK, + 'page callback' => 'drupal_get_form', + 'page arguments' => array('domain_lang_detection_selection_form', 4), + 'access arguments' => array('administer domains'), + 'file' => 'domain_lang.admin.inc', + ), + ); +} + +/** + * Implements hook_form_FORM_ID_alter(). + */ +function domain_lang_form_locale_language_providers_session_form_alter(&$form, &$form_state, $form_id) { + _domain_lang_form_locale_language_providers_alter($form, $form_state); +} + +/** + * Implements hook_form_FORM_ID_alter(). + */ +function domain_lang_form_locale_language_providers_url_form_alter(&$form, &$form_state, $form_id) { + _domain_lang_form_locale_language_providers_alter($form, $form_state); +} + +/** + * Alter language provider form callback. + */ +function _domain_lang_form_locale_language_providers_alter(&$form, &$form_state) { + $domain = domain_get_domain(); + + if ($domain['is_default'] || !isset($form['domain_settings']['domain_id'])) { + return FALSE; + } + + $form['domain_settings']['#collapsed'] = TRUE; + $form['domain_settings']['#disabled'] = TRUE; + $form['domain_settings']['domain_id']['#default_value'] = $domain['domain_id']; + $form['#submit'][] = 'domain_lang_form_locale_language_providers_submit'; + $form['domain_id'] = array( + '#type' => 'value', + '#value' => $domain['domain_id'], + ); +} + +/** + * Language provider form submit callback. Setup redirect to proper page. + */ +function domain_lang_form_locale_language_providers_submit($form, &$form_state) { + $form_state['redirect'] = 'admin/structure/domain/view/' . $form_state['values']['domain_id'] . '/language-detect-select'; +} + +/** + * Implements hook_form_FORM_ID_alter(). + */ +function domain_lang_form_locale_languages_configure_form_alter(&$form, &$form_state, $form_id) { + $domain = domain_get_domain(); + + if ($domain['is_default']) { + return TRUE; + } + if (current_path() !== 'admin/config/regional/language/configure') { + return TRUE; + } + + drupal_set_message(t( + 'NOT USE this page. Open Domain language detection and selection page for setup.', + array( + '@url' => url('admin/structure/domain/view/' . $domain['domain_id'] . '/language-detect-select') + )), 'error'); +} + +/** + * Saves a list of language negotiation providers for requested domain. + * + * @param $domain_id + * The unique domain ID that is being edited. + * @param $type + * The language negotiation type. + * @param $language_providers + * An array of language negotiation provider weights keyed by provider ID. + * @see language_provider_weight() + */ +function domain_lang_negotiation_set($domain_id, $type, $language_providers) { + // Save only the necessary fields. + $provider_fields = array('callbacks', 'file', 'cache'); + + $negotiation = array(); + $providers_weight = array(); + $defined_providers = language_negotiation_info(); + $default_types = language_types_configurable(FALSE); + + // Initialize the providers weight list. + foreach ($language_providers as $id => $provider) { + $providers_weight[$id] = language_provider_weight($provider); + } + + // Order providers list by weight. + asort($providers_weight); + + foreach ($providers_weight as $id => $weight) { + if (isset($defined_providers[$id])) { + $provider = $defined_providers[$id]; + // If the provider does not express any preference about types, make it + // available for any configurable type. + $types = array_flip(isset($provider['types']) ? $provider['types'] : $default_types); + // Check whether the provider is defined and has the right type. + if (isset($types[$type])) { + $provider_data = array(); + foreach ($provider_fields as $field) { + if (isset($provider[$field])) { + $provider_data[$field] = $provider[$field]; + } + } + $negotiation[$id] = $provider_data; + } + } + } + + domain_conf_variable_set($domain_id, "language_negotiation_$type", $negotiation); +} + +/** + * Updates the language type configuration for requested domain. + * + * @param $domain_id + * The unique domain ID that is being edited. + */ +function domain_lang_types_set($domain_id) { + // Ensure that we are getting the defined language negotiation information. An + // invocation of module_enable() or module_disable() could outdate the cached + // information. + drupal_static_reset('language_types_info'); + drupal_static_reset('language_negotiation_info'); + + // Determine which language types are configurable and which not by checking + // whether the 'fixed' key is defined. Non-configurable (fixed) language types + // have their language negotiation settings stored there. + $defined_providers = language_negotiation_info(); + foreach (language_types_info() as $type => $info) { + if (isset($info['fixed'])) { + $language_types[$type] = FALSE; + $negotiation = array(); + foreach ($info['fixed'] as $weight => $id) { + if (isset($defined_providers[$id])) { + $negotiation[$id] = $weight; + } + } + domain_lang_negotiation_set($domain_id, $type, $negotiation); + } + else { + $language_types[$type] = TRUE; + } + } + + // Save language types. + domain_conf_variable_set($domain_id, 'language_types', $language_types); + + // Ensure that subsequent calls of language_types_configurable() return the + // updated language type information. + drupal_static_reset('language_types_configurable'); +}