You are here

domain_lang.module in Domain Lang 7

Same filename and directory in other branches
  1. 8 domain_lang.module

Domain Language detection and selection module.

File

domain_lang.module
View source
<?php

/**
 * @file
 * Domain Language detection and selection module.
 */

/**
 * Implements hook_menu().
 */
function domain_lang_menu() {
  return array(
    'admin/structure/domain/view/%domain/language-detect-select' => 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 <a href="@url">Domain language detection and selection page</a> 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 int $domain_id
 *   The unique domain ID that is being edited.
 * @param string $type
 *   The language negotiation type.
 * @param array $language_providers
 *   An array of language negotiation provider weights keyed by provider ID.
 */
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 int $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');
}

Functions

Namesort descending Description
domain_lang_form_locale_languages_configure_form_alter Implements hook_form_FORM_ID_alter().
domain_lang_form_locale_language_providers_session_form_alter Implements hook_form_FORM_ID_alter().
domain_lang_form_locale_language_providers_submit Language provider form submit callback. Setup redirect to proper page.
domain_lang_form_locale_language_providers_url_form_alter Implements hook_form_FORM_ID_alter().
domain_lang_menu Implements hook_menu().
domain_lang_negotiation_set Saves a list of language negotiation providers for requested domain.
domain_lang_types_set Updates the language type configuration for requested domain.
_domain_lang_form_locale_language_providers_alter Alter language provider form callback.