18.10.2016 Views

Drupal 7 Module Development

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 7<br />

The most important magic callback for a field type is the schema callback, its<br />

definition can be seen in the following example:<br />

function dimfield_field_schema($field) {<br />

if ($field['type'] == 'dimensions') {<br />

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

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

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

);<br />

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

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

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

);<br />

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

'height' => array('height'),<br />

'width' => array('width'),<br />

);<br />

if ($field['settings']['num_dimensions'] == 3) {<br />

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

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

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

);<br />

$schema['indexes']['depth'] = array('depth');<br />

}<br />

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

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

'length' => 10,<br />

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

);<br />

}<br />

}<br />

return $schema;<br />

As we would expect from a name like hook_field_schema(), its return value is a<br />

<strong>Drupal</strong> schema array. Although fields will not always be saved in an SQL database,<br />

they usually are, and it's a convenient syntax to reuse. Note that in this case, we<br />

define two database columns, for height and width, and possibly a third for depth<br />

if our field is configured to have three dimensions. (We will skip over supporting<br />

four or five dimensions for now as it is an edge case.) The difference in the data<br />

structure is the reason the number of dimensions are a field setting rather than a<br />

field instance setting.<br />

[ 187 ]

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

Saved successfully!

Ooh no, something went wrong!