From db09a6178ba423fe2ce85317afaca5c58a5b6887 Mon Sep 17 00:00:00 2001
From: Dries Buytaert
' . t('These settings apply to the %field field everywhere it is used.', array('%field' => $instance['label'])) . '
'; + $has_data = field_attach_field_has_data($field); + if ($has_data) { + $description = '' . t('These settings apply to the %field field everywhere it is used. Because the field already has data, some settings can no longer be changed.', array('%field' => $instance['label'])) . '
'; + } + else { + $description = '' . t('These settings apply to the %field field everywhere it is used.', array('%field' => $instance['label'])) . '
'; + } // Create a form structure for the field values. $form['field'] = array( @@ -1151,10 +1143,11 @@ function field_ui_field_edit_form($form, &$form_state, $obj_type, $bundle, $inst '#description' => $description, ); - // Add additional field settings from the field module. - $additions = module_invoke($field['module'], 'field_settings_form', $field, $instance); + // Add additional field settings from the field module. The field module is + // responsible for not returning settings that cannot be changed if + // the field already has data. + $additions = module_invoke($field['module'], 'field_settings_form', $field, $instance, $has_data); if (is_array($additions)) { - // @todo Filter additional settings by whether they can be changed. $form['field']['settings'] = $additions; } @@ -1289,7 +1282,7 @@ function field_ui_field_edit_form_submit($form, &$form_state) { // Update any field settings that have changed. $field = field_info_field($instance_values['field_name']); $field = array_merge($field, $field_values); - field_ui_update_field($field); + field_update_field($field); // Move the default value from the sample widget to the default value field. if (isset($instance_values['default_value_widget'])) { diff --git a/modules/field_ui/field_ui.api.php b/modules/field_ui/field_ui.api.php index 9a938fae9..9442d94eb 100644 --- a/modules/field_ui/field_ui.api.php +++ b/modules/field_ui/field_ui.api.php @@ -14,14 +14,27 @@ /** * Field settings form. * + * The field type module is responsible for not returning form elements that + * cannot be changed. It may not always be possible to tell in advance, but + * modules should attempt to inform the user what is going/likely to work. + * + * @todo: Only the field type module knows which settings will affect the + * field's schema, but only the field storage module knows what schema + * changes are permitted once a field already has data. Probably we need an + * easy way for a field type module to ask whether an update to a new schema + * will be allowed without having to build up a fake $prior_field structure + * for hook_field_update_forbid(). + * * @param $field * The field structure being configured. * @param $instance * The instance structure being configured. + * @param $has_data + * Whether the field already has data. * @return * The form definition for the field settings. */ -function hook_field_settings_form($field, $instance) { +function hook_field_settings_form($field, $instance, $has_data) { $settings = $field['settings']; $form['max_length'] = array( '#type' => 'textfield', diff --git a/modules/field_ui/field_ui.module b/modules/field_ui/field_ui.module index 0f29be6cb..c5139b496 100644 --- a/modules/field_ui/field_ui.module +++ b/modules/field_ui/field_ui.module @@ -241,33 +241,6 @@ function field_ui_field_ui_build_modes_tabs() { return $modes; } -/** - * Updates a field. - * - * Field API does not allow field updates, so we create a method here to - * update a field if no data is created yet. - * - * @see field_create_field() - */ -function field_ui_update_field($field) { - $field_types = field_info_field_types(); - $module = $field_types[$field['type']]['module']; - - // If needed, remove the 'bundles' element added by field_info_field. - unset($field['bundles']); - - $defaults = field_info_field_settings($field['type']); - $field['settings'] = array_merge($defaults, (array) $field['settings']); - $data = $field; - unset($data['id'], $data['columns'], $data['field_name'], $data['type'], $data['locked'], $data['module'], $data['cardinality'], $data['active'], $data['deleted']); - $field['data'] = $data; - - drupal_write_record('field_config', $field, array('field_name')); - - // Clear caches. - field_cache_clear(TRUE); -} - /** * Implement hook_field_attach_create_bundle(). */ -- cgit v1.2.3