You are here

domain_variable_locale.module in Domain Variable 7

Set enabled Languages per Domain realm.

File

domain_variable_locale/domain_variable_locale.module
View source
<?php

/**
 * @file
 * Set enabled Languages per Domain realm.
 */

/**
 * Implements hook_init().
 *
 * Overrides static_cache for language_list function.
 */
function domain_variable_locale_init() {

  // Get the language_list variable and continue if the variable has been set.
  $language_list = variable_get('language_list');
  if (!$language_list) {
    return;
  }

  // Do not continue if this is Drupal's default admin page for languages.
  if (current_path() == 'admin/config/regional/language') {
    return;
  }

  // Initialize the language list() function from Drupal Core's bootstrap.inc.
  // It will be cached by drupal_static(). We clone its results, alter it and
  // then overwrite the drupal_static() result.
  $existing_languages = language_list();

  // When a new language is added, the key it's not available in language_list
  // until the domain form is saved; so we mark it as disabled here.
  $new_languages = array_diff_key($existing_languages, $language_list);
  if (!empty($new_languages)) {
    $new_language_list = array_fill_keys(array_keys($new_languages), 0);
    $language_list += $new_language_list;
  }

  // Clone drupal_static result and reset its cache.
  $language_list_static_clone = drupal_static("language_list");
  drupal_static_reset('language_list');

  // Iterate over variable 'language_list' and unset all languages that are
  // disabled for this Realm.
  foreach ($language_list as $language_key => $enabled) {
    if (!$enabled) {
      _domain_variable_locale_recursive_unset($language_list_static_clone, $language_key);
    }
  }

  // Assign changed results to drupal_static() again.
  $languages =& drupal_static('language_list');
  $languages = $language_list_static_clone;
}

/**
 * Helper function that unsets array-keys recursively.
 *
 * @see: http://stackoverflow.com/questions/1708860/php-recursively-unset-array-keys-if-match
 */
function _domain_variable_locale_recursive_unset(&$array, $unwanted_key) {
  unset($array[$unwanted_key]);
  foreach ($array as &$value) {
    if (is_array($value)) {
      _domain_variable_locale_recursive_unset($value, $unwanted_key);
    }
  }
}

Functions

Namesort descending Description
domain_variable_locale_init Implements hook_init().
_domain_variable_locale_recursive_unset Helper function that unsets array-keys recursively.