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

As a general rule, large, rarely used classes should be placed into a<br />

separate file while smaller or very frequently used classes should be<br />

left in the .module file to avoid the overhead of finding the class when<br />

needed. Additionally, classes that are typically used together can be<br />

placed into a single file so that we'll need to load only a single file.<br />

Let's create a new file in our module called artwork.controller.inc. Next, add that<br />

file to the files[] array in the artwork.info file. In artwork.controller.inc, let's<br />

start with just the following code:<br />

class ArtworkController extends <strong>Drupal</strong>DefaultEntityController {<br />

}<br />

Now when our module is enabled, <strong>Drupal</strong> will scan all files in the files[] array<br />

in artwork.info, find the ArtworkController class, and cache its location. Later<br />

on, when some code tries to create a new instance of ArtworkController it will<br />

lazy-load the artwork.controller.inc file, making the class available to be used.<br />

Naturally the ArtworkController class needs to actually do something. We will add<br />

additional methods to it as we go. There is already a load() method, inherited from<br />

<strong>Drupal</strong>DefaultEntityController, as well as several others.<br />

Most <strong>Drupal</strong> code prefers to work procedurally, however, even if the engine under<br />

the hood is object-oriented. Therefore, like the node module we will provide a set<br />

of utility API functions for us and other module developers to use.<br />

function artwork_load($aid = NULL, $vid = NULL, $reset = FALSE) {<br />

$aids = (isset($aid) ? array($aid) : array());<br />

$conditions = (isset($vid) ? array('vid' => $vid) : array());<br />

$artwork = artwork_load_multiple($aids, $conditions, $reset);<br />

return $artwork ? reset($artwork) : FALSE;<br />

}<br />

function artwork_load_multiple($aids = array(), $conditions = array(),<br />

$reset = FALSE) {<br />

return entity_load('artwork', $aids, $conditions, $reset);<br />

}<br />

Note that we're actually passing all of the loading logic back to the entity_load()<br />

function, which in turn will create a new instance of the ArtworkController as<br />

needed and call the load() method on it. The load() method assumes that all<br />

operations are multi-load. In fact, the entity system assumes multi-object operations<br />

wherever possible. That makes loading multiple objects at the same time much<br />

cheaper as we can load them all at once, while loading a single object is the exact<br />

same operation. "One" is a special case of "many". We can now load one or a dozen<br />

artwork objects with equal ease.<br />

[ 162 ]

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

Saved successfully!

Ooh no, something went wrong!