diff options
author | Dries Buytaert <dries@buytaert.net> | 2009-05-20 09:48:47 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2009-05-20 09:48:47 +0000 |
commit | ba29dbde381b8fb43f7f5ff6266ea74e119862de (patch) | |
tree | 64b4db92b95ff3c17baf90709aa12f8abdf15709 /modules/field/field.crud.inc | |
parent | 6ac0154f852a8b4637e847d9f583ed5e97c37722 (diff) | |
download | brdo-ba29dbde381b8fb43f7f5ff6266ea74e119862de.tar.gz brdo-ba29dbde381b8fb43f7f5ff6266ea74e119862de.tar.bz2 |
- Patch #415044 by bjaspan, yched: indexes for field storage.
Diffstat (limited to 'modules/field/field.crud.inc')
-rw-r--r-- | modules/field/field.crud.inc | 101 |
1 files changed, 83 insertions, 18 deletions
diff --git a/modules/field/field.crud.inc b/modules/field/field.crud.inc index 9daeebd51..5e7d20c69 100644 --- a/modules/field/field.crud.inc +++ b/modules/field/field.crud.inc @@ -65,6 +65,12 @@ * exactly like Schema API column specifications but, depending on * the field storage module in use, the name of the column may not * represent an actual column in an SQL database. + * - indexes (array). + * An array of indexes on data columns, using the same definition format + * as Schema API index specifications. Only columns that appear in the + * 'columns' setting are allowed. Note that field types can specify + * default indexes, which can be modified or added to when + * creating a field. * - settings (array) * A sub-array of key/value pairs of field-type-specific settings. Each * field type module defines and documents its own field settings. @@ -172,12 +178,25 @@ */ /** - * Create a field. This function does not bind the field to any - * bundle; use field_create_instance for that. + * Create a field. + * + * This function does not bind the field to any bundle; use + * field_create_instance() for that. * * @param $field - * A field structure. The field_name and type properties are - * required. + * A field structure. The field_name and type properties are required. + * Other properties, if omitted, will be given the following default values: + * - cardinality: 1 + * - locked: FALSE + * - indexes: the field-type indexes, specified by the field type's + * hook_field_schema(). The indexes specified in $field are added + * to those default indexes. It is possible to override the + * definition of a field-type index by providing an index with the + * same name, or to remove it by redefining it as an empty array + * of columns. Overriding field-type indexes should be done + * carefully, for it might seriously affect the site's performance. + * - settings: each omitted setting is given the default value defined in + * hook_field_info(). * @throw * FieldException */ @@ -215,18 +234,37 @@ function field_create_field($field) { 'locked' => FALSE, 'settings' => array(), ); - $module = $field_type['module']; // Create all per-field-type properties (needed here as long as we have // settings that impact column definitions). $field['settings'] += field_info_field_settings($field['type']); - $field['module'] = $module; + $field['module'] = $field_type['module']; $field['active'] = 1; $field['deleted'] = 0; - // Create the data table. We need to populate the field columns, even though - // we don't actually store them. - $field['columns'] = (array) module_invoke($field['module'], 'field_columns', $field); + + // Collect storage information. + $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 += array( + 'indexes' => array(), + ); + $field['indexes'] += $schema['indexes']; + + // Inform the storage engine. module_invoke(variable_get('field_storage_module', 'field_sql_storage'), 'field_storage_create_field', $field); + // 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['locked'], $data['module'], $data['active'], $data['deleted']); + $field['data'] = $data; + drupal_write_record('field_config', $field); // Clear caches @@ -285,13 +323,17 @@ function field_read_fields($params = array(), $include_additional = array()) { $fields = array(); $results = $query->execute(); foreach ($results as $field) { - // drupal_write_record() writes an empty string for empty arrays. - $field['settings'] = $field['settings'] ? unserialize($field['settings']) : array(); + // Extract serialized data. + $data = unserialize($field['data']); + unset($field['data']); + $field += $data; module_invoke_all('field_read_field', $field); - // Populate storage columns. - $field['columns'] = (array) module_invoke($field['module'], 'field_columns', $field); + // Populate storage information. + $schema = (array) module_invoke($field['module'], 'field_schema', $field); + $schema += array('columns' => array(), 'indexes' => array()); + $field['columns'] = $schema['columns']; $fields[$field['field_name']] = $field; } @@ -326,8 +368,29 @@ function field_delete_field($field_name) { * Creates an instance of a field, binding it to a bundle. * * @param $instance - * A field instance structure. The field_name and bundle properties - * are required. + * A field instance structure. The field_name and bundle properties are + * required. + * Other properties, if omitted, will be given the following default values: + * - label: the field name + * - description: empty string + * - weight: 0 + * - required: FALSE + * - default_value_function: empty string + * - settings: each omitted setting is given the default value specified in + * hook_field_info(). + * - widget: + * - type: the default widget specified in hook_field_info(). + * - settings: each omitted setting is given the default value specified in + * hook_field_widget_info(). + * - display: + * Settings for the 'full' build mode will be added, and each build mode + * will be completed with the follwong default values: + * - label: 'above' + * - exclude: FALSE + * TODO This is subject to change, see: http://drupal.org/node/367215 + * - type: the default formatter specified in hook_field_info(). + * - settings: each omitted setting is given the default value specified in + * hook_field_formatter_info(). * @throw * FieldException */ @@ -465,8 +528,9 @@ function _field_write_instance($instance, $update = FALSE) { $instance['display'][$context]['settings'] += field_info_formatter_settings($instance['display'][$context]['type']); } - // Create $data to contain everything from $instance that does not - // have its own column, and thus will be stored serialized. + // 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['bundle'], $data['widget']['type'], $data['weight'], $data['deleted']); @@ -486,7 +550,8 @@ function _field_write_instance($instance, $update = FALSE) { if ($update) { $record['id'] = $instance['id']; $primary_key = array('id'); - } else { + } + else { $primary_key = array(); } drupal_write_record('field_config_instance', $record, $primary_key); |