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.

Node Access<br />

From here, we are required to build a well-formed $grants array which defines the<br />

rules for our module. This array matches the schema of the {node_access} table and<br />

adds a 'priority' key. For our module, we return an array element for each role:<br />

// Initialize a $grants array.<br />

$grants = array();<br />

// Iterate through the $roles and get our grants.<br />

// We use the role id as the grant id, so let's name it that way for<br />

clarity.<br />

foreach ($roles as $grant_id) {<br />

$grants[] = array(<br />

'realm' => 'role_access', // The name of our module.<br />

'gid' => $grant_id,<br />

'grant_view' => 1,<br />

'grant_update' => 1,<br />

'grant_delete' => 1,<br />

'priority' => 0, // If not zero, other grants are ignored.<br />

);<br />

}<br />

// Return our grants.<br />

return $grants;<br />

}<br />

Inspecting the output of this code shows us:<br />

$grants[0] = array(<br />

'realm' => 'role_access',<br />

'gid' => 2,<br />

'grant_view' => 1,<br />

'grant_update' => 1,<br />

'grant_delete' => 1,<br />

'priority' => 0,<br />

),<br />

$grants[1] = array(<br />

'realm' => 'role_access',<br />

'gid' => 4,<br />

'grant_view' => 1,<br />

'grant_update' => 1,<br />

'grant_delete' => 1,<br />

'priority' => 0,<br />

);<br />

Note that we do not need to identify the node itself. The API handles that for us.<br />

[ 268 ]

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

Saved successfully!

Ooh no, something went wrong!