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.

<strong>Drupal</strong> Permissions and Security<br />

$items['user/%user/content'] = array(<br />

'title' => 'Content creation',<br />

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

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

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

'access arguments' => array('view content creation permissions',<br />

'administer users'),<br />

'type' => MENU_LOCAL_TASK,<br />

);<br />

return $items;<br />

}<br />

For this code to succeed, we must also provide the new function<br />

example_user_access():<br />

/**<br />

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

*<br />

* Takes a list of permissions and requires that all return<br />

* TRUE.<br />

*/<br />

function example_user_access() {<br />

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

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

return FALSE;<br />

}<br />

}<br />

return TRUE;<br />

}<br />

Note: When developing your module, you must rebuild the menu<br />

cache in order to see permission changes. You can do this by<br />

emptying the cache_menu table, or using the tools provided by<br />

Devel module or Drush.<br />

You can even dictate more complex logic within an access control function. Suppose<br />

we also want to prevent users from viewing pages other than their own. We would<br />

edit the functions in the following way:<br />

/**<br />

* Implement hook_menu().<br />

*/<br />

function example_menu() {<br />

$items['user/%user/content'] = array(<br />

'title' => 'Content creation',<br />

[ 222 ]

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

Saved successfully!

Ooh no, something went wrong!