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.

Chapter 8<br />

if (!flood_is_allowed('contact', $limit, $window) &&<br />

!user_access('administer contact forms')) {<br />

drupal_set_message(t("You cannot send more than %limit messages<br />

in @interval. Please try again later.", array('%limit' => $limit, '@<br />

interval' => format_interval($window))), 'error');<br />

drupal_access_denied();<br />

drupal_exit();<br />

}<br />

The preceding code stops all processing of the contact form page if the user is<br />

suspected of spamming the form. If the user violates the threshold of allowed<br />

messages per hour, a warning message is delivered and the Access denied<br />

page is rendered.<br />

Note the use of drupal_exit() here to stop the rest of the page execution. Since this<br />

access denied message is performed during form definition, drupal_exit() must be<br />

invoked to stop the rest of the rendering process.<br />

Note: Do not call the normal PHP exit() function from within <strong>Drupal</strong><br />

code. Doing so may stop the execution of internal functions (such as<br />

session handling) or API hooks. The drupal_exit() function is<br />

provided to safely stop the execution of a <strong>Drupal</strong> request.<br />

Within a normal page context, however, we should return the constant<br />

MENU_ACCESS_DENIED instead of drupal_exit(). We might do this instead<br />

of using a custom menu callback. Returning to our earlier example:<br />

/**<br />

* Implement hook_menu().<br />

*/<br />

function example_menu() {<br />

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

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

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

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

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

'type' => MENU_LOCAL_TASK,<br />

);<br />

return $items;<br />

}<br />

/**<br />

* Custom page callback for a user tab.<br />

*/<br />

[ 225 ]

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

Saved successfully!

Ooh no, something went wrong!