reverted:
--- b/language_fallback.admin.inc
+++ /dev/null
@@ -1,345 +0,0 @@
- $key);
-
- $form[$key]['fallback'] = array(
- '#type' => 'fieldset',
- '#title' => t('Language fallback'),
- '#tree' => true,
- '#prefix' => '
',
- '#suffix' => '
',
- );
-
- $countries = language_fallback_get_countries();
-
- if (empty($form_state[$key]['fallback']['countries'])) {
- $form_state[$key]['fallback']['countries'] = array(LANGUAGE_FALLBACK_ALL_COUNTRIES);
-
- if ($langcode) {
- // Get all countries that we have a fallback for this language
- $result = db_select('language_fallback', 'f')
- ->fields('f', array('country'))
- ->condition('language', $langcode, '=')
- ->condition('country', LANGUAGE_FALLBACK_ALL_COUNTRIES, '<>')
- ->execute();
- while($c = $result->fetchField()){
- $form_state[$key]['fallback']['countries'][] = $c;
- }
- }
-
- }
-
- foreach($form_state[$key]['fallback']['countries'] as $country){
- language_fallback_get_fallback_form($form, $form_state, $key, $country, $countries[$country]);
-
- // Unset countries that are in use
- unset($countries[$country]);
- }
-
- if (count($countries) > 0) {
- $form[$key]['fallback']['country'] = array(
- '#type' => 'select',
- '#options' => $countries
- );
-
- $form[$key]['fallback']['add_country'] = array(
- '#type' => 'submit',
- '#value' => t('Add country specific fallback'),
- '#submit' => array('language_fallback_country_add_another'),
- '#ajax' => array(
- 'callback' => 'language_fallback_country_add_remove_ajax_callback',
- 'wrapper' => 'countries-fieldset-wrapper',
- ),
- );
- }
-
- $form['#submit'][] = 'language_fallback_country_add_language_submit';
- }
-}
-
-function language_fallback_get_fallback_form(&$form, &$form_state, $key, $ccode = LANGUAGE_FALLBACK_ALL_COUNTRIES, $country = false, $scope = 'admin') {
- $langcode = isset($form['langcode_view']) ? $form['langcode']['#value'] : NULL;
-
- $options = &drupal_static(__FUNCTION__);
-
- if (!$options) {
- // Cache the language options as this function may get called multiple times per page load.
- $default_lang = language_default();
- $languages = language_list();
- $options = locale_language_list();
-
- // Alter label of default language.
- if (isset($options[$default_lang->language])) {
-
- //$options = array('' => t('Default language (@lang)', array('@lang' => $default_lang->name))) + $options;
- //unset($options[$default_lang->language]);
- $options[$default_lang->language] = t('Default language (@lang)', array('@lang' => $default_lang->name));
- $options = array('' => t('Select language')) + $options;
- }
-
- // Add native name.
- foreach ($options as $k => $option) {
- if (!empty($languages[$k]->native)) {
- $options[$k] .= " ({$languages[$k]->native})";
- }
- }
- }
-
- $description = '';
- if ($ccode == LANGUAGE_FALLBACK_ALL_COUNTRIES) {
- $description = (!variable_get('language_fallback_country', false)
- ? t('Enable country specific fallback to create different fallbacks for each country.')
- : t('This is default fallback chain for all countries. It will be used only if no country specific fallback is available.'));
- }
- $form[$key]['fallback'][$ccode] = array(
- '#type' => 'fieldset',
- '#title' => $country,
- '#prefix' => '', // This is unique to each country fallback chain
- '#suffix' => '
',
- '#description' => $description
- );
-
- $chain = ($langcode ? language_fallback_get_chain($langcode, $ccode, ($scope == 'admin')) : array());
-
- if (empty($form_state[$key]['fallback'][$ccode]['chain_length'])) {
- $form_state[$key]['fallback'][$ccode]['chain_length'] = count($chain) + 1;
- }
-
- for($i = 0; $i < $form_state[$key]['fallback'][$ccode]['chain_length']; $i++) {
- $form[$key]['fallback'][$ccode][$i] = array(
- '#type' => 'select',
- '#options' => $options,
- '#default_value' => ($i < count($chain) ? $chain[$i] : ''),
- );
- }
-
- /**
- * WARNING!
- * Due to the nature of AJAX FAPI the submit button value MUST be unique!
- * See this: https://drupal.org/node/1342066
- * Or try this workaround: http://stackoverflow.com/a/6521617/899092\
- *
- * This also applies to the "remove" button below.
- */
- $form[$key]['fallback'][$ccode]['add'] = array(
- '#type' => 'submit',
- '#value' => ($country ? t('Add another language for !country', array('!country' => $country)) : t('Add another language')),
- '#submit' => array('language_fallback_country_add_language_add_another'), // This is shared across all countries
- '#ajax' => array(
- 'callback' => 'language_fallback_country_add_language_ajax_callback', // This is shared across all countries
- 'wrapper' => 'country-' . $ccode . '-fieldset-wrapper', // This is unique to each country fallback chain
- ),
- );
-
- if ($form_state[$key]['fallback'][$ccode]['chain_length'] > 1) {
- $form[$key]['fallback'][$ccode]['remove_one'] = array(
- '#type' => 'submit',
- '#value' => ($country ? t('Remove last language for !country', array('!country' => $country)) : t('Remove last language')),
- '#submit' => array('language_fallback_country_remove_last_submit'), // This is shared across all countries
- '#ajax' => array(
- 'callback' => 'language_fallback_country_add_language_ajax_callback', // This is shared across all countries
- 'wrapper' => 'country-' . $ccode . '-fieldset-wrapper', // This is unique to each country fallback chain
- ),
- );
- }
-
- if ($ccode != LANGUAGE_FALLBACK_ALL_COUNTRIES) {
- $form[$key]['fallback'][$ccode]['remove'] = array(
- '#type' => 'submit',
- '#value' => t('Remove fallback chain for !country', array('!country' => $country)),
- '#submit' => array('language_fallback_country_remove_chain'), // This is shared across all countries
- '#ajax' => array(
- 'callback' => 'language_fallback_country_add_remove_ajax_callback', // This is shared across all countries
- 'wrapper' => 'countries-fieldset-wrapper',
- ),
- );
- }
-}
-
-/**
- * Ajax submit function for "Remove fallback chain for ..." button
- * @param unknown $form
- * @param unknown $form_state
- */
-function language_fallback_country_remove_chain($form, &$form_state) {
-
- $key = $form['_key_']['#value'];
-
- // Country to remove
- $remove = $form_state['triggering_element']['#parents'][1];
- if (($index = array_search($remove, $form_state[$key]['fallback']['countries'])) !== false) {
- // Remove country
- unset($form_state[$key]['fallback']['countries'][$index]);
- }
-
- $form_state['rebuild'] = TRUE;
-}
-
-/**
- * Callback function for ajax calls that add country specific fallback fieldset.
- * @param unknown $form
- * @param unknown $form_state
- */
-function language_fallback_country_add_remove_ajax_callback($form, $form_state) {
-
- $key = $form['_key_']['#value'];
- return $form[$key]['fallback'];
-}
-
-/**
- * Ajax submit function for "Add country specific fallback" button.
- * @param unknown $form
- * @param unknown $form_state
- */
-function language_fallback_country_add_another($form, &$form_state) {
-
- $key = $form['_key_']['#value'];
- $form_state[$key]['fallback']['countries'][] = $form_state['values']['fallback']['country'];
-
- $form_state['rebuild'] = TRUE;
-}
-
-/**
- * Callback function for ajax calls that add language select element to the country fieldset.
- * @param unknown $form
- * @param unknown $form_state
- */
-function language_fallback_country_add_language_ajax_callback($form, $form_state) {
-
- $key = $form['_key_']['#value'];
-
- /**
- * See language_fallback_country_add_language_add_another() function
- * for explanation of the followin line:
- */
- $id = count($form_state['triggering_element']['#parents']) - 2;
- $country = $form_state['triggering_element']['#parents'][$id];
-
- return $form[$key]['fallback'][$country];
-}
-
-/**
- * Ajax submit function for "Remove last language from ..." button.
- * See function language_fallback_country_add_language_add_another() for details.
- *
- * @param unknown $form
- * @param unknown $form_state
- */
-function language_fallback_country_remove_last_submit($form, &$form_state) {
- $key = $form['_key_']['#value'];
-
- // See function language_fallback_country_add_language_add_another() for details.
- $id = count($form_state['triggering_element']['#parents']) - 2;
- $country = $form_state['triggering_element']['#parents'][$id];
- $form_state[$key]['fallback'][$country]['chain_length']--;
-
- $form_state['rebuild'] = TRUE;
-}
-
-/**
- * Ajax submit function for "Add another language to ..." button.
- * @param unknown $form
- * @param unknown $form_state
- */
-function language_fallback_country_add_language_add_another($form, &$form_state) {
-
- $key = $form['_key_']['#value'];
-
- /**
- * The element that triggered this callback is "described" here:
- * $form_state['triggering_element'].
- * We know that this is an array that looks like this:
- * array(
- * 0 => 'fallback',
- * 1 => [COUNTRY],
- * 2 => 'add'
- * );
- * So we need the '1' index to know wchich country needs another select element.
- */
- $id = count($form_state['triggering_element']['#parents']) - 2;
- $country = $form_state['triggering_element']['#parents'][$id];
- $form_state[$key]['fallback'][$country]['chain_length']++;
-
- $form_state['rebuild'] = TRUE;
-}
-
-function language_fallback_country_add_language_submit($form, &$form_state) {
- $key = $form['_key_']['#value'];
- $fallbacks = $form_state['values']['fallback'];
- $language = $form_state['values']['langcode'];
-
- // Delete old entries. New entries will be added only if needed.
- db_delete('language_fallback')->condition('language', $language, '=')->execute();
- variable_del('locale_custom_strings_' . $language);
- $set_variable = false;
-
- foreach($fallbacks as $country => $fallback) {
- // Omit unwanted values.
- if (!is_array($fallback))
- continue;
-
- // Fix fallback array
- $fixed = language_fallback_fix_chain($fallback, $language);
-
- if (count($fixed) > 0) {
- // This stores our settings
- db_insert('language_fallback')->fields(array('language' => $language, 'country' => $country, 'chain' => implode('|', $fixed)))->execute();
-
- $set_variable = true;
- }
- }
-
- if ($set_variable) {
- // This tells drupal to use our fallback mechanism for string translation
- variable_set('locale_custom_strings_' . $language, new localeWithFallback($language));
- }
-}
-
-/**
- * Sanitize fallback chain to remove duplicates and default language.
- *
- * @param array $fallback
- * @param string $language
- * @return array clean fallback array.
- */
-function language_fallback_fix_chain($fallback, $language = LANGUAGE_NONE) {
- $valid = &drupal_static(__FUNCTION__);
-
- if (empty($valid))
- $valid = locale_language_list();
-
- // Fix fallback array
- $fixed = array();
- foreach($fallback as $id => $lang) {
- // Accept only valid languages.
- if (!isset($valid[$lang]))
- continue;
-
- // Prevent deadlocks.
- if ($lang == $language)
- continue;
-
- // Ignore languages that are already added.
- if (array_search($lang, $fixed) !== false)
- continue;
-
- // Add language to the chain.
- $fixed[] = $lang;
- }
-
- return $fixed;
-}
\ No newline at end of file
reverted:
--- b/language_fallback.block.inc
+++ /dev/null
@@ -1,143 +0,0 @@
- t('Country selection'),
- 'cache' => DRUPAL_NO_CACHE,
- );
-
- $blocks['language_fallback'] = array(
- 'info' => t('Language fallback selection'),
- 'cache' => DRUPAL_NO_CACHE,
- );
- return $blocks;
-}
-
-/**
- * Implements hook_block_view().
- */
-function language_fallback_block_view($delta = '') {
- switch ($delta) {
- case 'country_selection':
- $block['subject'] = t('Select country');
- $block['content'] = drupal_get_form('language_fallback_country_select_block_form');
- return $block;
-
- case 'language_fallback':
- $block['subject'] = t('Select language fallback');
- $block['content'] = drupal_get_form('language_fallback_fallback_select_block_form');
- return $block;
- }
-}
-
-/**
- * Callback to generate form for user fallback block
- * @param unknown $form_id
- * @param unknown $form_state
- * @return multitype:string NULL
- */
-function language_fallback_fallback_select_block_form($form_id, &$form_state) {
- $form = array();
- $key = '_fb_';
- $form['_key_'] = array('#value' => $key);
- $form[$key] = array('#tree' => 'true');
-
- /* 'key', langcode_view' and 'langcode' are to satisfy form building function.
- * See language_fallback_get_fallback_form function for details.
- */
- $form['langcode_view'] = array();
- $form['langcode'] = array('#value' => LANGUAGE_NONE);
-
- // Buld language fallback form with our reusable function.
- language_fallback_get_fallback_form($form, $form_state, $key, LANGUAGE_FALLBACK_ALL_COUNTRIES, false, 'block');
-
- // Get information about current fallback chain
- $description = '';
- $prop = 'name'; // Change to "native" to use native names;
- $user_chain = language_fallback_get_user_chain();
- if (empty($user_chain)) {
- global $language;
- $global_chain = language_fallback_get_chain($language->language);
- $all_languages = locale_language_list($prop, true);
- $display_chain = array();
- foreach($global_chain as $langcode) {
- $display_chain[] = $all_languages[$langcode];
- }
-
- if (count($display_chain) > 0)
- $description = t('Language fallback for content unavailable in %current is %fallback. Set your own fallback here.', array('%current' => $language->{$prop}, '%fallback' => implode(" => ", $display_chain)));
- else
- $description = t('Language fallback for content unavailable in %current is not set. Set your own fallback here.', array('%current' => $language->{$prop}));
- } else
- $description = t('Change language fallback for content unavailable in %current.', array('%current' => $language->{$prop}));
-
- /* Replace description from chain selection fieldset
- * with information about currently defined fallback
- */
- $form[$key]['fallback'][LANGUAGE_FALLBACK_ALL_COUNTRIES]['#description'] = $description;
-
- $form['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Save')
- );
-
- return $form;
-}
-
-/**
- * Callback function for user fallback selection form submit
- * @param unknown $form
- * @param unknown $form_state
- */
-function language_fallback_fallback_select_block_form_submit($form, $form_state) {
- // Read and fix user defined fallback chain.
- $key = $form['_key_']['#value'];
- $chain = language_fallback_fix_chain($form_state['values'][$key]['fallback'][LANGUAGE_FALLBACK_ALL_COUNTRIES]);
-
- language_fallback_set_user_chain($chain);
-}
-
-/**
- * Callback to generate form for country selection block.
- * @param unknown $form_id
- * @param unknown $form_state
- * @return multitype:multitype:NULL multitype:string NULL multitype:string NULL unknown
- */
-function language_fallback_country_select_block_form($form_id, $form_state) {
-
- /* There is no need for fancy AJAXification.
- * The page reload is needed anyway to view
- * content in another language.
- */
- $form = array();
-
- $countries = language_fallback_get_countries();
-
- if (count($countries) < 2) {
- $form[] = array('#markup' => t('Country selection is disabled'));
- } else {
- $form['country'] = array(
- '#type' => 'select',
- '#options' => $countries,
- '#default_value' => language_fallback_get_country()
- );
- $form['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Save')
- );
- }
-
- return $form;
-}
-
-/**
- * Callback function for user country selection form submit
- * @param unknown $form
- * @param unknown $form_state
- */
-function language_fallback_country_select_block_form_submit($form, $form_state) {
- language_fallback_set_country($form_state['values']['country']);
-}
\ No newline at end of file
diff -u b/language_fallback.fallback.inc b/language_fallback.fallback.inc
--- b/language_fallback.fallback.inc
+++ b/language_fallback.fallback.inc
@@ -39,7 +39,7 @@
if (function_exists('language_fallback_get_chain')) {
$this->fallbacks = language_fallback_get_chain($this->langcode);
$this->initialized = true;
-
+
// Reinitialize this offset with apropriate data.
if (isset($this->cache[$offset]))
unset($this->cache[$offset]);
@@ -48,9 +48,9 @@
$this->fallbacks = array();
}
}
-
+
$this->cache[$offset] = new localeContextWithFallback($this->langcode, $this->fallbacks, $offset);
-
+
}
return $this->cache[$offset];
}
@@ -85,7 +85,7 @@
$this->first_language = isset($languages[$langcode]) ? $langcode : language_default('language');
$this->second_language = $fallbacks ? array_shift($fallbacks) : language_default('language');
$this->context = $context;
-
+
// If the fallback is the default language, we don't need a
// localeContextWithFallback since the default language does not have a
// fallback. Otherwise we use a localeContextWithFallback so we gradually
diff -u b/language_fallback.install b/language_fallback.install
--- b/language_fallback.install
+++ b/language_fallback.install
@@ -1,120 +1,121 @@
- 'Contains fallback chains for languages',
- 'fields' => array(
- 'language' => array(
- 'description' => 'Lang code',
- 'type' => 'varchar',
- 'length' => 8,
- 'not null' => true,
- ),
- 'country' => array(
- 'description' => 'Visitor country code or "all" for all countries',
- 'type' => 'varchar',
- 'length' => 3,
- 'not null' => true,
- 'default' => 'all'
- ),
- 'chain' => array(
- 'description' => 'Fallback chain ("|" delimiter)',
- 'type' => 'varchar',
- 'length' => 255,
- 'not null' => true,
- ),
- ),
- 'primary key' => array('language', 'country'),
- );
- return $schema;
-}
-
-/**
- * Implements hook_disable()
- *
- * WARNING!
- * It is mandatory to remove all "locale_custom_strings_ID" variables!
- * After this module is disabled the class localeWithFallback is no longer
- * available but these variables hold reference to it.
- */
-function language_fallback_disable(){
- /**
- * We do not know all our variable names because they are created dynamically.
- * So to remove unwanted variables we first need to know their names.
- *
- * Our variables are "locale_custom_strings_ID"
- * and their values contain "localeWithFallback" stirng.
- *
- * So the trick is to read all variable names from "variable" table
- * that met the criteria above.
- */
-
- $result = db_update("variable")
- ->expression('name', "CONCAT('_', name)")
- ->condition('name', 'locale_custom_string_%', 'LIKE')
- ->condition('value', '%localeWithFallback%', 'LIKE')
- ->execute();
-}
-
-/**
- * Implements hook_enable()
- *
- * Revert back "locale_custom_strings_ID" variable names.
- * See language_fallback_disable() for details.
- */
-function language_fallback_enable(){
- $result = db_update("variable")
- ->expression('name', "REPLACE(name, '_locale_custom_strings_', 'locale_custom_strings_')")
- ->condition('name', '_locale_custom_string_%', 'LIKE')
- ->condition('value', '%localeWithFallback%', 'LIKE')
- ->execute();
-}
-
-/**
- * Implements hook_uninstall()
- *
- * WARNING!
- * It is mandatory to remove all "locale_custom_strings_ID" variables!
- * After this module is disabled the class localeWithFallback is no longer
- * available but these variables hold reference to it.
- */
-function language_fallback_uninstall(){
-
- /**
- * We do not know all our variable names because they are created dynamically.
- * So to remove unwanted variables we first need to know their names.
- *
- * Our variables are "locale_custom_strings_ID" but they were renamed on
- * hook_disable to "_locale_custom_strings_ID".
- * and their values contain "localeWithFallback" stirng.
- *
- * So the trick is to read all variable names from "variable" table
- * that met the criteria above.
- */
- $result = db_select("variable", "v")
- ->fields('v', array('name'))
- ->condition('name', '_locale_custom_string_%', 'LIKE')
- ->condition('value', '%localeWithFallback%', 'LIKE')
- ->execute();
-
- while($name = $result->fetchField()){
- variable_del($name);
- }
-
- if (isset($_SESSION['language_fallback']))
- unset($_SESSION['language_fallback']);
-
- /**
- * WARNING!
- * This is also very important as variables are cached!
- */
- cache_clear_all('variables', 'cache_bootstrap');
-}
\ No newline at end of file
+ 'Contains fallback chains for languages',
+ 'fields' => array(
+ 'language' => array(
+ 'description' => 'Lang code',
+ 'type' => 'varchar',
+ 'length' => 8,
+ 'not null' => true,
+ ),
+ 'country' => array(
+ 'description' => 'Visitor country code or "all" for all countries',
+ 'type' => 'varchar',
+ 'length' => 3,
+ 'not null' => true,
+ 'default' => 'all'
+ ),
+ 'chain' => array(
+ 'description' => 'Fallback chain ("|" delimiter)',
+ 'type' => 'varchar',
+ 'length' => 255,
+ 'not null' => true,
+ ),
+ ),
+ 'primary key' => array('language', 'country'),
+ );
+ return $schema;
+}
+
+/**
+ * Implements hook_disable()
+ *
+ * WARNING!
+ * It is mandatory to remove all "locale_custom_strings_ID" variables!
+ * After this module is disabled the class localeWithFallback is no longer
+ * available but these variables hold reference to it.
+ */
+function language_fallback_disable(){
+ /**
+ * We do not know all our variable names because they are created dynamically.
+ * So to remove unwanted variables we first need to know their names.
+ *
+ * Our variables are "locale_custom_strings_ID"
+ * and their values contain "localeWithFallback" string.
+ *
+ * So the trick is to read all variable names from "variable" table
+ * that met the criteria above.
+ */
+
+ $result = db_update("variable")
+ ->expression('name', "CONCAT('_', name)")
+ ->condition('name', 'locale_custom_string_%', 'LIKE')
+ ->condition('value', '%localeWithFallback%', 'LIKE')
+ ->execute();
+}
+
+/**
+ * Implements hook_enable()
+ *
+ * Revert back "locale_custom_strings_ID" variable names.
+ * See language_fallback_disable() for details.
+ */
+function language_fallback_enable(){
+ $result = db_update("variable")
+ ->expression('name', "REPLACE(name, '_locale_custom_strings_', 'locale_custom_strings_')")
+ ->condition('name', '_locale_custom_string_%', 'LIKE')
+ ->condition('value', '%localeWithFallback%', 'LIKE')
+ ->execute();
+}
+
+/**
+ * Implements hook_uninstall().
+ *
+ * WARNING!
+ * It is mandatory to remove all "locale_custom_strings_ID" variables!
+ * After this module is disabled the class localeWithFallback is no
+ * longer available but these variables hold reference to it.
+ */
+function language_fallback_uninstall(){
+
+ /**
+ * We do not know all our variable names because they are created dynamically.
+ * So to remove unwanted variables we first need to know their names.
+ *
+ * Our variables are "locale_custom_strings_ID" but they were renamed on
+ * hook_disable to "_locale_custom_strings_ID".
+ * and their values contain "localeWithFallback" stirng.
+ *
+ * So the trick is to read all variable names from "variable" table
+ * that met the criteria above.
+ */
+ $result = db_select("variable", "v")
+ ->fields('v', array('name'))
+ ->condition('name', '_locale_custom_string_%', 'LIKE')
+ ->condition('value', '%localeWithFallback%', 'LIKE')
+ ->execute();
+
+ while ($name = $result->fetchField()) {
+ variable_del($name);
+ }
+
+ if (isset($_SESSION['language_fallback'])) {
+ unset($_SESSION['language_fallback']);
+ }
+
+ /**
+ * WARNING!
+ * This is also very important as variables are cached!
+ */
+ cache_clear_all('variables', 'cache_bootstrap');
+}
diff -u b/language_fallback.module b/language_fallback.module
--- b/language_fallback.module
+++ b/language_fallback.module
@@ -30,32 +30,32 @@
/**
* Implements Drupal hook_language_fallback_candidates_alter().
- *
+ *
* This function provides fallback features for entities.
- *
+ *
* @param $fallback_candidates: An array of language codes whose order will determine the language fallback order.
*/
function language_fallback_language_fallback_candidates_alter(array &$fallback_candidates) {
-
+
// Check if entity fallback is enabled
if (!variable_get('language_fallback_entity', false))
return;
-
+
global $language;
-
+
if (LANGUAGE_NONE == $language->language)
return; // There is no fallback for "Undeterminated" language.
-
+
// Get fallback chain for current language
$fallbacks = language_fallback_get_chain($language->language);
-
+
if (count($fallbacks) > 0) {
$fallback_candidates = $fallbacks;
} else {
/* By default Drupal uses list of all enabled languages as a fallback chain.
* This is a bit weird and makes no sense in case of sites that actually
* make use of fallback mechanisms.
- *
+ *
* That is why we rewrite the fallback chain with one default value only.
*/
$fallback_candidates = array(LANGUAGE_NONE);
@@ -65,42 +65,42 @@
/**
* Get fallback chain for specified language.
* If you want to hook up to language fallback module - this is the place to start.
- *
+ *
* @param string $lang: Language code for which fallback chain is needed.
* @param string $country: Country code for which fallback chain is needed.
* @param bool $strict: Used only internally.
* @return array of languages. An empty array is returned if no fallback is defined.
*/
function language_fallback_get_chain($lang, $country = false, $strict = false){
-
+
if (!$strict) {
/* User defined fallabck always takes precedence before any other fallback
* But we can't use it on administration pages!
*/
$user_chain = language_fallback_get_user_chain();
-
+
if (count($user_chain) > 0)
return $user_chain;
}
-
+
$chains = &drupal_static(__FUNCTION__);
-
+
if (!isset($chains))
$chains = array();
-
+
if ($country === false)
$country = language_fallback_get_country();
-
+
// Check cache - maybe the chain was already loaded
if (!isset($chains[$lang][$country])) {
-
+
$chain = db_select('language_fallback', 'f')
->fields('f', array('chain'))
->condition('language', $lang, '=')
->condition('country', $country, '=')
->execute()
->fetchField();
-
+
if ($chain) {
// We have a fallback chain for this country
$chains[$lang][$country] = explode("|", $chain); // for better readability fallback chains are stored in a string separated with "|" character.
@@ -117,7 +117,7 @@
// We dno't have fallback so we try default fallback for all countries
// WARNING! Recurency!
$chains[$lang][$country] = language_fallback_get_chain($lang, LANGUAGE_FALLBACK_ALL_COUNTRIES);
- }
+ }
}
return $chains[$lang][$country];
@@ -126,7 +126,7 @@
/**
* Get user defined fallback chain if it exists.
* If user defined chain does not exists an empty array is returned.
- *
+ *
* @return array: user defined fallback chain.
*/
function language_fallback_get_user_chain() {
@@ -134,7 +134,7 @@
&& is_array($_SESSION['language_fallback']['user_chain'])
&& count($_SESSION['language_fallback']['user_chain']) > 0)
return $_SESSION['language_fallback']['user_chain'];
-
+
return array();
}
@@ -146,13 +146,13 @@
function language_fallback_set_user_chain($chain = null) {
if (empty($chain))
unset ($_SESSION['language_fallback']['user_chain']);
- else
+ else
$_SESSION['language_fallback']['user_chain'] = $chain;
}
/**
* Get country code for fallback check.
- *
+ *
* @return string Either country code for fallback chain lookup or empty string if no country is specified for current visitor.
*/
function language_fallback_get_country(){
@@ -160,21 +160,21 @@
if (isset($_SESSION['language_fallback']['country'])) {
return $_SESSION['language_fallback']['country'];
} else if (function_exists('smart_ip_get_location')) {
-
+
// Use smart IP to get user location
$country = smart_ip_get_location(ip_address());
if ($country !== false)
$_SESSION['language_fallback']['country'] = strtoupper($country['country_code']);
else
$_SESSION['language_fallback']['country'] = LANGUAGE_FALLBACK_ALL_COUNTRIES;
-
+
// We store country in a asession variable only if it is really needed.
}
-
+
if (isset($_SESSION['language_fallback']['country'])) {
return $_SESSION['language_fallback']['country'];
}
-
+
return LANGUAGE_FALLBACK_ALL_COUNTRIES;
}
@@ -185,7 +185,7 @@
function language_fallback_set_country($country = false){
if (!$country)
$country = LANGUAGE_FALLBACK_ALL_COUNTRIES;
-
+
$_SESSION['language_fallback']['country'] = $country;
}
@@ -208,7 +208,7 @@
/**
* Implementation of hook_field_views_data_alter().
- *
+ *
* This function overwrites default 'views_handler_filter_locale_language'
* filter handler with our own implementation that is fallback-aware.
*/
@@ -220 +220 @@
-}
\ No newline at end of file
+}
reverted:
--- b/language_fallback.settings.inc
+++ /dev/null
@@ -1,20 +0,0 @@
- 'checkbox',
- '#title' => t('Enable entity translation fallback.'),
- '#description' => t('Entity translation module must be enabled for this feature to work.'),
- '#default_value' => variable_get('language_fallback_entity', false)
- );
-
- $form['language_fallback_country'] = array(
- '#type' => 'checkbox',
- '#title' => t('Enable country specific fallback.'),
- '#description' => t('This feature allows you to create different fallbacks according to the user country. It is recommended to install Smart IP module to automatically map user IP to country. Otherwise users will have to use country selection block provided by this module.'),
- '#default_value' => variable_get('language_fallback_country', false)
- );
-
- return system_settings_form($form);
-}
\ No newline at end of file