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

The return value from the callback is an array with two keys: path, which is the<br />

<strong>Drupal</strong> path where the entity lives, and options, which defines other parameters to<br />

the url() and l() functions for things such as page anchors or GET query values. It<br />

is safe to omit the options key if it's not needed. Although our implementation is<br />

trivial, alternative implementations could, for instance, put all entities of a given<br />

type on a single page and have an anchor for each one.<br />

When creating a link to an entity, always use entity_uri($type,<br />

$entity) to generate the parameters to pass to either the url() or<br />

l() functions.<br />

Note that these array keys assume we are using the default controller for our entity.<br />

A "controller" is an object that handles the loading of the entity object for us. The<br />

controller is defined as a PHP class, and can be written to load our entity from<br />

anywhere, not just the local database. In our case, we are defining a custom controller<br />

called ArtworkController that will extend from <strong>Drupal</strong>DefaultEntityController,<br />

so it uses the same keys. <strong>Drupal</strong>DefaultEntityController is a generic controller<br />

for entities that are stored in the local database and behave, more or less, like nodes. If<br />

we were pulling data from an entirely different system we would implement our own<br />

controller from scratch that implements the <strong>Drupal</strong>EntityControllerInterface<br />

interface, and we might then need different keys defined in the entity hook.<br />

Two other important keys are the cacheable and fieldable flags:<br />

• static cache indicates that the controller should keep a copy of an entity in<br />

memory after it's been requested so that if we try to load it a second time in<br />

the same page request we can just use that cached copy.<br />

• fieldable indicates to the Field API that we can attach fields to this entity,<br />

in the same fashion as nodes. That's very important, as it is one of the main<br />

reasons to define a new entity type in the first place.<br />

The second part of the hook is a little more involved. It is primarily there to support<br />

the Field API, which will be covered in the next chapter. Since we have multiple<br />

bundles, we need to tell the Field API what bundles we have and at what paths to<br />

put the extra field management interfaces for our entity. To do that, we define, for<br />

each of our bundles, a label that is shown to the user and the menu information that<br />

the Field API will need to add itself into the menu. The keys here, under admin, are<br />

fairly self explanatory. path defines the path that should be used in hook_menu()<br />

for the Field UI's pages, while real path is the exact path that should be used when<br />

generating links within the admin interface.<br />

[ 159 ]

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

Saved successfully!

Ooh no, something went wrong!