You are here

function content_schema in Content Construction Kit (CCK) 6

Same name and namespace in other branches
  1. 6.3 content.install \content_schema()
  2. 6.2 content.install \content_schema()

Implementation of hook_schema.

File

./content.install, line 107

Code

function content_schema() {

  // Static (meta) tables.
  $schema['content_node_field'] = array(
    'fields' => array(
      'field_name' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'type' => array(
        'type' => 'varchar',
        'length' => 127,
        'not null' => TRUE,
        'default' => '',
      ),
      'global_settings' => array(
        'type' => 'text',
        'size' => 'medium',
        'not null' => TRUE,
        'serialize' => TRUE,
      ),
      'required' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'multiple' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'db_storage' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 1,
      ),
      'module' => array(
        'type' => 'varchar',
        'length' => 127,
        'not null' => TRUE,
        'default' => '',
      ),
      'db_columns' => array(
        'type' => 'text',
        'size' => 'medium',
        'not null' => TRUE,
        'serialize' => TRUE,
      ),
      'active' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'field_name',
    ),
  );
  $schema['content_node_field_instance'] = array(
    'fields' => array(
      'field_name' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'type_name' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'weight' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'label' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'widget_type' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'widget_settings' => array(
        'type' => 'text',
        'size' => 'medium',
        'not null' => TRUE,
        'serialize' => TRUE,
      ),
      'display_settings' => array(
        'type' => 'text',
        'size' => 'medium',
        'not null' => TRUE,
        'serialize' => TRUE,
      ),
      'description' => array(
        'type' => 'text',
        'size' => 'medium',
        'not null' => TRUE,
      ),
      'widget_module' => array(
        'type' => 'varchar',
        'length' => 127,
        'not null' => TRUE,
        'default' => '',
      ),
      'widget_active' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'field_name',
      'type_name',
    ),
  );
  $schema['cache_content'] = drupal_get_schema_unprocessed('system', 'cache');

  // When the module is first installed, the remaining code in the schema
  // will create errors, since these tables have not yet been created.
  // We don't need to create data tables on initial installation anyway
  // since no fields have been created yet, so just return with this much
  // of the schema.
  if (!db_table_exists('content_node_field') || !db_table_exists('content_node_field_instance')) {
    return $schema;
  }

  // Dynamic (data) tables.
  // We can't use many helper functions here, like content_fields() or
  // content_types() or we risk creating a fatal loop from circular
  // logic when they call other functions that use this schema, so create
  // the schema directly from a fresh query of the database.
  // content_table_schema() and content_database_info() have no
  // circular logic and are safe to use here.
  $db_result = db_query("SELECT * FROM {" . content_instance_tablename() . "} nfi " . " LEFT JOIN {" . content_field_tablename() . "} nf ON nf.field_name = nfi.field_name WHERE nf.active = 1 AND nfi.widget_active = 1");
  while ($field = db_fetch_array($db_result)) {

    // 'columns' is a reserved word in MySQL4, so our db column is named 'db_columns'.
    $field['columns'] = unserialize($field['db_columns']);
    unset($field['db_columns']);
    $content_table = _content_tablename($field['type_name'], CONTENT_DB_STORAGE_PER_CONTENT_TYPE);
    $field_table = _content_tablename($field['field_name'], CONTENT_DB_STORAGE_PER_FIELD);

    // We always add a 'per content type' table for each content type that
    // has fields.
    if (!isset($schema[$content_table])) {
      $schema[$content_table] = content_table_schema();
    }
    $base_schema = content_table_schema($field);
    if ($field['db_storage'] == CONTENT_DB_STORAGE_PER_FIELD) {

      // Per-field storage : add the 'per field' table if needed.
      if (!isset($schema[$field_table])) {
        $schema[$field_table] = $base_schema;
      }
    }
    else {

      // Per-type storage : merge the information for the field
      // in the existing table.
      $schema[$content_table]['fields'] = array_merge($schema[$content_table]['fields'], $base_schema['fields']);
      $schema[$content_table]['content fields'] = array_merge($schema[$content_table]['content fields'], $base_schema['content fields']);
    }
  }
  return $schema;
}