You are here

language_access.module in Language access 7

Same filename and directory in other branches
  1. 8 language_access.module
  2. 6 language_access.module

File

language_access.module
View source
<?php

/**
 * Implementation of hook_init().
 */
function language_access_init() {
  $path = current_path();

  // Allow user path to be accessed
  if ($path == 'user' || substr($path, 0, 5) == "user/") {
    return;
  }
  global $language;
  if (!user_access('access language ' . $language->language)) {
    $default_language = language_default();

    // We still want to allow access to default language
    if ($language->language != $default_language->language) {

      // Do not execute on drush
      if (php_sapi_name() != 'cli') {
        drupal_access_denied();
        exit;
      }
    }
  }
}

/**
 * Implementation of hook_permission().
 */
function language_access_permission() {
  $languages = language_list('enabled');
  $perms = array();
  foreach ($languages[1] as $language) {
    $perms['access language ' . $language->language] = array(
      'title' => t('Access language ' . $language->language),
    );
    $perms['create language ' . $language->language] = array(
      'title' => t('Create language ' . $language->language),
    );
  }
  return $perms;
}

/**
 * Implementation of hook_language_switch_links_alter().
 */
function language_access_language_switch_links_alter(&$links, $type, $path) {
  $languages = language_list('enabled');
  foreach ($languages[1] as $language) {
    if (!user_access('access language ' . $language->language)) {
      if (isset($links[$language->language])) {
        unset($links[$language->language]);
      }
    }
  }
}

/**
 * Implements hook_node_view_alter().
 */
function language_access_node_view_alter(&$build) {
  if (isset($build['links']['translation']['#links'])) {
    $links =& $build['links']['translation']['#links'];

    // Check if user has permission to language link and disable it if false
    foreach ($links as $link => $language_code) {
      if (!user_access('access language ' . $language_code['language']->language)) {
        unset($links[$link]);
      }
    }
  }
}

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function language_access_form_node_form_alter(&$form, &$form_state) {
  $form['#after_build'][] = '_language_access_form_node_form_alter';
}

/**
* Implements hook_form_BASE_FORM_ID_alter().
* Called by language_access_form_node_form_alter
*/
function _language_access_form_node_form_alter($form, &$form_state) {

  // Deny access if user has no permissions to create language and language is not undefined.
  if (isset($form['language']['#default_value'])) {
    $default_value = $form['language']['#default_value'];
    if (!user_access('create language ' . $default_value) && $default_value != 'und') {
      drupal_access_denied();
      exit;
    }
  }
  $default_language = language_default();

  // Remove unallowed languages from language list in form
  if (isset($form['language']['#options'])) {
    foreach ($form['language']['#options'] as $lang => $lang_name) {
      if ($lang != 'und' && $lang != $default_language->language) {
        if (!user_access('create language ' . $lang)) {
          unset($form['language']['#options'][$lang]);
        }
      }
    }
  }
  return $form;
}