7

Task:

Create a custom field which contains a single checkbox by implementing hook_field_schema etc.

Question:

Which data type is (or should) be used to define the corresponding database column to save checkbox values?

Sample code:

function field_test_field_schema ($field)
{
    $columns = array();
    switch ($field['type']) {
        case 'test':
            $columns = array(
                    'value' => array(
                            'type' => '???',
                            'not null' => TRUE
                    ),
            );
            break;
    }
    return array('columns'=> $columns);
}

Update: node_schema uses int for boolean columns. e.g. :

'status' => array(
  'description' => 'Boolean indicating whether the node is published (visible to non-administrators).',
  'type' => 'int',
  'not null' => TRUE,
  'default' => 1,
)
8

A tiny int would be most appropriate in my opinion:

$columns = array(
  'value' => array(
    'type' => 'int',
    'not null' => TRUE,
    'size' => 'tiny',
    'default' => 0,
  ),
);

A 'standard' int would be overkill for a field that will only ever hold a 0 or 1 value.

See the Data types docs for an overview of the different types available to you.

5

There is no boolean in the Drupal schema API so you just use a tiny int. Below is an example of a checkbox for a member where checked is "blocked" and unchecked is "in good standing":

  'status' => array(
    'description' => '0 = in good standing; 1 = blocked',
    'type' => 'int',
    'size' => 'tiny',
    'not null' => TRUE,
    'default' => 0,
  ),
0

pgsql worked sample:

'has_original' => array(
  'pgsql_type' => 'boolean',
  'type' => 'int',
  'size' => 'tiny',
  'not null' => TRUE,
),

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy