Project

General

Profile

Paste
Download (5.23 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / entityreference / entityreference.install @ 56aebcb7

1
<?php
2

    
3
/**
4
 * Implements hook_uninstall().
5
 */
6
function entityreference_uninstall() {
7
  variable_del('entityreference:base-tables');
8
}
9

    
10
/**
11
 * Implements hook_field_schema().
12
 */
13
function entityreference_field_schema($field) {
14
  if ($field['type'] == 'entityreference') {
15
    // Load the base table configuration from the cache.
16
    $base_tables = variable_get('entityreference:base-tables', array());
17

    
18
    $schema = array(
19
      'columns' => array(
20
        'target_id' => array(
21
          'description' => 'The id of the target entity.',
22
          'type' => 'int',
23
          'unsigned' => TRUE,
24
          'not null' => TRUE,
25
        ),
26
      ),
27
      'indexes' => array(
28
        'target_id' => array('target_id'),
29
      ),
30
      'foreign keys' => array(),
31
    );
32

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

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

    
49
    return $schema;
50
  }
51
}
52

    
53
/**
54
 * Update the field configuration to the new plugin structure.
55
 */
56
function entityreference_update_7000() {
57
  // Enable ctools.
58
  if (!module_enable(array('ctools'))) {
59
    throw new DrupalUpdateException('This version of Entity Reference requires ctools, but it could not be enabled.');
60
  }
61

    
62
  // Get the list of fields of type 'entityreference'.
63
  $fields = array();
64
  foreach (field_info_fields() as $field_name => $field) {
65
    // Update the field configuration.
66
    if ($field['type'] == 'entityreference') {
67
      $settings = &$field['settings'];
68
      if (!isset($settings['handler'])) {
69
        $settings['handler'] = 'base';
70
        $settings['handler_settings']['target_bundles'] = $settings['target_bundles'];
71
        unset($settings['target_bundles']);
72
        field_update_field($field);
73
      }
74
    }
75

    
76
    // Update the instance configurations.
77
    foreach ($field['bundles'] as $entity_type => $bundles) {
78
      foreach ($bundles as $bundle) {
79
        $instance = field_info_instance($entity_type, $field_name, $bundle);
80
        $save = FALSE;
81
        if ($instance['widget']['type'] == 'entityreference_autocomplete') {
82
          $instance['widget']['type'] = 'entityreference_autocomplete_tags';
83
          $save = TRUE;
84
        }
85
        // When the autocomplete path is the default value, remove it from
86
        // the configuration.
87
        if (isset($instance['widget']['settings']['path']) && $instance['widget']['settings']['path'] == 'entityreference/autocomplete') {
88
          unset($instance['widget']['settings']['path']);
89
          $save = TRUE;
90
        }
91
        if ($save) {
92
          field_update_instance($instance);
93
        }
94
      }
95
    }
96
  }
97
}
98

    
99
/**
100
 * Drop "target_type" from the field schema.
101
 */
102
function entityreference_update_7001() {
103
  if (!module_exists('field_sql_storage')) {
104
    return;
105
  }
106
  foreach (field_info_fields() as $field_name => $field) {
107
    if ($field['type'] != 'entityreference') {
108
      // Not an entity reference field.
109
      continue;
110
    }
111

    
112
    // Update the field settings.
113
    $field = field_info_field($field_name);
114
    unset($field['indexes']['target_entity']);
115
    $field['indexes']['target_id'] = array('target_id');
116
    field_update_field($field);
117

    
118
    if ($field['storage']['type'] !== 'field_sql_storage') {
119
      // Field doesn't use SQL storage, we cannot modify the schema.
120
      continue;
121
    }
122
    $table_name = _field_sql_storage_tablename($field);
123
    $revision_name = _field_sql_storage_revision_tablename($field);
124

    
125
    db_drop_index($table_name, $field_name . '_target_entity');
126
    db_drop_index($table_name, $field_name . '_target_id');
127
    db_drop_field($table_name, $field_name . '_target_type');
128
    db_add_index($table_name, $field_name . '_target_id', array($field_name . '_target_id'));
129

    
130
    db_drop_index($revision_name, $field_name . '_target_entity');
131
    db_drop_index($revision_name, $field_name . '_target_id');
132
    db_drop_field($revision_name, $field_name . '_target_type');
133
    db_add_index($revision_name, $field_name . '_target_id', array($field_name . '_target_id'));
134
  }
135
}
136

    
137
/**
138
 * Make the target_id column NOT NULL.
139
 */
140
function entityreference_update_7002() {
141
  if (!module_exists('field_sql_storage')) {
142
    return;
143
  }
144
  foreach (field_info_fields() as $field_name => $field) {
145
    if ($field['type'] != 'entityreference') {
146
      // Not an entity reference field.
147
      continue;
148
    }
149

    
150
    if ($field['storage']['type'] !== 'field_sql_storage') {
151
      // Field doesn't use SQL storage, we cannot modify the schema.
152
      continue;
153
    }
154

    
155
    $table_name = _field_sql_storage_tablename($field);
156
    $revision_name = _field_sql_storage_revision_tablename($field);
157

    
158
    db_change_field($table_name, $field_name . '_target_id', $field_name . '_target_id', array(
159
      'description' => 'The id of the target entity.',
160
      'type' => 'int',
161
      'unsigned' => TRUE,
162
      'not null' => TRUE,
163
    ));
164
  }
165
}