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.

* - "delete"<br />

* - "create"<br />

* @param $node<br />

* The node object on which the operation is to be performed, or<br />

* node type (e.g. 'forum') for "create" operation.<br />

* @param $account<br />

* Optional, a user object representing the user for whom the<br />

* operation is to be performed.<br />

* Determines access for a user other than the current user.<br />

* @return<br />

* TRUE if the operation may be performed, FALSE otherwise.<br />

*/<br />

function node_access($op, $node, $account = NULL) {<br />

global $user;<br />

$rights = &drupal_static(__FUNCTION__, array());<br />

From reading over the code, we can use our knowledge of <strong>Drupal</strong> to infer some<br />

key points:<br />

Chapter 9<br />

• The $op parameter indicates the node operation being requested.<br />

• Creating nodes is a special case, even changing the $node parameter sent to<br />

the function.<br />

• Node Access is a user-driven action. That means it matters who is trying to<br />

perform the operation.<br />

• Node Access in <strong>Drupal</strong> 7 is statically cached per user for the duration of the<br />

page request. That means that once set, it cannot be changed until another<br />

request is sent or drupal_static_reset('node_access') is called.<br />

Recall our discussion of $user and $account in the previous chapter.<br />

The node_access() function accepts an $account object, but falls back<br />

to using the global $user object if one is not supplied. This feature allows<br />

for access checks to be performed for users other than the current user.<br />

A single node may return different answers to an access request depending on who is<br />

making the request and what request is being made.<br />

[ 245 ]

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

Saved successfully!

Ooh no, something went wrong!