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.

Working with Content<br />

drupal_set_title(t('Edit @type @title', array('@type' =><br />

$types[$artwork->type]->name, '@title' => $artwork->title)),<br />

PASS_THROUGH);<br />

}<br />

return drupal_get_form($artwork->type . '_artwork_form', $artwork);<br />

The edit page callback is simple enough that we could almost get away with not<br />

having it at all and just calling the form directly from the menu system. However,<br />

since the form name is dynamic based on the artwork type we have to have a<br />

small function here. Note too that we're setting a dynamic title and again allowing<br />

HTML through.<br />

That's it! The form we built earlier handles both new and existing artworks just the<br />

same, so editing an artwork, including saving new revisions, is done.<br />

Deleting an artwork<br />

The final missing piece is the ability to delete an artwork. Naturally we don't want to<br />

let users do so on a whim, so we'll give them a confirmation form.<br />

Recall earlier that in our artwork edit form we had a Delete button with its own<br />

submit handler. First we need to define that submit handler:<br />

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

$destination = array();<br />

if (isset($_GET['destination'])) {<br />

$destination = drupal_get_destination();<br />

unset($_GET['destination']);<br />

}<br />

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

$form_state['redirect'] = array('artwork/' . $artwork->aid . '/<br />

delete', array('query' => $destination));<br />

}<br />

All this submit handler does is redirect the user to artwork/$aid/delete. The rest<br />

of the code there is simply to handle <strong>Drupal</strong>'s page redirect system, which we can<br />

largely copy and paste. At that path we'll put a confirmation form so that users have<br />

to confirm they really mean to delete an artwork:<br />

function artwork_menu() {<br />

$items['artwork/%artwork/delete'] = array(<br />

'title' => 'Delete',<br />

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

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

[ 178 ]

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

Saved successfully!

Ooh no, something went wrong!