summaryrefslogtreecommitdiff
path: root/modules/field/field.install
blob: 4178f6da7a3d6cc52dd569d7b58dbf5030b83ca0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
// $Id$

/**
 * @file
 * Install, update and uninstall functions for the field module.
 */

/**
 * Implements hook_schema().
 */
function field_schema() {
  // Static (meta) tables.
  $schema['field_config'] = array(
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'The primary identifier for a field',
      ),
      'field_name' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'description' => 'The name of this field. Non-deleted field names are unique, but multiple deleted fields can have the same name.',
      ),
      'type' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'description' => 'The type of this field.',
      ),
     'module' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The module that implements the field type.',
      ),
      'active' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Boolean indicating whether the module that implements the field type is enabled.',
      ),
      'storage_type' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'description' => 'The storage backend for the field.',
      ),
      'storage_module' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The module that implements the storage backend.',
      ),
      'storage_active' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Boolean indicating whether the module that implements the storage backend is enabled.',
      ),
      'locked' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
        'description' => '@TODO',
      ),
      'data' => array(
        'type' => 'blob',
        'size' => 'big',
        'not null' => TRUE,
        'serialize' => TRUE,
        'description' => 'Serialized data containing the field properties that do not warrant a dedicated column.',
      ),
      'cardinality' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'translatable' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
      'deleted' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('id'),
    'indexes' => array(
      'field_name' => array('field_name'),
      // Used by field_read_fields().
      'active' => array('active'),
      'storage_active' => array('storage_active'),
      'deleted' => array('deleted'),
      // Used by field_modules_disabled().
      'module' => array('module'),
      'storage_module' => array('storage_module'),
      // Used by field_associate_fields().
      'type' => array('type'),
      'storage_type' => array('storage_type'),
    ),
  );
  $schema['field_config_instance'] = array(
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'The primary identifier for a field instance',
      ),
      'field_id' => array(
        'type' => 'int',
        'not null' => TRUE,
        'description' => 'The identifier of the field attached by this instance',
      ),
      'field_name' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => ''
      ),
      'entity_type'       => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => ''
      ),
      'bundle' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => ''
      ),
      'data' => array(
        'type' => 'blob',
        'size' => 'big',
        'not null' => TRUE,
        'serialize' => TRUE,
      ),
      'deleted' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('id'),
    'indexes' => array(
      // Used by field_delete_instance().
      'field_name_bundle' => array('field_name', 'entity_type', 'bundle'),
      // Used by field_read_instances().
      'deleted' => array('deleted'),
    ),
  );
  $schema['cache_field'] = drupal_get_schema_unprocessed('system', 'cache');

  return $schema;
}

/**
 * Utility function: create a field by writing directly to the database.
 *
 * This function is valid for a database schema version 7000.
 *
 * @ingroup update-api-6.x-to-7.x
 */
function _update_7000_field_create_field(&$field) {
  // Merge in default values.`
  $field += array(
    'entity_types' => array(),
    'cardinality' => 1,
    'translatable' => FALSE,
    'locked' => FALSE,
    'settings' => array(),
    'indexes' => array(),
    'deleted' => 0,
    'active' => 1,
  );

  // Set storage.
  $field['storage'] = array(
    'type' => 'field_sql_storage',
    'settings' => array(),
    'module' => 'field_sql_storage',
    'active' => 1,
  );

  // The serialized 'data' column contains everything from $field that does not
  // have its own column and is not automatically populated when the field is
  // read.
  $data = $field;
  unset($data['columns'], $data['field_name'], $data['type'], $data['active'], $data['module'], $data['storage_type'], $data['storage_active'], $data['storage_module'], $data['locked'], $data['cardinality'], $data['deleted']);
  // Additionally, do not save the 'bundles' property populated by
  // field_info_field().
  unset($data['bundles']);

  // Write the field to the database.
  $record = array(
    'field_name' => $field['field_name'],
    'type' => $field['type'],
    'module' => $field['module'],
    'active' => (int) $field['active'],
    'storage_type' => $field['storage']['type'],
    'storage_module' => $field['storage']['module'],
    'storage_active' => (int) $field['storage']['active'],
    'locked' => (int) $field['locked'],
    'data' => serialize($data),
    'cardinality' => $field['cardinality'],
    'translatable' => (int) $field['translatable'],
    'deleted' => (int) $field['deleted'],
  );
  // We don't use drupal_write_record() here because it depends on the schema.
  $field['id'] = db_insert('field_config')
    ->fields($record)
    ->execute();

  // Create storage for this field, the field module is not guaranteed to be
  // loaded at this point.
  module_load_install($field['module']);
  $schema = (array) module_invoke($field['module'], 'field_schema', $field);
  $schema += array('columns' => array(), 'indexes' => array());
  // 'columns' are hardcoded in the field type.
  $field['columns'] = $schema['columns'];
  // 'indexes' can be both hardcoded in the field type, and specified in the
  // incoming $field definition.
  $field['indexes'] += $schema['indexes'];

  field_sql_storage_field_storage_create_field($field);
}

/**
 * Utility function: create a field instance directly to the database.
 *
 * This function is valid for a database schema version 7000.
 *
 * @ingroup update-api-6.x-to-7.x
 */
function _update_7000_field_create_instance($field, &$instance) {
  // Merge in defaults.
  $instance += array(
    'field_id' => $field['id'],
    'field_name' => $field['field_name'],
    'deleted' => 0,
  );

  // The serialized 'data' column contains everything from $instance that does
  // not have its own column and is not automatically populated when the
  // instance is read.
  $data = $instance;
  unset($data['id'], $data['field_id'], $data['field_name'], $data['entity_type'], $data['bundle'], $data['deleted']);

  $record = array(
    'field_id' => $instance['field_id'],
    'field_name' => $instance['field_name'],
    'entity_type' => $instance['entity_type'],
    'bundle' => $instance['bundle'],
    'data' => serialize($data),
    'deleted' => (int) $instance['deleted'],
  );
  $instance['id'] = db_insert('field_config_instance')
    ->fields($record)
    ->execute();
}

/**
 * @defgroup field-updates-6.x-to-7.x Field updates from 6.x to 7.x
 * @{
 */

/**
 * Field update version placeholder.
 */
function field_update_7000() {
  // _update_7000_field_create_field() is supposed to create fields according
  // to the structure of fields after running field_update_7000(). So this
  // function needs to exist but as field is a new module in Drupal 7, it
  // does not need to do anything.
}

/**
 * @} End of "defgroup field-updates-6.x-to-7.x"
 */