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

• We may be accessing entities that are not stored in <strong>Drupal</strong>'s local database,<br />

such as a legacy data store.<br />

• We may need to have internal variants, like node types, but nodes don't<br />

support "sub-type types".<br />

For simplicity we'll not do anything too exotic for now. Instead, we'll look at a<br />

relatively simple use case and mirror node handling fairly closely.<br />

The goal<br />

For our example, we'll create a new entity called "artwork". This entity will represent<br />

a work of art held by a museum and managed through <strong>Drupal</strong>. Like nodes, artworks<br />

will have sub-types like "painting" and "sculpture". We will want to allow users to<br />

create, edit, and delete artworks, as well as configure what fields are available on<br />

each artwork type.<br />

In practice most real museums would have their collection stored in a dedicated<br />

collection management system and we would need to just provide a wrapper that<br />

reads data from it in a <strong>Drupal</strong>-friendly way. For our purposes though we will<br />

assume a very small museum that wants to use <strong>Drupal</strong> itself as a simple collection<br />

management system, which implies full create, read, update, and delete capabilities.<br />

Bundles<br />

In earlier versions of <strong>Drupal</strong> only nodes had the ability to have sub-types. In <strong>Drupal</strong><br />

7, all entities have the ability to support sub-types. In <strong>Drupal</strong> parlance, these subtypes<br />

are called "bundles".<br />

A bundle is a sub-type of an entity that can be configured separately.<br />

Node types are an example of a bundle. Not all entity types have bundles.<br />

Users, for instance, do not have separate bundles.<br />

For now, we'll hard-code two bundles, painting and sculpture. In a real use case we'd<br />

be likely to also include an administration system to create and manage bundles.<br />

The Schema API<br />

We will need a place to store our artwork data, so we need to create some new<br />

database tables. Rather than create them directly, though, we'll let <strong>Drupal</strong> do<br />

that for us using a part of the database layer called the Schema API.<br />

[ 152 ]

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

Saved successfully!

Ooh no, something went wrong!