You are here

language_selection_page.module in Language Selection Page 7.2

File

language_selection_page.module
View source
<?php

// @codingStandardsIgnoreStart
define('LANGUAGE_NEGOTIATION_SELECTION_PAGE', 'language-selection-page');
define('LANGUAGE_SELECTION_PAGE_TEMPLATE_IN_THEME', 32);
define('LANGUAGE_SELECTION_PAGE_TEMPLATE_ONLY', 64);
define('LANGUAGE_SELECTION_PAGE_BLOCK', 128);

// @codingStandardsIgnoreEnd
require_once DRUPAL_ROOT . '/includes/locale.inc';

/**
 * 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,
  );
  if (LANGUAGE_SELECTION_PAGE_BLOCK != variable_get('language_selection_page_redirect_type', LANGUAGE_SELECTION_PAGE_TEMPLATE_ONLY)) {
    $language_selection_page_url = variable_get('language_selection_page_path', 'language_selection');
    $items[$language_selection_page_url] = 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 = array_slice(explode('/', trim($_SERVER['REQUEST_URI'], '/')), 0);
  if (LANGUAGE_SELECTION_PAGE_BLOCK == variable_get('language_selection_page_redirect_type', LANGUAGE_SELECTION_PAGE_TEMPLATE_ONLY) || $path[0] == variable_get('language_selection_page_path', 'language_selection')) {
    $_POST['globalredirect'] = 'Disabled on language selection page.';
  }
}

/**
 * Implements hook_language_negotiation_info().
 */
function language_selection_page_language_negotiation_info() {
  return array(
    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',
    ),
  );
}

/**
 * Language selection callback method.
 *
 * This callback is called prior calling the callback defined in hook_menu.
 * It should normally return a language code but instead of that, we redirect
 * the user to the language selection page.
 *
 * This function is not a hook but a callback and I think it has its place in
 * the .module file.
 */
function language_selection_page_language($languages) {

  // 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;
  }
  $path = array_slice(explode('/', trim($_SERVER['REQUEST_URI'], '/')), 0);
  $request_path = implode('/', $path);

  // 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 FALSE;
  }
  if (strpos($request_path, 'cdn/farfuture') === 0) {
    return FALSE;
  }
  if (strpos($request_path, 'httprl_async_function_callback') === 0) {
    return FALSE;
  }

  // Don't run this code on the language selection page itself.
  require_once './includes/common.inc';
  if ($path[0] === variable_get('language_selection_page_path', 'language_selection')) {
    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;
  }

  // Check the path against a list of paths where that the module shouldn't run
  // on.
  // This list of path is configurable on the admin page.
  require_once './includes/path.inc';
  require_once './includes/unicode.inc';
  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;
    }
  }

  // Check if the ignore "language neutral" option is checked.
  // If so, we will check if the entity language is set to LANGUAGE_NONE.
  // Checking also for content type translation options since node can have the
  // default language set instead of LANGUAGE_NONE.
  if (variable_get('language_selection_page_ignore_language_neutral', FALSE)) {
    require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
    $entity = menu_get_object();
    if (isset($entity) && (isset($entity->language) && $entity->language == LANGUAGE_NONE || variable_get('language_content_type_' . $entity->type, '') === '0')) {
      return FALSE;
    }
  }

  // Do not return any language if we use the Drupal's block method
  // to display the redirection.
  // Be aware that this will automatically assign the default language.
  if (LANGUAGE_SELECTION_PAGE_BLOCK === variable_get('language_selection_page_redirect_type', LANGUAGE_SELECTION_PAGE_TEMPLATE_ONLY)) {
    return FALSE;
  }

  // Redirect to the language selection page properly.
  $language_selection_page_url_elements[] = variable_get('language_selection_page_path', 'language_selection');
  $language_selection_page_url_elements[] = $request_path ? $request_path : NULL;
  $language_selection_page_url = trim(implode('/', $language_selection_page_url_elements), '/');
  if (empty($GLOBALS['language']->provider)) {
    drupal_goto($language_selection_page_url, array(
      'absolute' => TRUE,
      'language' => LANGUAGE_NONE,
    ));
  }
  return FALSE;
}

/**
 * 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_block_info().
 */
function language_selection_page_block_info() {
  $blocks = array();
  if (LANGUAGE_SELECTION_PAGE_BLOCK === variable_get('language_selection_page_redirect_type', LANGUAGE_SELECTION_PAGE_TEMPLATE_ONLY)) {
    $blocks['block'] = array(
      'info' => t('Language Selection Block'),
      'cache' => DRUPAL_NO_CACHE,
    );
  }
  return $blocks;
}

/**
 * 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', 'includes/language_selection_page.helpers');
  if (language_selection_page_is_negotiation_detected()) {
    return array();
  }
  $block = array();
  switch ($delta) {
    case 'block':
      $block['subject'] = '';
      $block['content'] = array(
        '#theme' => 'language_selection_page_body',
        '#language_selection_page' => language_selection_page_selection_page_data(),
      );
      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();
  }
}