You are here

language_selection_page.module in Language Selection Page 7

File

language_selection_page.module
View source
<?php

define('LANGUAGE_NEGOTIATION_SELECTION_PAGE', 'language-selection-page');

// theme settings - numeric values for D6 upgrade purposes.
define('LANGUAGE_SELECTION_PAGE_TEMPLATE_IN_THEME', 32);
define('LANGUAGE_SELECTION_PAGE_TEMPLATE_ONLY', 64);
define('LANGUAGE_SELECTION_PAGE_BLOCK', 128);

/**
 * Implements hook_menu().
 */
function language_selection_page_menu() {
  $items = array();
  $items['admin/config/regional/language/configure/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 languages',
    ),
    'file' => 'language_selection_page.admin.inc',
    'type' => MENU_VISIBLE_IN_BREADCRUMB,
  );
  $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_boot().
 *
 * Prevents redirect by globalredirect. Putting this in hook_boot so that it
 * executes before globalredirect_init().
 *
 * See https://drupal.org/node/1438584#comment-6167916
 */
function language_selection_page_boot() {
  $path = request_path();
  if ($path == 'language_selection') {
    $_POST['globalredirect'] = 'Disabled on language selection page.';
  }
}

/**
 * Implements hook_language_negotiation_info().
 */
function language_selection_page_language_negotiation_info() {
  $language_providers[LANGUAGE_NEGOTIATION_SELECTION_PAGE] = array(
    'types' => array(
      LANGUAGE_TYPE_CONTENT,
      LANGUAGE_TYPE_INTERFACE,
      LANGUAGE_TYPE_URL,
    ),
    'callbacks' => array(
      'language' => 'language_selection_page_language',
      'switcher' => 'locale_language_switcher_url',
      'url_rewrite' => 'locale_language_url_rewrite_url',
    ),
    'file' => drupal_get_path('module', 'language_selection_page') . '/language_selection_page.module',
    'weight' => 50,
    'name' => t('Selection Page'),
    'description' => t('Show a language selection page to the visitor for making a language choice.'),
    'config' => 'admin/config/regional/language/configure/selection_page',
  );
  return $language_providers;
}

/**
 * Language selection callback method.
 */
function language_selection_page_language($languages) {
  require_once './includes/locale.inc';
  require_once './includes/path.inc';
  require_once './includes/common.inc';
  require_once './includes/unicode.inc';

  // Bail out when running tests on commandline.
  if (drupal_is_cli()) {
    return FALSE;
  }

  // Bail out when handling AJAX requests.
  if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    return FALSE;
  }

  // See if we can remove this check and use $language->provider
  $lang = locale_language_from_url($languages);
  if ($lang) {
    return $lang;
  }

  // Don't run this code on the language selection page itself.
  if (request_path() == 'language_selection') {
    return FALSE;
  }
  if ($blacklist_pages = variable_get('language_selection_page_blacklisted_paths')) {
    $blacklist_pages = drupal_strtolower($blacklist_pages);
    $current_path = drupal_strtolower(drupal_get_path_alias(request_path()));

    // In order to be able to blacklist pages like 'update.php',
    // if current_path is empty, the php REQUEST_URI will be match.
    if (empty($current_path)) {
      $current_path = ltrim($_SERVER["REQUEST_URI"], '/');
    }
    if (drupal_match_path($current_path, $blacklist_pages)) {
      return FALSE;
    }
  }

  // Don't run this code if we are accessing an image.
  $styles_path = variable_get('file_public_path', conf_path() . '/files') . '/styles/';
  if (strpos(request_path(), $styles_path) === 0) {
    return FALSE;
  }

  // @TODO: document this
  if (!isset($_SERVER['SERVER_ADDR'])) {
    return FALSE;
  }

  // Don't run this code if we are accessing another php file than index.php.
  if ($_SERVER['SCRIPT_NAME'] != $GLOBALS['base_path'] . 'index.php') {
    return FALSE;
  }

  // Redirect to the language selection page,
  // with the currently requested page as destination.
  $query = drupal_get_query_parameters();
  $query['destination'] = request_path() ? request_path() : '<front>';
  $language = $GLOBALS['language'];

  // Return FALSE if we use the block method
  if (LANGUAGE_SELECTION_PAGE_BLOCK == variable_get('language_selection_page_redirect_type', LANGUAGE_SELECTION_PAGE_TEMPLATE_ONLY)) {
    return FALSE;
  }
  if (empty($language->provider)) {
    drupal_goto('language_selection', array(
      'query' => $query,
      'absolute' => FALSE,
      'language' => LANGUAGE_NONE,
    ));
  }
}

/**
 * Implements hook_theme().
 */
function language_selection_page_theme() {
  return array(
    'language_selection_page_body' => array(
      'variables' => array(
        'language_selection_page' => array(),
      ),
      'path' => drupal_get_path('module', 'language_selection_page') . '/themes',
      'template' => 'language-selection-page-body',
    ),
  );
}

/**
 * Implements hook_preprocess_page().
 */
function language_selection_page_preprocess_page(&$vars) {
  $vars['theme_hook_suggestions'][] = 'page__language_selection';
}

/**
 * Implements hook_block_info().
 */
function language_selection_page_block_info() {
  return array(
    'block' => array(
      'info' => t('Language Selection Block'),
      'cache' => DRUPAL_NO_CACHE,
    ),
  );
}

/**
 * Implements hook_block_view().
 */
function language_selection_page_block_view($delta = '') {
  if (LANGUAGE_SELECTION_PAGE_BLOCK != variable_get('language_selection_page_redirect_type', LANGUAGE_SELECTION_PAGE_TEMPLATE_ONLY)) {
    return array();
  }
  module_load_include('inc', 'language_selection_page', 'language_selection_page.pages');
  if (!($data = language_selection_page_selection_page_data())) {
    return array();
  }
  $block = array();
  switch ($delta) {
    case 'block':
      $block['subject'] = '';
      $block['content'] = array(
        '#theme' => 'language_selection_page_body',
        '#language_selection_page' => $data,
        'blop' => 'blap',
      );
      break;
  }
  return $block;
}

/**
 * Implements hook_preprocess_HOOK().
 */
function language_selection_page_preprocess_block(&$vars) {
  $block = $vars['block'];
  if ($block->module == 'language_selection_page' && $block->delta == 'block') {
    $vars['language_selection_page'] = language_selection_page_selection_page_data();
  }
}