diff --git a/modules/field/field.info.inc b/modules/field/field.info.inc index f435d99a6357d981e3d4e5a77e34275880a590a4..c3b29573f856c48453cae86ded2fe8fc884a33a4 100644 --- a/modules/field/field.info.inc +++ b/modules/field/field.info.inc @@ -24,6 +24,8 @@ * are affected. */ function field_info_cache_clear() { + drupal_static_reset('field_view_mode_settings'); + // @todo: Remove this when field_attach_*_bundle() bundle management // functions are moved to the entity API. entity_info_cache_clear(); @@ -273,7 +275,7 @@ function _field_info_prepare_field($field) { * Prepares an instance definition for the current run-time context. * * Since the instance was last saved or updated, a number of things might have - * changed: widgets or formatters disabled, new settings expected, new build + * changed: widgets or formatters disabled, new settings expected, new view * modes added... * * @param $instance @@ -301,16 +303,21 @@ function _field_info_prepare_instance($instance, $field) { $instance['display'][$view_mode] = _field_info_prepare_instance_display($field, $display); } - // Fallback to 'hidden' for unspecified view modes. + // Fallback to 'hidden' for view modes configured to use custom display + // settings, and for which the instance has no explicit settings. $entity_info = entity_get_info($instance['entity_type']); - foreach ($entity_info['view modes'] as $view_mode => $info) { - if (!isset($instance['display'][$view_mode])) { - $instance['display'][$view_mode] = array( - 'type' => 'hidden', - 'label' => 'above', - 'settings' => array(), - 'weight' => 0, - ); + $view_modes = array_merge(array('default'), array_keys($entity_info['view modes'])); + $view_mode_settings = field_view_mode_settings($instance['entity_type'], $instance['bundle']); + foreach ($view_modes as $view_mode) { + if ($view_mode == 'default' || !empty($view_mode_settings[$view_mode]['custom_settings'])) { + if (!isset($instance['display'][$view_mode])) { + $instance['display'][$view_mode] = array( + 'type' => 'hidden', + 'label' => 'above', + 'settings' => array(), + 'weight' => 0, + ); + } } } diff --git a/modules/field/field.module b/modules/field/field.module index 823b0f24c010f94576bb34dad9f9c5c71cc5b5f5..d3545d911077873594e3bab213ddfb4c803afeb1 100644 --- a/modules/field/field.module +++ b/modules/field/field.module @@ -369,7 +369,7 @@ function _field_sort_items_value_helper($a, $b) { * 'extra_field_2' => ... * ), * ), - * ), + * ); * @endcode * * @param $entity_type @@ -387,8 +387,8 @@ function field_bundle_settings($entity_type, $bundle, $settings = NULL) { if (isset($settings)) { $stored_settings[$entity_type][$bundle] = $settings; + variable_set('field_bundle_settings', $stored_settings); - drupal_static_reset('field_view_mode_settings'); field_info_cache_clear(); } else { @@ -397,6 +397,10 @@ function field_bundle_settings($entity_type, $bundle, $settings = NULL) { 'view_modes' => array(), 'extra_fields' => array(), ); + $settings['extra_fields'] += array( + 'form' => array(), + 'display' => array(), + ); return $settings; } diff --git a/modules/field_ui/field_ui.admin.inc b/modules/field_ui/field_ui.admin.inc index 27ff1e03ad69617335a5900c9f647f6ee8a9640c..c88a51251d05725fb02c9dc4d27f961a2492a9f2 100644 --- a/modules/field_ui/field_ui.admin.inc +++ b/modules/field_ui/field_ui.admin.inc @@ -1267,19 +1267,23 @@ function field_ui_display_overview_form_submit($form, &$form_state) { // Save data for 'regular' fields. foreach ($form['#fields'] as $field_name) { - $instance = field_info_instance($entity_type, $field_name, $bundle); + // Retrieve the stored instance settings to merge with the incoming values. + $instance = field_read_instance($entity_type, $field_name, $bundle); $values = $form_values['fields'][$field_name]; // Get formatter settings. They lie either directly in submitted form // values (if the whole form was submitted while some formatter // settings were being edited), or have been persisted in // $form_state. - $settings = $instance['display'][$view_mode]['settings']; + $settings = array(); if (isset($values['settings_edit_form']['settings'])) { $settings = $values['settings_edit_form']['settings']; } elseif (isset($form_state['formatter_settings'][$field_name])) { $settings = $form_state['formatter_settings'][$field_name]; } + elseif (isset($instance['display'][$view_mode]['settings'])) { + $settings = $instance['display'][$view_mode]['settings']; + } // Only save settings actually used by the selected formatter. $default_settings = field_info_formatter_settings($values['type']); @@ -1311,10 +1315,14 @@ function field_ui_display_overview_form_submit($form, &$form_state) { foreach ($form_values['view_modes_custom'] as $view_mode_name => $value) { // Display a message for each view mode newly configured to use custom // settings. - if (!empty($value) && empty($bundle_settings['view_modes'][$view_mode_name]['custom_settings'])) { + $view_mode_settings = field_view_mode_settings($entity_type, $bundle); + if (!empty($value) && empty($view_mode_settings[$view_mode_name]['custom_settings'])) { $view_mode_label = $entity_info['view modes'][$view_mode_name]['label']; $path = _field_ui_bundle_admin_path($entity_type, $bundle) . "/display/$view_mode_name"; drupal_set_message(t('The %view_mode mode now uses custom display settings. You might want to configure them.', array('%view_mode' => $view_mode_label, '@url' => url($path)))); + // Initialize the newly customized view mode with the display settings + // from the default view mode. + _field_ui_add_default_view_mode_settings($entity_type, $bundle, $view_mode_name, $bundle_settings); } $bundle_settings['view_modes'][$view_mode_name]['custom_settings'] = !empty($value); } @@ -1326,6 +1334,50 @@ function field_ui_display_overview_form_submit($form, &$form_state) { drupal_set_message(t('Your settings have been saved.')); } +/** + * Helper function for field_ui_display_overview_form_submit(). + * + * When an administrator decides to use custom display settings for a view mode, + * that view mode needs to be initialized with the display settings for the + * 'default' view mode, which it was previously using. This helper function + * adds the new custom display settings to this bundle's instances, and saves + * them. It also modifies the passed-in $settings array, which the caller can + * then save using field_bundle_settings(). + * + * @see field_bundle_settings() + * + * @param $entity_type + * The bundle's entity type. + * @param $bundle + * The bundle whose view mode is being customized. + * @param $view_mode + * The view mode that the administrator has set to use custom settings. + * @param $settings + * An associative array of bundle settings, as expected by + * field_bundle_settings(). + */ +function _field_ui_add_default_view_mode_settings($entity_type, $bundle, $view_mode, &$settings) { + // Update display settings for field instances. + $instances = field_read_instances(array('entity_type' => $entity_type, 'bundle' => $bundle)); + foreach ($instances as $instance) { + // If this field instance has display settings defined for this view mode, + // respect those settings. + if (!isset($instance['display'][$view_mode])) { + // The instance doesn't specify anything for this view mode, so use the + // default display settings. + $instance['display'][$view_mode] = $instance['display']['default']; + field_update_instance($instance); + } + } + + // Update display settings for 'extra fields'. + foreach (array_keys($settings['extra_fields']['display']) as $name) { + if (!isset($settings['extra_fields']['display'][$name][$view_mode])) { + $settings['extra_fields']['display'][$name][$view_mode] = $settings['extra_fields']['display'][$name]['default']; + } + } +} + /** * Return an array of field_type options. */ @@ -1537,17 +1589,24 @@ function field_ui_field_settings_form_submit($form, &$form_state) { * Menu callback; select a widget for the field. */ function field_ui_widget_type_form($form, &$form_state, $instance) { + drupal_set_title($instance['label']); + $bundle = $instance['bundle']; $entity_type = $instance['entity_type']; - $field = field_info_field($instance['field_name']); - - drupal_set_title($instance['label']); + $field_name = $instance['field_name']; + $field = field_info_field($field_name); $field_type = field_info_field_types($field['type']); $widget_type = field_info_widget_types($instance['widget']['type']); $bundles = field_info_bundles(); $bundle_label = $bundles[$entity_type][$bundle]['label']; + $form = array( + '#bundle' => $bundle, + '#entity_type' => $entity_type, + '#field_name' => $field_name, + ); + $form['basic'] = array( '#type' => 'fieldset', '#title' => t('Change widget'), @@ -1561,7 +1620,6 @@ function field_ui_widget_type_form($form, &$form_state, $instance) { '#description' => t('The type of form element you would like to present to the user when creating this field in the %type type.', array('%type' => $bundle_label)), ); - $form['#instance'] = $instance; $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Continue')); @@ -1576,9 +1634,12 @@ function field_ui_widget_type_form($form, &$form_state, $instance) { */ function field_ui_widget_type_form_submit($form, &$form_state) { $form_values = $form_state['values']; - $instance = $form['#instance']; - $bundle = $instance['bundle']; - $entity_type = $instance['entity_type']; + $bundle = $form['#bundle']; + $entity_type = $form['#entity_type']; + $field_name = $form['#field_name']; + + // Retrieve the stored instance settings to merge with the incoming values. + $instance = field_read_instance($entity_type, $field_name, $bundle); // Set the right module information. $widget_type = field_info_widget_types($form_values['widget_type']); @@ -1586,6 +1647,7 @@ function field_ui_widget_type_form_submit($form, &$form_state) { $instance['widget']['type'] = $form_values['widget_type']; $instance['widget']['module'] = $widget_module; + try { field_update_instance($instance); drupal_set_message(t('Changed the widget for field %label.', array('%label' => $instance['label']))); @@ -1912,8 +1974,8 @@ function field_ui_field_edit_form_submit($form, &$form_state) { field_default_submit(NULL, NULL, $field, $instance, LANGUAGE_NONE, $items, $form, $form_state); $instance['default_value'] = $items ? $items : NULL; - // Update the instance settings. - $instance_source = field_info_instance($instance['entity_type'], $instance['field_name'], $instance['bundle']); + // Retrieve the stored instance settings to merge with the incoming values. + $instance_source = field_read_instance($instance['entity_type'], $instance['field_name'], $instance['bundle']); $instance = array_merge($instance_source, $instance); field_update_instance($instance); diff --git a/modules/field_ui/field_ui.test b/modules/field_ui/field_ui.test index 43649df287518ed529bb7d271af2b3c2386e0a14..f1102488c5364b7db93d9da6745ba0a07748b816 100644 --- a/modules/field_ui/field_ui.test +++ b/modules/field_ui/field_ui.test @@ -470,4 +470,151 @@ class FieldUIManageDisplayTestCase extends FieldUITestCase { $this->assertEqual($current_format, $format, t('The formatter was updated.')); $this->assertEqual($current_setting_value, $setting_value, t('The setting was updated.')); } + + /** + * Test switching view modes to use custom or 'default' settings'. + */ + function testViewModeCustom() { + // Create a field, and a node with some data for the field. + $edit = array( + 'fields[_add_new_field][label]' => 'Test field', + 'fields[_add_new_field][field_name]' => 'field_test', + ); + $this->fieldUIAddNewField('admin/structure/types/manage/' . $this->hyphen_type, $edit); + $value = rand(1, 100); + $settings = array( + 'type' => $this->type, + 'field_test' => array(LANGUAGE_NONE => array(array('value' => $value))), + ); + $node = $this->drupalCreateNode($settings); + + // Gather expected output values with the various formatters. + $formatters = field_info_formatter_types(); + $output = array( + 'field_test_default' => $formatters['field_test_default']['settings']['test_formatter_setting'] . '|' . $value, + 'field_test_with_prepare_view' => $formatters['field_test_with_prepare_view']['settings']['test_formatter_setting_additional'] . '|' . $value. '|' . ($value + 1), + ); + + // Check that the field is displayed with the default formatter in 'rss' + // mode (uses 'default'), and hidden in 'teaser' mode (uses custom settings). + $this->assertNodeViewText($node, 'rss', $output['field_test_default'], t("The field is displayed as expected in view modes that use 'default' settings.")); + $this->assertNodeViewNoText($node, 'teaser', $value, t("The field is hidden in view modes that use custom settings.")); + + // Change fomatter for 'default' mode, check that the field is displayed + // accordingly in 'rss' mode. + $edit = array( + 'fields[field_test][type]' => 'field_test_with_prepare_view', + ); + $this->drupalPost('admin/structure/types/manage/' . $this->hyphen_type . '/display', $edit, t('Save')); + $this->assertNodeViewText($node, 'rss', $output['field_test_with_prepare_view'], t("The field is displayed as expected in view modes that use 'default' settings.")); + + // Specialize the 'rss' mode, check that the field is displayed the same. + $edit = array( + "view_modes_custom[rss]" => TRUE, + ); + $this->drupalPost('admin/structure/types/manage/' . $this->hyphen_type . '/display', $edit, t('Save')); + $this->assertNodeViewText($node, 'rss', $output['field_test_with_prepare_view'], t("The field is displayed as expected in newly specialized 'rss' mode.")); + + // Set the field to 'hidden' in the view mode, check that the field is + // hidden. + $edit = array( + 'fields[field_test][type]' => 'hidden', + ); + $this->drupalPost('admin/structure/types/manage/' . $this->hyphen_type . '/display/rss', $edit, t('Save')); + $this->assertNodeViewNoText($node, 'rss', $value, t("The field is hidden in 'rss' mode.")); + + // Set the view mode back to 'default', check that the field is displayed + // accordingly. + $edit = array( + "view_modes_custom[rss]" => FALSE, + ); + $this->drupalPost('admin/structure/types/manage/' . $this->hyphen_type . '/display', $edit, t('Save')); + $this->assertNodeViewText($node, 'rss', $output['field_test_with_prepare_view'], t("The field is displayed as expected when 'rss' mode is set back to 'default' settings.")); + + // Specialize the view mode again. + $edit = array( + "view_modes_custom[rss]" => TRUE, + ); + $this->drupalPost('admin/structure/types/manage/' . $this->hyphen_type . '/display', $edit, t('Save')); + // Check that the previous settings for the view mode have been kept. + $this->assertNodeViewNoText($node, 'rss', $value, t("The previous settings are kept when 'rss' mode is specialized again.")); + } + + /** + * Pass if the text is found in the rendered node in a given view mode. + * + * @param $node + * The node. + * @param $view_mode + * The view mode in which the node should be displayed. + * @param $text + * Plain text to look for. + * @param $message + * Message to display. + * + * @return + * TRUE on pass, FALSE on fail. + */ + function assertNodeViewText($node, $view_mode, $text, $message) { + return $this->assertNodeViewTextHelper($node, $view_mode, $text, $message, FALSE); + } + + /** + * Pass if the text is node found in the rendered node in a given view mode. + * + * @param $node + * The node. + * @param $view_mode + * The view mode in which the node should be displayed. + * @param $text + * Plain text to look for. + * @param $message + * Message to display. + * @return + * TRUE on pass, FALSE on fail. + */ + function assertNodeViewNoText($node, $view_mode, $text, $message) { + return $this->assertNodeViewTextHelper($node, $view_mode, $text, $message, TRUE); + } + + /** + * Helper for assertNodeViewText and assertNodeViewNoText. + * + * @param $node + * The node. + * @param $view_mode + * The view mode in which the node should be displayed. + * @param $text + * Plain text to look for. + * @param $message + * Message to display. + * @param $not_exists + * TRUE if this text should not exist, FALSE if it should. + * + * @return + * TRUE on pass, FALSE on fail. + */ + function assertNodeViewTextHelper($node, $view_mode, $text, $message, $not_exists) { + // Make sure caches on the tester side are refreshed after changes + // submitted on the tested side. + field_info_cache_clear(); + + // Save current content so that we can restore it when we're done. + $old_content = $this->drupalGetContent(); + + // Render a cloned node, so that we do not alter the original. + $clone = clone $node; + $output = drupal_render(node_view($clone, $view_mode)); + $this->verbose(t('Rendered node - view mode: @view_mode', array('@view_mode' => $view_mode)) . '
'. $output); + + // Assign content so that DrupalWebTestCase functions can be used. + $this->drupalSetContent($output); + $method = ($not_exists ? 'assertNoText' : 'assertText'); + $return = $this->{$method}((string) $text, $message); + + // Restore previous content. + $this->drupalSetContent($old_content); + + return $return; + } }