18.10.2016 Views

Drupal 7 Module Development

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 9<br />

A few things to consider when returning your node grants.<br />

• Your module may assert one or more 'realms' as appropriate to your<br />

business logic.<br />

• The 'realm' must be a string unique to your module. Namespace your grant<br />

with the name of the module. If you only store one grant, use the name of the<br />

module as the realm.<br />

• The three grants are each checked separately to determine permissions. This<br />

means that you may define all three grants (view, update and delete) in a<br />

single statement.<br />

• The 'priority' element is deprecated for <strong>Drupal</strong> 7. It can be used to discard<br />

the grants set by other modules. However, this is best done through the<br />

new hook_node_access_records_alter(). (See http://drupal.org/<br />

node/686858 for details.)<br />

• Your grants declarations must be integers (0 or 1) and not Boolean TRUE or<br />

FALSE. <strong>Drupal</strong> 7's database layer uses stricter variable typing than <strong>Drupal</strong> 6<br />

and below, so be sure to update your legacy code.<br />

• The {node_access} table does not distinguish between published and<br />

unpublished nodes. Only trusted users should be given permission to access<br />

unpublished content.<br />

We have now established our rules in the database. Let's examine the second part of<br />

the node access system.<br />

Right now, if we save a node with our module in this form, nothing<br />

happens. Why? Because the Node Access API assumes that your<br />

module also implements hook_node_grants(). Without that<br />

hook, your records will not be stored. <strong>Drupal</strong> does this to save<br />

database overhead associated with storing unused records.<br />

Using hook_node_grants()<br />

For every page request involving nodes, <strong>Drupal</strong> queries the enabled modules for<br />

the node access grants that are in force for the current user. <strong>Module</strong>s respond to this<br />

query using hook_node_grants().<br />

Unlike hook_node_access_records(), which is node-centric, hook_node_grants()<br />

is user-centric. The hook passes two parameters:<br />

• $account – the user object of the person viewing the page.<br />

• $op – the operation being performed (view, update or delete).<br />

[ 269 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!