You are here

function entityreference_field_schema in Entity reference 7

Same name and namespace in other branches
  1. 8 entityreference.install \entityreference_field_schema()

Implements hook_field_schema().

File

./entityreference.install, line 13

Code

function entityreference_field_schema($field) {
  if ($field['type'] == 'entityreference') {

    // Load the base table configuration from the cache.
    $base_tables = variable_get('entityreference:base-tables', array());
    $schema = array(
      'columns' => array(
        'target_id' => array(
          'description' => 'The id of the target entity.',
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
        ),
      ),
      'indexes' => array(
        'target_id' => array(
          'target_id',
        ),
      ),
      'foreign keys' => array(),
    );

    // Create a foreign key to the target entity type base type, if available.
    $entity_type = $field['settings']['target_type'];
    if (isset($base_tables[$entity_type])) {
      list($base_table, $id_column) = $base_tables[$entity_type];
      $schema['foreign keys'][$base_table] = array(
        'table' => $base_table,
        'columns' => array(
          'target_id' => $id_column,
        ),
      );
    }

    // Invoke the behaviors to allow them to change the schema.
    module_load_include('module', 'entityreference');
    foreach (entityreference_get_behavior_handlers($field) as $handler) {
      $handler
        ->schema_alter($schema, $field);
    }
    return $schema;
  }
}