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

'title' => t('View role-restricted content as authenticated<br />

user'),<br />

'description' => t('Allow anonymous users to view content<br />

created by authenticated users. Lack of this permission removes access<br />

for users in custom roles.'),<br />

),<br />

);<br />

}<br />

So now we have a new permission setting:<br />

We can then use hook_node_grants_alter() to modify the permissions that<br />

anonymous (and other users) have. To do so, we have to understand the format<br />

for the $grants array that is passed to our hook.<br />

<strong>Drupal</strong> gathers these grants with the node_access_grants() function, which<br />

combines all module grants into a single associative array of arrays. The $grants<br />

array keys are the realms of access control; and the array associated to these keys<br />

indicate the grant ids that are active for that realm. A var_dump() of a typical<br />

$grants array looks like so:<br />

array(1) {<br />

["role_access"] => array(1) {<br />

[0] => int(2),<br />

[1] => int(4),<br />

}<br />

}<br />

[ 276 ]

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

Saved successfully!

Ooh no, something went wrong!