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

}<br />

if (module_implements('node_grants')) {<br />

$query = db_select('node_access');<br />

$query->addExpression('1');<br />

$query->condition('grant_' . $op, 1, '>=');<br />

$nids = db_or()->condition('nid', $node->nid);<br />

if ($node->status) {<br />

$nids->condition('nid', 0);<br />

}<br />

$query->condition($nids);<br />

$query->range(0, 1);<br />

$grants = db_or();<br />

foreach (node_access_grants($op, $account) as $realm => $gids) {<br />

foreach ($gids as $gid) {<br />

$grants->condition(db_and()<br />

->condition('gid', $gid)<br />

->condition('realm', $realm)<br />

);<br />

}<br />

}<br />

if (count($grants) > 0) {<br />

$query->condition($grants);<br />

}<br />

$result = (bool) $query<br />

->execute()<br />

->fetchField();<br />

$rights[$account->uid][$cid][$op] = $result;<br />

return $result;<br />

}<br />

elseif (is_object($node) && $op == 'view' && $node->status) {<br />

// If no modules implement hook_node_grants(), the default<br />

// behavior is to allow all users to view published nodes,<br />

// so reflect that here.<br />

$rights[$account->uid][$cid][$op] = TRUE;<br />

return TRUE;<br />

}<br />

Here we get to the heart of the Node Access API. The key is in the function<br />

node_access_grants(), which defines the permissions for the current user for the<br />

current operation. <strong>Module</strong>s respond to this function using hook_node_grants(),<br />

which we will examine in detail a little later.<br />

[ 249 ]

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

Saved successfully!

Ooh no, something went wrong!