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.

[ 165 ]<br />

Chapter 6<br />

This callback simply gives us an unordered list of all of the artwork types we have<br />

available. Each one is presented as a link to the manage page for that type. Note that<br />

we are converting the artwork type to use dashes instead of underscores in the path.<br />

That is a <strong>Drupal</strong> standard that we will use as well.<br />

function artwork_type_page_title($type) {<br />

return t('Manage @type', array('@type' => $type->name));<br />

}<br />

We want the title of each page to be a string based on the type that is passed in, so<br />

we need to specify a title callback. The title callback couldn't be simpler:<br />

function artwork_information($artwork_type) {<br />

return $artwork_type->name . ': ' . $artwork_type->description;<br />

}<br />

The summary page for each artwork type is trivial as well, at least in our case.<br />

A more complex entity type would have various configuration options here in a<br />

form, much the way nodes do. In our case, however, we will just show the title<br />

and description of the artwork type.<br />

Believe it or not, that's it. Because we're not allowing any other user configuration<br />

of artwork types ourselves, setting up that minimal skeleton is all we need to do.<br />

However, if the Field UI module is enabled we should see Manage fields and<br />

Manage display tabs when viewing each of those artwork types.<br />

Go to the Painting page, then select Manage fields. Add a new single-line text field<br />

called Artist, just as you would for a node. Now go to Sculpture and add the now<br />

existing Artist field to that artwork type.<br />

That's the biggest advantage of making a new data object an entity. All of the power<br />

of Fields we get for free that way. Take note that even fields that normally attach to<br />

node types are available to us. We can now build up the entire data model of our<br />

artworks without writing any additional code, using all of the Field types available<br />

to us either in core or in contributed modules.<br />

Adding artworks<br />

Now that we have our artwork types hooked up, we need to be able to create<br />

them. Again modeling on the way nodes work let's add the following new entries<br />

to our menu hook:<br />

function artwork_menu() {<br />

$items['artwork/add'] = array(<br />

'title' => 'Add new artwork',<br />

'page callback' => 'artwork_add_page',

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

Saved successfully!

Ooh no, something went wrong!