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.

Chapter 6<br />

'access arguments' => array('delete artworks'),<br />

'weight' => 1,<br />

'type' => MENU_LOCAL_TASK,<br />

'context' => MENU_CONTEXT_INLINE,<br />

);<br />

}<br />

// ...<br />

This menu item calls drupal_get_form directly, specifically loading the artwork_<br />

delete_confirm form created by the function of the same name:<br />

function artwork_delete_confirm($form, &$form_state, $artwork) {<br />

$form['#artwork'] = $artwork;<br />

// Always provide entity id in the same form key as in the entity<br />

edit form.<br />

$form['aid'] = array('#type' => 'value', '#value' => $artwork->aid);<br />

return confirm_form($form,<br />

t('Are you sure you want to delete %title?', array('%title' =><br />

$artwork->title)),<br />

'artwork/' . $artwork->aid,<br />

t('This action cannot be undone.'),<br />

t('Delete'),<br />

t('Cancel')<br />

);<br />

}<br />

Rather than build a complete form, we will simply pass data on to a utility function<br />

of the Form API called confirm_form(). confirm_form() takes a number of<br />

parameters: a form array, a question to ask, a path to redirect to if the user changes<br />

his mind, a label for the Yes I mean it button, and a label for the No, I changed my<br />

mind link.<br />

The only form information we need is the artwork to be deleted. Neither will be<br />

displayed but we need to pass them along to the submit callback. The rest of the<br />

parameters are simply text to display to the user.<br />

If the user submits the confirmation form, then we know he really means it. The<br />

submit callback handles deleting the artwork:<br />

function artwork_delete_confirm_submit($form, &$form_state) {<br />

if ($form_state['values']['confirm']) {<br />

$artwork = artwork_load($form_state['values']['aid']);<br />

artwork_delete($form_state['values']['aid']);<br />

[ 179 ]

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

Saved successfully!

Ooh no, something went wrong!