You are here

content_language_access.module in Content Language Access 6

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

This module provides access checking of the current language of the site with the language of the content (language neutral are not considered).

File

content_language_access.module
View source
<?php

/**
 * @file
 * This module provides access checking of the current language of the site
 * with the language of the content (language neutral are not considered).
 */

/**
 * Implements hook_menu().
 */
function content_language_access_menu() {
  $items['admin/settings/content_language_access'] = array(
    'title' => 'Content language access',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'content_language_access_admin_form',
    ),
    'access arguments' => array(
      'administer content_language_access settings',
    ),
    'file' => 'content_language_access.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implements hook_menu_alter().
 */
function content_language_access_menu_alter(&$items) {

  // Check if we are not overrriding the another access callback.
  $old_callback = $items['node/%node']['access callback'];
  if ($old_callback == 'node_access') {

    // Keep as null if it is already node_access callback.
    $old_callback = NULL;
  }

  // Use _content_language_access_node_access() instead of node_access().
  $items['node/%node']['access callback'] = $items['node/%node/view']['access callback'] = '_content_language_access_node_access';

  // Adding aditional parameter to the access arguments containing the old
  // callback.
  $items['node/%node']['access arguments'] = $items['node/%node/view']['access arguments'] = array(
    'view',
    1,
    NULL,
    $old_callback,
  );
}

/**
 * Implements hook_help().
 */
function content_language_access_help($path, $arg) {
  switch ($path) {

    // Main module help for the content_language_access module.
    case 'admin/help#content_language_access':
      return '<p>' . t('Content Language Access Module restricts the access of only contents with language (except neutral language) that are equal of the actual Drupal language being accessed or others that were previous configured in the <a href="@content_language_access">admin page</a>.', array(
        '@content_language_access' => url('admin/settings/content_language_access'),
      )) . '</p>';

    // Help for admin page for the content_language_access module.
    case 'admin/settings/content_language_access':
      return '<p>' . t('This page provides an interface for configuring more languages that can be accessed from a Drupal language') . '</p>';
  }
}

/**
 * Implements hook_perm().
 */
function content_language_access_perm() {
  return array(
    'administer content_language_access settings',
  );
}

/**
 * Access callback for node/%node.
 *
 * Wrapper around node_access() or another callback with additional checks for
 * language permissions.
 *
 * @global object $user
 * @global object $language
 *
 * @param string $op
 *   The operation to be performed on the node. Possible values are:
 *   - "view"
 *   - "update"
 *   - "delete"
 *   - "create"
 * @param object $node
 *   The node object (or node array) on which the operation is to be performed,
 *   or node type (e.g. 'forum') for "create" operation.
 * @param object $account
 *   Optional, a user object representing the user for whom the operation is to
 *   be performed. Determines access for a user other than the current user.
 * @param string $old_callback
 *   Optional, a function name containing the old callback.
 *
 * @return bool
 *   TRUE if the operation may be performed, or FALSE otherwise.
 *
 * @see node_access()
 */
function _content_language_access_node_access($op, $node, $account = NULL, $old_callback = NULL) {
  global $user;
  global $language;

  // If no user object is supplied, the access check is for the current user.
  if (empty($account)) {
    $account = $user;
  }

  // User #1 has all privileges:
  if ($account->uid == 1) {
    return TRUE;
  }

  // Convert the node to an object if necessary:
  if ($op != 'create') {
    $node = (object) $node;
  }
  if ($old_callback != NULL && function_exists($old_callback)) {
    $access = $old_callback($op, $node, $account);
  }
  else {
    $access = node_access($op, $node, $account);
  }

  // Bypass completely if access returns false.
  if (!$access) {
    return FALSE;
  }

  // Only checks for view permission.
  if ($op != 'view') {
    return TRUE;
  }
  if ($node->language) {

    // Verifies if the current language is the same of the content.
    if ($node->language == $language->language) {
      return TRUE;
    }
    else {

      // Checks the configuration defined in admin page.
      $config = _content_language_access_get_config();
      $actual_language_permission = isset($config[$language->language][$node->language]) ? $config[$language->language][$node->language] : FALSE;
      if ($actual_language_permission) {
        return TRUE;
      }
    }
  }
  else {

    // Language neutral are always allowed.
    return TRUE;
  }
  return FALSE;
}

/**
 * Helper to get all the config available for the module.
 *
 * @return array
 *   All configuration from admin/settings/content_language_access page.
 */
function _content_language_access_get_config() {
  return variable_get('content_language_access', array());
}

Functions

Namesort descending Description
content_language_access_help Implements hook_help().
content_language_access_menu Implements hook_menu().
content_language_access_menu_alter Implements hook_menu_alter().
content_language_access_perm Implements hook_perm().
_content_language_access_get_config Helper to get all the config available for the module.
_content_language_access_node_access Access callback for node/%node.