You are here

language_cookie.module in Language Cookie 7.2

Same filename and directory in other branches
  1. 8 language_cookie.module
  2. 7 language_cookie.module

Language Cookie module.

File

language_cookie.module
View source
<?php

/**
 * @file
 * Language Cookie module.
 */

/**
 * Constant for language negotiation type.
 */
define('LANGUAGE_COOKIE_NEGOTIATION', 'language_cookie');

/**
 * Implements hook_menu().
 */
function language_cookie_menu() {
  $items = array();
  $items['admin/config/regional/language/configure/cookie'] = array(
    'title' => 'Cookie language detection configuration',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'language_cookie_admin_form',
    ),
    'access arguments' => array(
      'administer languages',
    ),
    'file' => 'language_cookie.admin.inc',
    'type' => MENU_VISIBLE_IN_BREADCRUMB,
  );
  return $items;
}

/**
 * Implements hook_language_negotiation_info().
 */
function language_cookie_language_negotiation_info() {
  return array(
    LANGUAGE_COOKIE_NEGOTIATION => array(
      'types' => array(
        LANGUAGE_TYPE_CONTENT,
        LANGUAGE_TYPE_INTERFACE,
        LANGUAGE_TYPE_URL,
      ),
      'callbacks' => array(
        'language' => 'language_cookie_language',
      ),
      'file' => drupal_get_path('module', 'language_cookie') . '/language_cookie.module',
      'weight' => -5,
      'name' => t('Cookie'),
      'description' => t('Determine the language from a cookie'),
      'config' => 'admin/config/regional/language/configure/cookie',
    ),
  );
}

/**
 * Alters hook_language_types_info().
 *
 * For determining the URL language (LANGUAGE_TYPE_URL / $language_url global),
 * the cookie language provider should take precedence over the URL fallback
 * provider.
 *
 * The first provider (LOCALE_LANGUAGE_NEGOTIATION_URL) attempts to get the
 * language to use for links in the page from the path prefix or the domain.
 * If no language is found there, instead of falling back to the
 * LOCALE_LANGUAGE_NEGOTIATION_URL_FALLBACK provider (which would return the
 * site default language), we attempt to get the language from the cookie first.
 *
 * See locale_language_types_info() and https://drupal.org/node/1497272.
 */
function language_cookie_language_types_info_alter(&$language_types) {
  require_once DRUPAL_ROOT . '/includes/locale.inc';
  $language_types[LANGUAGE_TYPE_URL]['fixed'] = array(
    LOCALE_LANGUAGE_NEGOTIATION_URL,
    LANGUAGE_COOKIE_NEGOTIATION,
    LOCALE_LANGUAGE_NEGOTIATION_URL_FALLBACK,
  );
}

/**
 * Set cookie for current language.
 * If no parameter is passed the current language is used.
 *
 * @param string $lang
 */
function language_cookie_set($lang = NULL) {
  if (!$lang) {
    global $language;
    $lang = $language->language;
  }
  $cookie = new stdClass();
  $cookie->name = variable_get('language_cookie_param', 'language');
  $cookie->value = $lang;
  $cookie->expire = variable_get('language_cookie_time', 31536000);
  $cookie->path = variable_get('language_cookie_path', '/');
  $cookie->domain = variable_get('language_cookie_domain', '');
  $cookie->secure = FALSE;
  $cookie->httponly = FALSE;

  // Allow other modules to alter the cookie. Include bootstrap modules
  // only as this is being called during hook_boot().
  // @see bootstrap_invoke_all()
  foreach (module_list(FALSE, TRUE) as $module) {
    drupal_load('module', $module);

    // Note: this will be executed during hook_boot(), so be careful not
    // to use module_implements() directly or indirectly during
    // hook_language_cookie_alter().
    module_invoke($module, 'language_cookie_alter', $cookie);
  }
  setrawcookie($cookie->name, rawurlencode($cookie->value), REQUEST_TIME + $cookie->expire, $cookie->path, $cookie->domain, $cookie->secure, $cookie->httponly);
}

/**
 * Identify language from a cookie parameter.
 *
 * @param $languages
 *   An array of valid language objects.
 *
 * @return
 *   A valid language code on success, FALSE otherwise.
 */
function language_cookie_language($languages) {
  $param = variable_get('language_cookie_param', 'language');
  return isset($_COOKIE[$param]) && in_array($_COOKIE[$param], array_keys($languages)) ? $_COOKIE[$param] : FALSE;
}

/**
 * Implements hook_boot().
 */
function language_cookie_boot() {
  require_once DRUPAL_ROOT . '/includes/language.inc';

  // Do not set cookie if not configured in Language Negotiation.
  if (!language_negotiation_get_any(LANGUAGE_COOKIE_NEGOTIATION)) {
    return;
  }

  // Do not set cookie on AJAX requests (ie. Admin_menu).
  if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    return;
  }

  // Do not set cookie on blacklisted paths:
  require_once DRUPAL_ROOT . '/' . variable_get('path_inc', 'includes/path.inc');
  require_once DRUPAL_ROOT . '/includes/unicode.inc';
  $language_selection = variable_get('language_selection_page_path', 'language_selection');
  if ($blacklist_pages = variable_get('language_cookie_blacklisted_paths', $language_selection)) {
    $blacklist_pages = drupal_strtolower($blacklist_pages);
    $current_path = ltrim($_SERVER["REQUEST_URI"], '/');
    if (drupal_match_path($current_path, $blacklist_pages)) {
      return;
    }
  }

  // Get the current request path.
  $request_path = $_SERVER["REQUEST_URI"];

  // Don't run this code if we are accessing anything in the files path.
  $public_files_path = variable_get('file_public_path', conf_path() . '/files');
  if (strpos($request_path, $public_files_path) === 0) {
    return;
  }
  if (strpos($request_path, 'cdn/farfuture') === 0) {
    return;
  }
  if (strpos($request_path, 'httprl_async_function_callback') === 0) {
    return;
  }

  // Get current language
  $lang = _language_cookie_get_language();
  if (isset($lang->language)) {
    $lang = $lang->language;
    $param = variable_get('language_cookie_param', 'language');
    if (!isset($_COOKIE[$param]) || isset($_COOKIE[$param]) && $_COOKIE[$param] != $lang || variable_get('language_cookie_set_on_every_pageload', FALSE)) {
      language_cookie_set($lang);
    }
  }
}

/**
 * Language negotiation callback which is here for legacy reasons (so we don't
 * get a fatal error when updating this module).
 */
function language_cookie_language_rewrite(&$path, &$options) {

  // This code has been removed.
}

/**
 * Language negotiation callback which is here for legacy reasons (so we don't
 * get a fatal error when updating this module).
 */
function language_cookie_language_switcher($type, $path) {

  // This code has been removed.
}

/**
 * @see language_initialize.
 */
function _language_cookie_get_language() {
  require_once DRUPAL_ROOT . '/includes/locale.inc';

  // We assume the interface language will be used to set the cookie.
  // If you want to use another language type instead (ie. content/URL),
  // you can use the "language_cookie_language_type" variable.
  $type = variable_get('language_cookie_language_type', LANGUAGE_TYPE_INTERFACE);

  // Execute the language negotiation providers in the order they were set up and return the
  // first valid language found.
  $negotiation = variable_get("language_negotiation_{$type}", array());
  foreach ($negotiation as $provider_id => $provider) {

    // Do not consider language providers with a lower priority than the cookie
    // language provider, nor the cookie provider itself.
    if ($provider_id == LANGUAGE_COOKIE_NEGOTIATION) {
      return FALSE;
    }
    $language = language_provider_invoke($provider_id, $provider);
    if ($language) {
      $language->provider = $provider_id;
      return $language;
    }
  }

  // If no other language was found use the default one.
  $language = language_default();
  $language->provider = LANGUAGE_NEGOTIATION_DEFAULT;
  return $language;
}

Functions

Namesort descending Description
language_cookie_boot Implements hook_boot().
language_cookie_language Identify language from a cookie parameter.
language_cookie_language_negotiation_info Implements hook_language_negotiation_info().
language_cookie_language_rewrite Language negotiation callback which is here for legacy reasons (so we don't get a fatal error when updating this module).
language_cookie_language_switcher Language negotiation callback which is here for legacy reasons (so we don't get a fatal error when updating this module).
language_cookie_language_types_info_alter Alters hook_language_types_info().
language_cookie_menu Implements hook_menu().
language_cookie_set Set cookie for current language. If no parameter is passed the current language is used.
_language_cookie_get_language

Constants

Namesort descending Description
LANGUAGE_COOKIE_NEGOTIATION Constant for language negotiation type.