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 />

Domain Access keeps track of two separate realms, but sets all three grant operations<br />

to TRUE for each node. So this table stores just the data necessary to rebuild the node<br />

access table.<br />

To ensure that your data is present during rebuild, your module should implement<br />

hook_node_load(). This will ensure that the data required by your implementation<br />

of hook_node_access_records() is available to you.<br />

It is important to load this data in hook_node_load() rather than<br />

inside hook_node_access_records() for the following reason. Other<br />

modules may wish to act based on your data – particularly modules<br />

that implement hook_node_access_records_alter(). While<br />

hook_node_load() allows the $node object to be altered and extended,<br />

hook_node_access_records() does not. So it is your module's<br />

responsibility to ensure that the data used by your node access logic is<br />

loaded onto the $node object properly.<br />

Since the Role Access module can always access $node->uid to derive its data, we<br />

won't worry about data storage for our module.<br />

Modifying the behavior of other modules<br />

Our choice of Role Access as a sample module was deliberate for two reasons: first,<br />

we can ignore the data storage issue discussed above; second, the role system gives<br />

us a good opportunity to look at how other modules may modify the behavior of<br />

node access modules.<br />

If you saved and installed the Role Access code to this point, you will see that it<br />

works just fine, but with two potential issues.<br />

• For most sites, anonymous users are not allowed to create content but they<br />

are allowed to view content. Since Role Access restricts the View operation<br />

to users with the same role, this would mean that anonymous users cannot<br />

view any content.<br />

• All custom roles are also tagged as authenticated users (role id 2). This means<br />

that any content created by someone in an 'administrator' role would also be<br />

tagged for authenticated users. This seems too permissive.<br />

We could write some logic into the Role Access module to handle these use cases,<br />

but it may also be the case that the default functionality is proper. So rather than<br />

edit the module or create some special module settings and exception handling, in<br />

<strong>Drupal</strong> 7 we can write a simple extension module that will modify the behavior of<br />

the parent module.<br />

[ 274 ]

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

Saved successfully!

Ooh no, something went wrong!