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.

}<br />

Chapter 8<br />

'page callback' => 'example_user_page',<br />

'page arguments' => array(1),<br />

'access callback' => 'example_user_access',<br />

'access arguments' => array(1,<br />

'view content creation permissions', 'administer users'),<br />

'type' => MENU_LOCAL_TASK,<br />

);<br />

return $items;<br />

/**<br />

* Access callback that checks multiple permissions.<br />

*/<br />

function example_user_access() {<br />

global $user;<br />

$arguments = func_get_args();<br />

$account = array_shift($arguments);<br />

if ($account->uid != $user->uid) {<br />

return FALSE;<br />

}<br />

foreach ($arguments as $permission) {<br />

if (!user_access($permission)) {<br />

return FALSE;<br />

}<br />

}<br />

return TRUE;<br />

}<br />

In the preceding way, complex access rules may be enforced on menu callbacks.<br />

When writing an access check outside of the menu system, it is tempting<br />

to chain together a series of IF checks to produce the logic required.<br />

Consider moving such statements into a clearly defined access function.<br />

These can improve the readability and portability of your code. If you<br />

want extra style points, consider adding a drupal_alter() function<br />

before returning TRUE or FALSE to allow other modules to rewrite your<br />

standard logic.<br />

[ 223 ]

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

Saved successfully!

Ooh no, something went wrong!