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.

Chapter 6<br />

In our case, we defined the bundle to use the type property so our artwork types<br />

have a property called type that contains the machine name of the bundle. A<br />

property called name for the human-friendly name of the bundle is a standard<br />

convention but not strictly required, as is a human-readable description property.<br />

The artwork_type_load() function is necessary for the menu placeholder to work,<br />

but is also a very nice convenience function to have available as well. Generally it<br />

is good practice to provide clean, flexible APIs to any system we develop, even if<br />

we don't expect to use them. Odds are that we'll either find a use for them later or<br />

someone else will think of a use we never expected.<br />

Note that we are replacing dashes in a type name with underscores. That's because,<br />

by convention, all <strong>Drupal</strong> paths use dashes in place of underscores but bundle names<br />

need to use underscores, not dashes. When we use a type name in a URL we will<br />

always use a dash, and so here we first fold the dash back to an underscore to make<br />

sure we find the correct artwork type.<br />

There is one other important detail here, and that is the drupal_static() function.<br />

That function acts as a central collector for static PHP variables, that is, those that are<br />

not technically global but should persist between calls to a function. They are quite<br />

commonly used as a lightweight cache to avoid re-processing or refetching the same<br />

data within the same page request, but that can in some cases lead to weird side<br />

effects when the data being cached changes mid-request, such as when writing<br />

unit tests.<br />

The drupal_static() function acts as a central collector for such static variables.<br />

By putting all such static variables in one place and giving the variable a name that<br />

matches our function (that's what the __FUNCTION__ PHP constant means), we allow<br />

systems that need to forcibly reset static caches without having a separate $reset<br />

parameter for every part of the system.<br />

Use the drupal_static() function to cheaply cache data<br />

structures for one page request. Don't cache data that is too large<br />

and too cheap to regenerate, though, as it does use up memory.<br />

The entity controller<br />

In the entity info hook we declared that we were going to use our own controller<br />

class. That means we need to provide one. However, a controller class may not<br />

always be small, and if it's only rarely used we don't want to parse that code on all<br />

pages. Fortunately PHP provides a way to load class definitions on demand, and<br />

<strong>Drupal</strong> makes it very easy to expose classes to PHP's autoloader.<br />

[ 161 ]

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

Saved successfully!

Ooh no, something went wrong!