18.10.2016 Views

Drupal 7 Module Development

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 9<br />

Using the new hook_node_access_records_alter() and hook_node_grants_<br />

alter(), we can fundamentally alter how any other node access module behaves.<br />

To do so, we will create the Role Access Extend module to implement our<br />

optional behaviors.<br />

Using hook_node_grants_alter()<br />

When using node access alter hooks, we must decide: Should we alter what is saved<br />

in the database {node_access} or should we alter how the user interacts with the<br />

stored data? Since we might want to turn this module off, the best solution is to leave<br />

{node_access} alone and instead alter the grants on page load. We do this with the<br />

new <strong>Drupal</strong> 7 hook_node_grants_alter().<br />

hook_node_grants_alter() is a very powerful hook. After <strong>Drupal</strong> has gathered all<br />

the node access permissions set by your site's modules, this hook fires and allows a<br />

module to modify the cumulative $grants array by reference. In structure, the hook<br />

looks much like hook_node_grants(). It passes &$grants, plus the requesting user's<br />

$account object and the requested $op.<br />

To make our first rule work, we need to control the View operation and decide<br />

which user roles may view content as if they were in the authenticated user<br />

role. First, we create our role_access_extend.info file, and then we create a<br />

role_access_extend.module.<br />

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

Saved successfully!

Ooh no, something went wrong!