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 />

}<br />

foreach ($artworks as $artwork_id => $artwork) {<br />

field_attach_delete('artwork', $artwork);<br />

}<br />

db_ignore_slave();<br />

}<br />

catch (Exception $e) {<br />

$transaction->rollback();<br />

watchdog_exception('artwork', $e, NULL, WATCHDOG_ERROR);<br />

return FALSE;<br />

}<br />

module_invoke_all('entity_delete', $artwork, 'artwork');<br />

// Clear the page and block and artwork caches.<br />

cache_clear_all();<br />

$this->resetCache();<br />

}<br />

return TRUE;<br />

Once again we use a transaction to ensure that the entire deletion process either<br />

succeeds or fails. We first load the artwork objects that we are about to delete,<br />

as other operations will require them. We then handle our own deletion logic:<br />

Removing the appropriate records from the artwork and artwork_revision tables. We<br />

then call field_attach_delete() to let the Field system remove any field data that<br />

was associated with those artworks, and finally disable the slave server as before.<br />

There's one other task to take care of; we need to flush the cache. By default <strong>Drupal</strong><br />

caches most page requests for performance. If we've deleted an artwork, however,<br />

we need to remove that page from the cache. There may be many other pages<br />

affected by that deletion, however, so we simply clear the entire page cache with<br />

cache_clear_all(). It will get rebuilt as needed. We also clear the static cache<br />

of loaded artwork objects that the controller maintains so that if later in this same<br />

request someone tries to load the artwork we just deleted they get an error rather<br />

than a ghost object.<br />

And we're done!<br />

[ 181 ]

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

Saved successfully!

Ooh no, something went wrong!