summaryrefslogtreecommitdiff
path: root/modules/field_ui
diff options
context:
space:
mode:
authorAngie Byron <webchick@24967.no-reply.drupal.org>2010-10-23 00:54:39 +0000
committerAngie Byron <webchick@24967.no-reply.drupal.org>2010-10-23 00:54:39 +0000
commit21b41548ccf1c83ca480d216a51f30222d15cd0a (patch)
treebd3880a61175fae12a406cb3364b79563174bd1f /modules/field_ui
parent85dd04fec51a64b4f7bc9ede93b313b3d5daf8ef (diff)
downloadbrdo-21b41548ccf1c83ca480d216a51f30222d15cd0a.tar.gz
brdo-21b41548ccf1c83ca480d216a51f30222d15cd0a.tar.bz2
#886152 by yched, pwolanin, hefox, chx, rszrama, ksenzee: Fixed All fields are hidden after the administrator newly configures a view mode to not use 'default' display.
Diffstat (limited to 'modules/field_ui')
-rw-r--r--modules/field_ui/field_ui.admin.inc86
-rw-r--r--modules/field_ui/field_ui.test147
2 files changed, 221 insertions, 12 deletions
diff --git a/modules/field_ui/field_ui.admin.inc b/modules/field_ui/field_ui.admin.inc
index 27ff1e03a..c88a51251 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 <a href="@url">configure them</a>.', 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);
}
@@ -1327,6 +1335,50 @@ function field_ui_display_overview_form_submit($form, &$form_state) {
}
/**
+ * 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.
*/
function field_ui_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 43649df28..f1102488c 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)) . '<hr />'. $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;
+ }
}