hook_node_grants

Versions
4.6 – 4.7
hook_node_grants($user, $op)
5 – 7
hook_node_grants($account, $op)

Grant access to nodes.

This hook is for implementation by node access modules. In addition to managing entries in the node_access table in the database, such modules must implement this hook to inform Drupal which "grants" the current user has. For example, in a role-based access scheme, this function would return a list of the roles the user belongs to.

For a detailed example, see node_access_example.module.

Parameters

$user The user object whose grants are requested.

$op The node operation to be performed, such as "view", "update", or "delete".

Return value

An array whose keys are "realms" of grants such as "user" or "role", and whose values are linear lists of grant IDs.

Related topics

Code

developer/hooks/core.php, line 667

<?php
function hook_node_grants($user, $op) {
  $grants = array();
  if ($op == 'view') {
    if (user_access('access content')) {
      $grants[] = 0;
    }
    if (user_access('access private content')) {
      $grants[] = 1;
    }
  }
  if ($op == 'update' || $op == 'delete') {
    if (user_access('edit content')) {
      $grants[] = 0;
    }
    if (user_access('edit private content')) {
      $grants[] = 1;
    }
  }
  return array('example' => $grants);
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.