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.

Working with Content<br />

watchdog('artwork', '@type: deleted %title.', array('@type' =><br />

$artwork->type, '%title' => $artwork->title));<br />

}<br />

$types = artwork_types();<br />

drupal_set_message(t('@type %title has been deleted.', array(<br />

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

'%title' => $artwork->title)));<br />

}<br />

$form_state['redirect'] = '';<br />

Just to be certain, we first confirm that the $form_state['values']['confirm']<br />

property is set to TRUE. If so, the first thing we need to do is load the artwork. That's<br />

not really necessary for the deletion operation but we need the artwork object for<br />

displaying messages to the user and recording log messages. Then all we do is call<br />

artwork_delete().<br />

It should be no surprise that artwork_delete() is another simple wrapper:<br />

function artwork_delete($aid) {<br />

return artwork_delete_multiple(array($aid));<br />

}<br />

function artwork_delete_multiple($aids) {<br />

return entity_get_controller('artwork')->delete($aids);<br />

}<br />

Like loading, deletion is a multi-value operation. We can delete an arbitrary number<br />

of artworks in one operation, and one is just a special case of many. The heavy lifting<br />

is again handed off to the controller:<br />

public function delete($aids) {<br />

if (!empty($aids)) {<br />

$artworks = $this->load($aids, array());<br />

$transaction = db_transaction();<br />

try {<br />

db_delete('artwork')<br />

->condition('aid', $aids, 'IN')<br />

->execute();<br />

db_delete('artwork_revision')<br />

->condition('aid', $aids, 'IN')<br />

->execute();<br />

[ 180 ]

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

Saved successfully!

Ooh no, something went wrong!