You are here

language_access.module in Language access 6

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

File

language_access.module
View source
<?php

/**
 * Implementation of hook_init().
 */
function language_access_init() {
  $path = $_GET['q'];

  // 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;
      }
    }
  }
}

/**
 * Implements hook_perm().
 */
function language_access_perm() {
  $perms = array();
  $languages = language_list('enabled');
  foreach ($languages[1] as $language) {
    array_push($perms, 'access language ' . $language->language, 'create language ' . $language->language);
  }
  return $perms;
}

/*
 * hook_translation_link_alter()
 * hide language from language switcher
 */
function language_access_translation_link_alter(&$links, $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]);
      }
    }
  }
}

/*
 * hook_link_alter()
 * hide from node links
 */
function language_access_link_alter(&$links, $node, $comment = NULL) {
  $languages = language_list('enabled');
  foreach ($languages[1] as $language) {
    if (!user_access('access language ' . $language->language)) {
      if (isset($links['node_translation_' . $language->language])) {
        unset($links['node_translation_' . $language->language]);
      }
    }
  }
}

/**
 * Implements hook_form_alter().
 */
function language_access_form_alter(&$form, &$form_state, $form_id) {
  if ($form['#id'] == 'node-form') {
    $form['#after_build'][] = '_language_access_form_node_form_alter';
  }
}

/**
 * Implements hook_form_alter().
 * Called by language_access_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.
  $default_value = $form['language']['#default_value'];
  if (!empty($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;
}