field.install 11.7 KB
Newer Older
Dries's avatar
 
Dries committed
1
<?php
2

3 4 5 6 7
/**
 * @file
 * Install, update and uninstall functions for the field module.
 */

Dries's avatar
 
Dries committed
8
/**
9
 * Implements hook_schema().
Dries's avatar
 
Dries committed
10 11 12 13 14
 */
function field_schema() {
  // Static (meta) tables.
  $schema['field_config'] = array(
    'fields' => array(
15 16 17 18 19
      'id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'The primary identifier for a field',
      ),
Dries's avatar
 
Dries committed
20 21 22 23
      'field_name' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
24
        'description' => 'The name of this field. Non-deleted field names are unique, but multiple deleted fields can have the same name.',
Dries's avatar
 
Dries committed
25 26 27 28 29
      ),
      'type' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
30
        'description' => 'The type of this field.',
Dries's avatar
 
Dries committed
31
      ),
32 33 34 35 36 37 38 39
     'module' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The module that implements the field type.',
      ),
      'active' => array(
Dries's avatar
 
Dries committed
40 41 42 43
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
44
        'description' => 'Boolean indicating whether the module that implements the field type is enabled.',
Dries's avatar
 
Dries committed
45
      ),
46 47 48
      'storage_type' => array(
        'type' => 'varchar',
        'length' => 128,
Dries's avatar
 
Dries committed
49
        'not null' => TRUE,
50
        'description' => 'The storage backend for the field.',
Dries's avatar
 
Dries committed
51
      ),
52
      'storage_module' => array(
Dries's avatar
 
Dries committed
53 54 55 56
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
57
        'description' => 'The module that implements the storage backend.',
Dries's avatar
 
Dries committed
58
      ),
59
      'storage_active' => array(
Dries's avatar
 
Dries committed
60 61 62
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
63
        'default' => 0,
64
        'description' => 'Boolean indicating whether the module that implements the storage backend is enabled.',
Dries's avatar
 
Dries committed
65
      ),
66
      'locked' => array(
67 68 69 70
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
71
        'description' => '@TODO',
72
      ),
73
      'data' => array(
74 75
        'type' => 'blob',
        'size' => 'big',
76 77 78 79 80 81 82 83 84 85 86
        '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(
Dries's avatar
 
Dries committed
87 88 89
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
90
        'default' => 0,
Dries's avatar
 
Dries committed
91 92 93 94 95 96 97 98
      ),
      'deleted' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
99
    'primary key' => array('id'),
Dries's avatar
 
Dries committed
100
    'indexes' => array(
101
      'field_name' => array('field_name'),
102 103 104 105
      // Used by field_read_fields().
      'active' => array('active'),
      'storage_active' => array('storage_active'),
      'deleted' => array('deleted'),
106
      // Used by field_sync_field_status().
Dries's avatar
 
Dries committed
107
      'module' => array('module'),
108
      'storage_module' => array('storage_module'),
Dries's avatar
 
Dries committed
109
      'type' => array('type'),
110
      'storage_type' => array('storage_type'),
Dries's avatar
 
Dries committed
111 112 113 114
    ),
  );
  $schema['field_config_instance'] = array(
    'fields' => array(
115 116 117 118 119 120 121 122 123 124
      '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',
      ),
125 126 127
      'field_name' => array(
        'type' => 'varchar',
        'length' => 32,
Dries's avatar
 
Dries committed
128
        'not null' => TRUE,
129 130
        'default' => ''
      ),
131
      'entity_type'       => array(
132 133 134 135 136
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => ''
      ),
137 138 139 140 141
      'bundle' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => ''
Dries's avatar
 
Dries committed
142 143
      ),
      'data' => array(
144 145
        'type' => 'blob',
        'size' => 'big',
Dries's avatar
 
Dries committed
146 147 148 149 150 151 152 153 154 155
        'not null' => TRUE,
        'serialize' => TRUE,
      ),
      'deleted' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
156
    'primary key' => array('id'),
Dries's avatar
 
Dries committed
157
    'indexes' => array(
158
      // Used by field_delete_instance().
159
      'field_name_bundle' => array('field_name', 'entity_type', 'bundle'),
160 161
      // Used by field_read_instances().
      'deleted' => array('deleted'),
Dries's avatar
 
Dries committed
162 163 164 165 166 167
    ),
  );
  $schema['cache_field'] = drupal_get_schema_unprocessed('system', 'cache');

  return $schema;
}
168 169 170 171

/**
 * Utility function: create a field by writing directly to the database.
 *
172
 * @ingroup update-api-7.x-to-8.x
173
 */
174
function _update_7000_field_create_field(&$field) {
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
  // 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,
  );

195 196 197 198 199 200 201 202 203 204 205
  // Fetch the field schema to initialize columns and indexes. 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'];

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
  // 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();

235
  // Create storage for the field.
236 237 238
  field_sql_storage_field_storage_create_field($field);
}

239
/**
240
 * Utility function: delete a field stored in SQL storage directly from the database.
241 242 243 244
 *
 * To protect user data, this function can only be used to delete fields once
 * all information it stored is gone. Delete all data from the
 * field_data_$field_name table before calling by either manually issuing
245
 * delete queries against it or using _update_7000_field_delete_instance().
246 247 248 249
 *
 * @param $field_name
 *   The field name to delete.
 *
250
 * @ingroup update-api-7.x-to-8.x
251
 */
252
function _update_7000_field_delete_field($field_name) {
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
  $table_name = 'field_data_' . $field_name;
  if (db_select($table_name)->range(0, 1)->countQuery()->execute()->fetchField()) {
    $t = get_t();
    throw new Exception($t('This function can only be used to delete fields without data'));
  }
  // Delete all instances.
  db_delete('field_config_instance')
    ->condition('field_name', $field_name)
    ->execute();

  // Nuke field data and revision tables.
  db_drop_table($table_name);
  db_drop_table('field_revision_' . $field_name);

  // Delete the field.
  db_delete('field_config')
    ->condition('field_name', $field_name)
    ->execute();
}


/**
 * Utility function: delete an instance and all its data of a field stored in SQL Storage.
 *
 * BEWARE: this function deletes user data from the field storage tables.
 *
279
 * @ingroup update-api-7.x-to-8.x
280
 */
281
function _update_7000_field_delete_instance($field_name, $entity_type, $bundle) {
282 283 284 285 286 287 288 289 290
  // Delete field instance configuration data.
  db_delete('field_config_instance')
    ->condition('field_name', $field_name)
    ->condition('entity_type', $entity_type)
    ->condition('bundle', $bundle)
    ->execute();

  // Nuke data.
  db_delete('field_data_' . $field_name)
291
    ->condition('entity_type', $entity_type)
292 293 294
    ->condition('bundle', $bundle)
    ->execute();
  db_delete('field_revision_' . $field_name)
295
    ->condition('entity_type', $entity_type)
296 297 298 299 300 301
    ->condition('bundle', $bundle)
    ->execute();
}

/**
 * Utility function: fetch all the field definitions from the database.
302
 *
303 304 305 306
 * Warning: unlike the field_read_fields() API function, this function returns
 * all fields by default, including deleted and inactive fields, unless
 * specified otherwise in the $conditions parameter.
 *
307 308
 * @param $conditions
 *   An array of conditions to limit the select query to.
309 310 311 312 313 314 315 316
 * @param $key
 *   The name of the field property the return array is indexed by. Using
 *   anything else than 'id' might cause incomplete results if the $conditions
 *   do not filter out deleted fields.
 *
 * @return
 *   An array of fields matching $conditions, keyed by the property specified
 *   by the $key parameter.
317
 * @ingroup update-api-7.x-to-8.x
318
 */
319
function _update_7000_field_read_fields(array $conditions = array(), $key = 'id') {
320
  $fields = array();
321
  $query = db_select('field_config', 'fc', array('fetch' => PDO::FETCH_ASSOC))
322 323 324
    ->fields('fc');
  foreach ($conditions as $column => $value) {
    $query->condition($column, $value);
325 326
  }
  foreach ($query->execute() as $record) {
327 328 329 330 331 332 333 334 335 336 337 338 339 340
    $field = unserialize($record['data']);
    $field['id'] = $record['id'];
    $field['field_name'] = $record['field_name'];
    $field['type'] = $record['type'];
    $field['module'] = $record['module'];
    $field['active'] = $record['active'];
    $field['storage']['type'] = $record['storage_type'];
    $field['storage']['module'] = $record['storage_module'];
    $field['storage']['active'] = $record['storage_active'];
    $field['locked'] = $record['locked'];
    $field['cardinality'] = $record['cardinality'];
    $field['translatable'] = $record['translatable'];
    $field['deleted'] = $record['deleted'];

341
    $fields[$field[$key]] = $field;
342 343 344 345
  }
  return $fields;
}

346
/**
347
 * Utility function: write a field instance directly to the database.
348
 *
349
 * @ingroup update-api-7.x-to-8.x
350
 */
351
function _update_7000_field_create_instance($field, &$instance) {
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
  // 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();
}