Drupal PHP Cross Reference Content Management Systems

Source: /modules/field/field.install - 464 lines - 14752 bytes - Summary - Text - Print

   1  <?php
   2  
   3  /**
   4   * @file
   5   * Install, update and uninstall functions for the field module.
   6   */
   7  
   8  /**
   9   * Implements hook_schema().
  10   */
  11  function field_schema() {
  12    // Static (meta) tables.
  13    $schema['field_config'] = array(
  14      'fields' => array(
  15        'id' => array(
  16          'type' => 'serial',
  17          'not null' => TRUE,
  18          'description' => 'The primary identifier for a field',
  19        ),
  20        'field_name' => array(
  21          'type' => 'varchar',
  22          'length' => 32,
  23          '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.',
  25        ),
  26        'type' => array(
  27          'type' => 'varchar',
  28          'length' => 128,
  29          'not null' => TRUE,
  30          'description' => 'The type of this field.',
  31        ),
  32       'module' => array(
  33          'type' => 'varchar',
  34          'length' => 128,
  35          'not null' => TRUE,
  36          'default' => '',
  37          'description' => 'The module that implements the field type.',
  38        ),
  39        'active' => array(
  40          'type' => 'int',
  41          'size' => 'tiny',
  42          'not null' => TRUE,
  43          'default' => 0,
  44          'description' => 'Boolean indicating whether the module that implements the field type is enabled.',
  45        ),
  46        'storage_type' => array(
  47          'type' => 'varchar',
  48          'length' => 128,
  49          'not null' => TRUE,
  50          'description' => 'The storage backend for the field.',
  51        ),
  52        'storage_module' => array(
  53          'type' => 'varchar',
  54          'length' => 128,
  55          'not null' => TRUE,
  56          'default' => '',
  57          'description' => 'The module that implements the storage backend.',
  58        ),
  59        'storage_active' => array(
  60          'type' => 'int',
  61          'size' => 'tiny',
  62          'not null' => TRUE,
  63          'default' => 0,
  64          'description' => 'Boolean indicating whether the module that implements the storage backend is enabled.',
  65        ),
  66        'locked' => array(
  67          'type' => 'int',
  68          'size' => 'tiny',
  69          'not null' => TRUE,
  70          'default' => 0,
  71          'description' => '@TODO',
  72        ),
  73        'data' => array(
  74          'type' => 'blob',
  75          'size' => 'big',
  76          'not null' => TRUE,
  77          'serialize' => TRUE,
  78          'description' => 'Serialized data containing the field properties that do not warrant a dedicated column.',
  79        ),
  80        'cardinality' => array(
  81          'type' => 'int',
  82          'size' => 'tiny',
  83          'not null' => TRUE,
  84          'default' => 0,
  85        ),
  86        'translatable' => array(
  87          'type' => 'int',
  88          'size' => 'tiny',
  89          'not null' => TRUE,
  90          'default' => 0,
  91        ),
  92        'deleted' => array(
  93          'type' => 'int',
  94          'size' => 'tiny',
  95          'not null' => TRUE,
  96          'default' => 0,
  97        ),
  98      ),
  99      'primary key' => array('id'),
 100      'indexes' => array(
 101        'field_name' => array('field_name'),
 102        // Used by field_sync_field_status().
 103        'active' => array('active'),
 104        'storage_active' => array('storage_active'),
 105        'deleted' => array('deleted'),
 106        // Used by field_modules_disabled().
 107        'module' => array('module'),
 108        'storage_module' => array('storage_module'),
 109        'type' => array('type'),
 110        'storage_type' => array('storage_type'),
 111      ),
 112    );
 113    $schema['field_config_instance'] = array(
 114      'fields' => array(
 115        'id' => array(
 116          'type' => 'serial',
 117          'not null' => TRUE,
 118          'description' => 'The primary identifier for a field instance',
 119        ),
 120        'field_id' => array(
 121          'type' => 'int',
 122          'not null' => TRUE,
 123          'description' => 'The identifier of the field attached by this instance',
 124        ),
 125        'field_name' => array(
 126          'type' => 'varchar',
 127          'length' => 32,
 128          'not null' => TRUE,
 129          'default' => ''
 130        ),
 131        'entity_type'       => array(
 132          'type' => 'varchar',
 133          'length' => 32,
 134          'not null' => TRUE,
 135          'default' => ''
 136        ),
 137        'bundle' => array(
 138          'type' => 'varchar',
 139          'length' => 128,
 140          'not null' => TRUE,
 141          'default' => ''
 142        ),
 143        'data' => array(
 144          'type' => 'blob',
 145          'size' => 'big',
 146          'not null' => TRUE,
 147          'serialize' => TRUE,
 148        ),
 149        'deleted' => array(
 150          'type' => 'int',
 151          'size' => 'tiny',
 152          'not null' => TRUE,
 153          'default' => 0,
 154        ),
 155      ),
 156      'primary key' => array('id'),
 157      'indexes' => array(
 158        // Used by field_delete_instance().
 159        'field_name_bundle' => array('field_name', 'entity_type', 'bundle'),
 160        // Used by field_read_instances().
 161        'deleted' => array('deleted'),
 162      ),
 163    );
 164    $schema['cache_field'] = drupal_get_schema_unprocessed('system', 'cache');
 165  
 166    return $schema;
 167  }
 168  
 169  /**
 170   * Utility function: create a field by writing directly to the database.
 171   *
 172   * This function can be used for databases whose schema is at field module
 173   * version 7000 or higher.
 174   *
 175   * @ingroup update_api
 176   */
 177  function _update_7000_field_create_field(&$field) {
 178    // Merge in default values.`
 179    $field += array(
 180      'entity_types' => array(),
 181      'cardinality' => 1,
 182      'translatable' => FALSE,
 183      'locked' => FALSE,
 184      'settings' => array(),
 185      'indexes' => array(),
 186      'deleted' => 0,
 187      'active' => 1,
 188    );
 189  
 190    // Set storage.
 191    $field['storage'] = array(
 192      'type' => 'field_sql_storage',
 193      'settings' => array(),
 194      'module' => 'field_sql_storage',
 195      'active' => 1,
 196    );
 197  
 198    // Fetch the field schema to initialize columns and indexes. The field module
 199    // is not guaranteed to be loaded at this point.
 200    module_load_install($field['module']);
 201    $schema = (array) module_invoke($field['module'], 'field_schema', $field);
 202    $schema += array('columns' => array(), 'indexes' => array());
 203    // 'columns' are hardcoded in the field type.
 204    $field['columns'] = $schema['columns'];
 205    // 'indexes' can be both hardcoded in the field type, and specified in the
 206    // incoming $field definition.
 207    $field['indexes'] += $schema['indexes'];
 208  
 209    // The serialized 'data' column contains everything from $field that does not
 210    // have its own column and is not automatically populated when the field is
 211    // read.
 212    $data = $field;
 213    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']);
 214    // Additionally, do not save the 'bundles' property populated by
 215    // field_info_field().
 216    unset($data['bundles']);
 217  
 218    // Write the field to the database.
 219    $record = array(
 220      'field_name' => $field['field_name'],
 221      'type' => $field['type'],
 222      'module' => $field['module'],
 223      'active' => (int) $field['active'],
 224      'storage_type' => $field['storage']['type'],
 225      'storage_module' => $field['storage']['module'],
 226      'storage_active' => (int) $field['storage']['active'],
 227      'locked' => (int) $field['locked'],
 228      'data' => serialize($data),
 229      'cardinality' => $field['cardinality'],
 230      'translatable' => (int) $field['translatable'],
 231      'deleted' => (int) $field['deleted'],
 232    );
 233    // We don't use drupal_write_record() here because it depends on the schema.
 234    $field['id'] = db_insert('field_config')
 235      ->fields($record)
 236      ->execute();
 237  
 238    // Create storage for the field.
 239    field_sql_storage_field_storage_create_field($field);
 240  }
 241  
 242  /**
 243   * Utility function: delete a field stored in SQL storage directly from the database.
 244   *
 245   * To protect user data, this function can only be used to delete fields once
 246   * all information it stored is gone. Delete all data from the
 247   * field_data_$field_name table before calling by either manually issuing
 248   * delete queries against it or using _update_7000_field_delete_instance().
 249   *
 250   * This function can be used for databases whose schema is at field module
 251   * version 7000 or higher.
 252   *
 253   * @param $field_name
 254   *   The field name to delete.
 255   *
 256   * @ingroup update_api
 257   */
 258  function _update_7000_field_delete_field($field_name) {
 259    $table_name = 'field_data_' . $field_name;
 260    if (db_select($table_name)->range(0, 1)->countQuery()->execute()->fetchField()) {
 261      $t = get_t();
 262      throw new Exception($t('This function can only be used to delete fields without data'));
 263    }
 264    // Delete all instances.
 265    db_delete('field_config_instance')
 266      ->condition('field_name', $field_name)
 267      ->execute();
 268  
 269    // Nuke field data and revision tables.
 270    db_drop_table($table_name);
 271    db_drop_table('field_revision_' . $field_name);
 272  
 273    // Delete the field.
 274    db_delete('field_config')
 275      ->condition('field_name', $field_name)
 276      ->execute();
 277  }
 278  
 279  
 280  /**
 281   * Utility function: delete an instance and all its data of a field stored in SQL Storage.
 282   *
 283   * BEWARE: this function deletes user data from the field storage tables.
 284   *
 285   * This function is valid for a database schema version 7000.
 286   *
 287   * @ingroup update_api
 288   */
 289  function _update_7000_field_delete_instance($field_name, $entity_type, $bundle) {
 290    // Delete field instance configuration data.
 291    db_delete('field_config_instance')
 292      ->condition('field_name', $field_name)
 293      ->condition('entity_type', $entity_type)
 294      ->condition('bundle', $bundle)
 295      ->execute();
 296  
 297    // Nuke data.
 298    db_delete('field_data_' . $field_name)
 299      ->condition('entity_type', $entity_type)
 300      ->condition('bundle', $bundle)
 301      ->execute();
 302    db_delete('field_revision_' . $field_name)
 303      ->condition('entity_type', $entity_type)
 304      ->condition('bundle', $bundle)
 305      ->execute();
 306  }
 307  
 308  /**
 309   * Utility function: fetch all the field definitions from the database.
 310   *
 311   * Warning: unlike the field_read_fields() API function, this function returns
 312   * all fields by default, including deleted and inactive fields, unless
 313   * specified otherwise in the $conditions parameter.
 314   *
 315   * @param $conditions
 316   *   An array of conditions to limit the select query to.
 317   * @param $key
 318   *   The name of the field property the return array is indexed by. Using
 319   *   anything else than 'id' might cause incomplete results if the $conditions
 320   *   do not filter out deleted fields.
 321   *
 322   * @return
 323   *   An array of fields matching $conditions, keyed by the property specified
 324   *   by the $key parameter.
 325   *
 326   * @ingroup update_api
 327   */
 328  function _update_7000_field_read_fields(array $conditions = array(), $key = 'id') {
 329    $fields = array();
 330    $query = db_select('field_config', 'fc', array('fetch' => PDO::FETCH_ASSOC))
 331      ->fields('fc');
 332    foreach ($conditions as $column => $value) {
 333      $query->condition($column, $value);
 334    }
 335    foreach ($query->execute() as $record) {
 336      $field = unserialize($record['data']);
 337      $field['id'] = $record['id'];
 338      $field['field_name'] = $record['field_name'];
 339      $field['type'] = $record['type'];
 340      $field['module'] = $record['module'];
 341      $field['active'] = $record['active'];
 342      $field['storage']['type'] = $record['storage_type'];
 343      $field['storage']['module'] = $record['storage_module'];
 344      $field['storage']['active'] = $record['storage_active'];
 345      $field['locked'] = $record['locked'];
 346      $field['cardinality'] = $record['cardinality'];
 347      $field['translatable'] = $record['translatable'];
 348      $field['deleted'] = $record['deleted'];
 349  
 350      $fields[$field[$key]] = $field;
 351    }
 352    return $fields;
 353  }
 354  
 355  /**
 356   * Utility function: write a field instance directly to the database.
 357   *
 358   * This function can be used for databases whose schema is at field module
 359   * version 7000 or higher.
 360   *
 361   * @ingroup update_api
 362   */
 363  function _update_7000_field_create_instance($field, &$instance) {
 364    // Merge in defaults.
 365    $instance += array(
 366      'field_id' => $field['id'],
 367      'field_name' => $field['field_name'],
 368      'deleted' => 0,
 369    );
 370  
 371    // The serialized 'data' column contains everything from $instance that does
 372    // not have its own column and is not automatically populated when the
 373    // instance is read.
 374    $data = $instance;
 375    unset($data['id'], $data['field_id'], $data['field_name'], $data['entity_type'], $data['bundle'], $data['deleted']);
 376  
 377    $record = array(
 378      'field_id' => $instance['field_id'],
 379      'field_name' => $instance['field_name'],
 380      'entity_type' => $instance['entity_type'],
 381      'bundle' => $instance['bundle'],
 382      'data' => serialize($data),
 383      'deleted' => (int) $instance['deleted'],
 384    );
 385    $instance['id'] = db_insert('field_config_instance')
 386      ->fields($record)
 387      ->execute();
 388  }
 389  
 390  /**
 391   * @addtogroup updates-6.x-to-7.x
 392   * @{
 393   */
 394  
 395  /**
 396   * Field update version placeholder.
 397   */
 398  function field_update_7000() {
 399    // Some update helper functions (such as _update_7000_field_create_field())
 400    // modify the database directly. They can be used safely only if the database
 401    // schema matches the field module schema established for Drupal 7.0 (i.e.
 402    // version 7000). This function exists solely to set the schema version to
 403    // 7000, so that update functions calling those helpers can do so safely
 404    // by declaring a dependency on field_update_7000().
 405  }
 406  
 407  /**
 408   * Fix fields definitions created during the d6 to d7 upgrade path.
 409   */
 410  function field_update_7001() {
 411    $fields = _update_7000_field_read_fields();
 412    foreach ($fields as $field) {
 413      // _update_7000_field_create_field() was broken in d7 RC2, and the fields
 414      // created during a d6 to d7 upgrade do not correcly store the 'index'
 415      // entry. See http://drupal.org/node/996160.
 416  
 417      module_load_install($field['module']);
 418      $schema = (array) module_invoke($field['module'], 'field_schema', $field);
 419      $schema += array('indexes' => array());
 420      // 'indexes' can be both hardcoded in the field type, and specified in the
 421      // incoming $field definition.
 422      $field['indexes'] += $schema['indexes'];
 423  
 424      // Place the updated entries in the existing serialized 'data' column.
 425      $data = db_query("SELECT data FROM {field_config} WHERE id = :id", array(':id' => $field['id']))->fetchField();
 426      $data = unserialize($data);
 427      $data['columns'] = $field['columns'];
 428      $data['indexes'] = $field['indexes'];
 429  
 430      // Save the new data.
 431      $query = db_update('field_config')
 432        ->condition('id', $field['id'])
 433        ->fields(array('data' => serialize($data)))
 434        ->execute();
 435    }
 436  }
 437  
 438  /**
 439   * @} End of "addtogroup updates-6.x-to-7.x".
 440   */
 441  
 442  /**
 443   * @addtogroup updates-7.x-extra
 444   * @{
 445   */
 446  
 447  /**
 448   * Split the all-inclusive field_bundle_settings variable per bundle.
 449   */
 450  function field_update_7002() {
 451    $settings = variable_get('field_bundle_settings', array());
 452    if ($settings) {
 453      foreach ($settings as $entity_type => $entity_type_settings) {
 454        foreach ($entity_type_settings as $bundle => $bundle_settings) {
 455          variable_set('field_bundle_settings_' . $entity_type . '__' . $bundle, $bundle_settings);
 456        }
 457      }
 458      variable_del('field_bundle_settings');
 459    }
 460  }
 461  
 462  /**
 463   * @} End of "addtogroup updates-7.x-extra".
 464   */

title

Description

title

Description

title

Description

title

title

Body