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.

Node Access<br />

Now we have an advanced rule set that gives us a great deal of flexibility, and you<br />

have two new hooks in your <strong>Drupal</strong> toolkit.<br />

Testing and debugging your module<br />

Testing and debugging node access modules presents a particular challenge in<br />

<strong>Drupal</strong>, largely because most access rules are user-based. That fact, combined with<br />

user 1's ability to bypass all access checks, means that you cannot test node access<br />

module through the browser while logged in as user 1. Nor can you test while<br />

logged in as any user who has the bypass node access permission, since that<br />

permission causes the entire node access system to be ignored, granting the user<br />

View, Update, and Delete permission to all nodes.<br />

While we don't have space to write up a Simpletest suite for our module here, there<br />

are a few simple tricks you can remember to make your development (and support!)<br />

life easier.<br />

• Never test as user 1 or a user who can bypass node access.<br />

• You can use hook_node_load() and hook_node_view() to append your<br />

modules rule set to the node object for display. If you do so, be sure only to<br />

display this information to trusted users.<br />

• Remember to examine the contents of {node_access} after a node is saved.<br />

Be sure the rules in the table reflect the logic of your code.<br />

• Be sure that the data you need to store your rules is loaded onto the $node<br />

object so you can safely rebuild {node_access} when you need to.<br />

• Be sure to test access to both published and unpublished content.<br />

These guidelines will help, but there is a better, faster, and easier way to debug your<br />

working code.<br />

Using Devel Node Access<br />

The Devel Node Access module is part of the Devel module suite (http://drupal.<br />

org/project/devel). The module is maintained by salvis (http://drupal.org/<br />

user/82964) and gives you a browser-based view into how node access rules are<br />

being enforced on your site.<br />

[ 282 ]

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

Saved successfully!

Ooh no, something went wrong!