You are here

language_selection_page.module in Language Selection Page 6

The main module page of language_selection_page.

File

language_selection_page.module
View source
<?php

/**
 * @file
 * The main module page of language_selection_page.
 *
 */
define('LANGUAGE_SELECTION_PAGE_LANGUAGE_COOKIE_KEY', 'language_prefix');

/**
 * Implements hook_menu().
 */
function language_selection_page_menu() {
  $items = array();
  $items['admin/settings/language/i18n/selection_page'] = array(
    'title' => 'Language selection page',
    'description' => 'Configure the language selection page behavior',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'language_selection_page_admin',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'language_selection_page.admin.inc',
    'type' => MENU_LOCAL_TASK,
  );
  $items['language_selection'] = array(
    'description' => 'Language selection page',
    'page callback' => 'language_selection_page_selection_page',
    'access arguments' => array(
      'access content',
    ),
    'file' => 'language_selection_page.pages.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implements hook_init().
 */
function language_selection_page_init() {
  global $language;
  if (variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE) != 2) {
    return;
  }
  module_load_include('inc', 'language_selection_page', 'includes/language_selection_page.helpers');
  $lang_count = _language_selection_page_check_language_count();
  if ($lang_count['db'] < 2) {
    return;
  }
  $black_paths = variable_get('language_selection_page_blacklisted_paths', array());
  if ((bool) drupal_match_path($_GET['q'], implode("\r\n", $black_paths))) {
    return;
  }
  if ($_GET['q'] == 'language_selection') {
    return;
  }
  if (!isset($_SERVER['SERVER_ADDR'])) {
    return;
  }
  drupal_init_language();
  $url_parsed = _language_selection_page_parse_url_custom();
  $lang_from_url_argument = _language_selection_page_detect_lang_from_url_argument();
  $lang_from_url = _language_selection_page_detect_lang_from_url();
  $lang_from_cookie = _language_selection_page_detect_lang_from_cookie();
  $language = _language_selection_page_resolve_lang($lang_from_url_argument, $lang_from_url, $lang_from_cookie);
  if ($lang_from_url || $lang_from_url_argument) {
    $cookie = variable_get('language_selection_page_use_language_cookie', 8) == 16 ? $language->language : NULL;
    setcookie(LANGUAGE_SELECTION_PAGE_LANGUAGE_COOKIE_KEY, $cookie, time() + variable_get('language_selection_page_cookie_lifetime', 60 * 60 * 24 * 30), '/');
    return;
  }
  $query = array();
  $query['destination'] = $_GET['q'];
  $query = array_merge((array) $query, (array) $url_parsed['query']);
  unset($query['q']);
  $destination = NULL;

  // It may be hard to understand at first sight but this way of working
  // reduce the number of level of 'if-else' and improve code readability.
  $behavior = (int) variable_get('language_selection_page_use_language_cookie', 8) + (int) variable_get('language_selection_page_enable', 2);

  /***************************************************************
   * $behavior explanations                                       *
   ****************************************************************
   * 9 = 1 + 8                                                    *
   ****************************************************************
   * Do not redirect unless a valid language cookie is found.     *
   * Cookie functionality is disabled.                            *
   ****************************************************************
   * 10 = 2 + 8                                                   *
   ****************************************************************
   * Disable redirection and cookie.                              *
   ****************************************************************
   * 18 = 2 + 16                                                  *
   ****************************************************************
   * Disable redirection and enable cookie.                       *
   ****************************************************************
   * 17 = 1 + 16                                                  *
   ****************************************************************
   * Do not redirect unless a valid language cookie is found.     *
   * Disable redirection and enable cookie.                       *
   ****************************************************************
   * 12 = 4 + 8                                                   *
   ****************************************************************
   * Enable redirection and disable cookie.                       *
   ****************************************************************
   * 20 = 4 + 16                                                  *
   ****************************************************************
   * Enable redirection and enable cookie.                        *
   ***************************************************************/
  switch ($behavior) {
    case 9:
    case 10:
    case 18:
      $destination = NULL;
      break;
    case 17:
      $destination = $_GET['q'];
      if ($lang_from_cookie) {
        unset($query['destination']);
        break;
      }

    // Intentionally, no break.
    case 12:
    case 20:
      $language = new stdClass();
      $destination = 'language_selection';
      break;
  }
  if ($destination) {
    drupal_goto($destination, drupal_query_string_encode($query));
  }
}

/**
 * Implements hook_theme().
 */
function language_selection_page_theme() {
  return array(
    'language_selection_page' => array(
      'arguments' => array(
        'data' => array(),
      ),
      'path' => drupal_get_path('module', 'language_selection_page') . '/themes',
      'template' => 'language_selection_page',
    ),
    'language_selection_page_body' => array(
      'arguments' => array(
        'data' => array(),
      ),
      'path' => drupal_get_path('module', 'language_selection_page') . '/themes',
      'template' => 'language_selection_page_body',
    ),
  );
}

Functions

Constants

Namesort descending Description
LANGUAGE_SELECTION_PAGE_LANGUAGE_COOKIE_KEY @file The main module page of language_selection_page.