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_sync_field_status(). 'module' => array('module'), 'storage_module' => array('storage_module'), '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'); $schema['cache_field']['description'] = 'Cache table for the Field module to store already built field informations.'; return $schema; } /** * Creates a field by writing directly to the database. * * @ingroup update_api */ 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, ); // 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']; // 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 the field. field_sql_storage_field_storage_create_field($field); } /** * Deletes a field stored in SQL storage directly from the database. * * 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 * delete queries against it or using _update_7000_field_delete_instance(). * * @param $field_name * The field name to delete. * * @ingroup update_api */ function _update_7000_field_delete_field($field_name) { $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(); } /** * Deletes an instance and all its data of a field stored in SQL Storage. * * BEWARE: This function deletes user data from the field storage tables. * * @ingroup update_api */ function _update_7000_field_delete_instance($field_name, $entity_type, $bundle) { // 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) ->condition('entity_type', $entity_type) ->condition('bundle', $bundle) ->execute(); db_delete('field_revision_' . $field_name) ->condition('entity_type', $entity_type) ->condition('bundle', $bundle) ->execute(); } /** * Fetches all of the field definitions from the database. * * 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. * * @param $conditions * An array of conditions to limit the select query to. * @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. * @ingroup update_api */ function _update_7000_field_read_fields(array $conditions = array(), $key = 'id') { $fields = array(); $query = db_select('field_config', 'fc', array('fetch' => PDO::FETCH_ASSOC)) ->fields('fc'); foreach ($conditions as $column => $value) { $query->condition($column, $value); } foreach ($query->execute() as $record) { $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']; $fields[$field[$key]] = $field; } return $fields; } /** * Writes a field instance directly to the database. * * @ingroup update_api */ 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(); } /** * @addtogroup updates-7.x-to-8.x * @{ */ /** * Reassign all list.module fields to be controlled by options.module. */ function field_update_8001() { db_update('field_config') ->fields(array( 'module' => 'options', )) ->condition('module', 'list') ->execute(); } /** * Migrate all instance display settings to configuration. * * @ingroup config_upgrade */ function field_update_8002() { $displays = array(); module_load_install('entity'); $query = db_select('field_config_instance', 'fc')->fields('fc'); foreach ($query->execute() as $record) { // Unserialize the data array and start investigating the display key // which holds the configuration of this instance for all view modes. $data = unserialize($record->data); // Skip field instances that were created directly with the new API earlier // in the upgrade path. if (!isset($data['display'])) { continue; } foreach ($data['display'] as $view_mode => $display_options) { // Determine name and create initial entry in the $displays array if it // does not exist yet. $display_id = $record->entity_type . '.' . $record->bundle . '.' . $view_mode; if (!isset($displays[$display_id])) { $displays[$display_id] = _update_8000_entity_get_display($record->entity_type, $record->bundle, $view_mode); } // The display object does not store hidden fields. if ($display_options['type'] != 'hidden') { // We do not need the 'module' key anymore. unset($display_options['module']); $displays[$display_id]->set("content.$record->field_name", $display_options); } } // Remove the 'display' key and save the record back into the table. unset($data['display']); db_update('field_config_instance') ->condition('id', $record->id) ->fields(array( 'data' => serialize($data), )) ->execute(); } // Migration of 'extra_fields' display settings. Avoid calling // entity_get_info() by fetching the relevant variables directly in the // cariables table. $variables = array_map('unserialize', db_query("SELECT name, value FROM {variable} WHERE name LIKE '%field_bundle_settings_%'")->fetchAllKeyed()); foreach ($variables as $variable_name => $variable_value) { if (preg_match('/field_bundle_settings_(.*)__(.*)/', $variable_name, $matches)) { $entity_type = $matches[1]; $bundle = $matches[2]; if (isset($variable_value['extra_fields']['display'])) { foreach ($variable_value['extra_fields']['display'] as $field_name => $field_settings) { foreach ($field_settings as $view_mode => $display_options) { // Determine name and create initial entry in the $displays array // if it does not exist yet. $display_id = $entity_type . '.' . $bundle . '.' . $view_mode; if (!isset($displays[$display_id])) { $displays[$display_id] = _update_8000_entity_get_display($entity_type, $bundle, $view_mode); } // Set options in the display. $new_options = array('visible' => $display_options['visible']); // The display object only stores the weight for 'visible' extra // fields. if ($display_options['visible']) { $new_options['weight'] = $display_options['weight']; } $displays[$display_id]->set("content.$field_name", $new_options); // Remove the old entry. unset($variable_value['extra_fields']['display']); variable_set($variable_name, $variable_value); } } } } } // Save the displays to configuration. foreach ($displays as $config) { $config->save(); } update_config_manifest_add('entity.display', array_keys($displays)); } /** * @} End of "addtogroup updates-7.x-to-8.x". * The next series of updates should start at 9000. */