Projet

Général

Profil

Paste
Télécharger (5,17 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / entityreference / entityreference.install @ a45e4bc1

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
    foreach (entityreference_get_behavior_handlers($field) as $handler) {
45
      $handler->schema_alter($schema, $field);
46
    }
47

    
48
    return $schema;
49
  }
50
}
51

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

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

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

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

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

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

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

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

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

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

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

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