Check your version

This video covers a topic in Drupal 7 which may or may not be the version you're using. We're keeping this tutorial online as a courtesy to users of Drupal 7, but we consider it archived.

Alternate resources: 

Defining A Field's Schema For Database Storage

Video loading...

Transcript language code: 

Join Drupalize.Me to watch this video

Join today and gain instant access to our entire video library.

Log in Sign up
  • 0:01
    Defining a Field's Schema for Database Storage
  • 0:03
    with Joe Shindelar.
  • 0:06
  • 0:07
    With our implementation of hook_field_info
  • 0:09
    in place, Drupal now knows that we want to provide a new field type.
  • 0:13
    However, we haven't yet defined what the data for that field
  • 0:16
    is going to look like.
  • 0:18
    In this Lesson
  • 0:18
    JOE SHINDELAR: In this lesson, we're going to focus on the field type
  • 0:21
    portion of defining a custom field a bit more

Defining a Field's Schema for Database Storage


Before we can actually get our field to store data for us we need to define what the data that we're going to store looks like. The Field API does this with hook_field_schema(), which uses a very similar syntax to what is used by the hook_schema() that modules can use to define database tables. In this particular case though we're only defining what the column, or columns, that store our specific data will be and allowing the Field Storage API to decide what the structure of the created table, or tables, should be. This allows our field to remain mostly storage system agnostic and frees us from having to worry about things like how the stored field data is connected back to the entity that it belongs to, or how to format our table for proper handling of revision data or translations.

Example hook_field_schema() implemenatation.

 * Implements hook_field_schema().
function rgb_field_schema($field) {
  $columns = array(
    'rgb' => array(
      'type' => 'varchar',
      'length' => 6,
      'not null' => FALSE,
    'label' => array(
      'type' => 'varchar',
      'length' => 128,
      'not null' => FALSE,

  $indexes = array(
    'rgb' => array('rgb'),

  return array(
    'columns' => $columns,
    'indexes' => $indexes,
Log in or sign up to download companion files.