View source
<?php
function entityreference_uninstall() {
variable_del('entityreference:base-tables');
}
function entityreference_field_schema($field) {
if ($field['type'] == 'entityreference') {
$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(),
);
$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,
),
);
}
module_load_include('module', 'entityreference');
foreach (entityreference_get_behavior_handlers($field) as $handler) {
$handler
->schema_alter($schema, $field);
}
return $schema;
}
}
function entityreference_update_7000() {
if (!module_enable(array(
'ctools',
))) {
throw new DrupalUpdateException('This version of Entity Reference requires ctools, but it could not be enabled.');
}
$fields = array();
foreach (field_info_fields() as $field_name => $field) {
if ($field['type'] == 'entityreference') {
$settings =& $field['settings'];
if (!isset($settings['handler'])) {
$settings['handler'] = 'base';
$settings['handler_settings']['target_bundles'] = $settings['target_bundles'];
unset($settings['target_bundles']);
field_update_field($field);
}
}
foreach ($field['bundles'] as $entity_type => $bundles) {
foreach ($bundles as $bundle) {
$instance = field_info_instance($entity_type, $field_name, $bundle);
$save = FALSE;
if ($instance['widget']['type'] == 'entityreference_autocomplete') {
$instance['widget']['type'] = 'entityreference_autocomplete_tags';
$save = TRUE;
}
if (isset($instance['widget']['settings']['path']) && $instance['widget']['settings']['path'] == 'entityreference/autocomplete') {
unset($instance['widget']['settings']['path']);
$save = TRUE;
}
if ($save) {
field_update_instance($instance);
}
}
}
}
}
function entityreference_update_7001() {
if (!module_exists('field_sql_storage')) {
return;
}
foreach (field_info_fields() as $field_name => $field) {
if ($field['type'] != 'entityreference') {
continue;
}
$field = field_info_field($field_name);
unset($field['indexes']['target_entity']);
$field['indexes']['target_id'] = array(
'target_id',
);
field_update_field($field);
if ($field['storage']['type'] !== 'field_sql_storage') {
continue;
}
$table_name = _field_sql_storage_tablename($field);
$revision_name = _field_sql_storage_revision_tablename($field);
db_drop_index($table_name, $field_name . '_target_entity');
db_drop_index($table_name, $field_name . '_target_id');
db_drop_field($table_name, $field_name . '_target_type');
db_add_index($table_name, $field_name . '_target_id', array(
$field_name . '_target_id',
));
db_drop_index($revision_name, $field_name . '_target_entity');
db_drop_index($revision_name, $field_name . '_target_id');
db_drop_field($revision_name, $field_name . '_target_type');
db_add_index($revision_name, $field_name . '_target_id', array(
$field_name . '_target_id',
));
}
}
function entityreference_update_7002() {
if (!module_exists('field_sql_storage')) {
return;
}
foreach (field_info_fields() as $field_name => $field) {
if ($field['type'] != 'entityreference') {
continue;
}
if ($field['storage']['type'] !== 'field_sql_storage') {
continue;
}
$table_name = _field_sql_storage_tablename($field);
$revision_name = _field_sql_storage_revision_tablename($field);
db_change_field($table_name, $field_name . '_target_id', $field_name . '_target_id', array(
'description' => 'The id of the target entity.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
));
}
}
function entityreference_update_7100() {
if (db_table_exists('taxonomy_index') && ($tx_schema = drupal_get_schema('taxonomy_index'))) {
if (db_table_exists('taxonomy_index_tmp')) {
db_drop_table('taxonomy_index_tmp');
}
db_create_table('taxonomy_index_tmp', $tx_schema);
$select = db_select('taxonomy_index', 'tx');
$select
->fields('tx', array(
'nid',
'tid',
));
$select
->groupBy('tx.nid');
$select
->groupBy('tx.tid');
$select
->addExpression('MAX(sticky)', 'sticky');
$select
->addExpression('MAX(created)', 'created');
db_insert('taxonomy_index_tmp')
->from($select)
->execute();
db_drop_table('taxonomy_index');
db_rename_table('taxonomy_index_tmp', 'taxonomy_index');
}
}