Field Data Storage

STORE FIELD DATA

Next we can define the columns that we'll use to store our data. This information will be defined in the .install file for the module.

In order to define the database column we'll need to implement the hook_field_schema() 

hook_field_schema() uses the SCHEMA API. For more information on the schema api see https://www.drupal.org/developing/api/schema
 

function myfield_field_schema(){
    $columns = array(
        'color' => array(
            'type' => 'varchar',
            'length' => 6, //#FF00FF
            'not null' => true
        ),
        'label' => array(
            'type' => 'varchar',
            'length' => 512,
            'not null' => true
        )
    );
    $indexes = array(
        'color' => array('color')
    );
    return array(
        'columns' => $columns,
        'indexes' => $indexes
    );
}

 

FIELD INSTANCE

Here we added an instance of our field to acontent type. The field instance is field_car_color

 

 

DATABASE TABLE

The field API creates a table named field_data_field_car_color in the database to store the field information.
2 special columns are created based on the array keys we defined in our schema in the database to store our field data.
field_data_field_car_color 
field_car_color_label

 

Cookbook Category: