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 Schema API allows database-agnostic definition and manipulation of<br />

the tables in <strong>Drupal</strong>'s SQL database.<br />

First, let's create a new module called "artwork". Start with the artwork.info and<br />

artwork.module files, as we've seen in previous chapters. However, we will also<br />

add another file, artwork.install. This file contains hooks that <strong>Drupal</strong> only ever<br />

uses when the module is being installed, removed, or updated so it only gets loaded<br />

at those times, saving considerable code on most page loads.<br />

The most important hook in the artwork.install file is hook_schema(), which<br />

defines the database tables this module provides. We'll start with the following<br />

table definition, closely based on the node table:<br />

function artwork_schema() {<br />

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

'description' => 'The base table for artworks.',<br />

'fields' => array(<br />

'aid' => array(<br />

'description' => 'The primary identifier for an artwork.',<br />

'type' => 'serial',<br />

'unsigned' => TRUE,<br />

'not null' => TRUE,<br />

),<br />

'vid' => array(<br />

'description' =><br />

'The current {artwork_revision}.vid version identifier.',<br />

'type' => 'int',<br />

'unsigned' => TRUE,<br />

'not null' => TRUE,<br />

'default' => 0,<br />

),<br />

'type' => array(<br />

'description' => 'The {artwork_type} of this artwork.',<br />

'type' => 'varchar',<br />

'length' => 32,<br />

'not null' => TRUE,<br />

'default' => '',<br />

),<br />

'title' => array(<br />

'description' => 'The title of this artwork.',<br />

'type' => 'varchar',<br />

'length' => 255,<br />

'not null' => TRUE,<br />

[ 153 ]

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

Saved successfully!

Ooh no, something went wrong!