diff --git includes/form.inc includes/form.inc index 5a81c24..3d1800e 100644 --- includes/form.inc +++ includes/form.inc @@ -193,7 +193,7 @@ function drupal_build_form($form_id, &$form_state) { // Record the filepath of the include file containing the original form, // so the form builder callbacks can be loaded when the form is being // rebuilt from cache on a different path (such as 'system/ajax'). See - // form_get_cache(). + // form_get_cache(). // $menu_get_item() is not available at installation time. if (!isset($form_state['build_info']['file']) && !defined('MAINTENANCE_MODE')) { $item = menu_get_item(); @@ -1293,9 +1293,13 @@ function form_builder($form_id, $element, &$form_state) { // Don't squash existing parents value. if (!isset($element[$key]['#parents'])) { - // Check to see if a tree of child elements is present. If so, - // continue down the tree if required. - $element[$key]['#parents'] = $element[$key]['#tree'] && $element['#tree'] ? array_merge($element['#parents'], array($key)) : array($key); + $element[$key]['#parents'] = $element['#parents']; + // Check to see if a tree of child elements is present. If not, remove + // the parent element from #parents, but keep any other ancestors. + if (!$element['#tree']) { + array_pop($element[$key]['#parents']); + } + $element[$key]['#parents'][] = $key; } // Ensure #array_parents follows the actual form structure. $array_parents = isset($element['#array_parents']) ? $element['#array_parents'] : array(); diff --git modules/image/image.admin.inc modules/image/image.admin.inc index 508f771..7265e33 100644 --- modules/image/image.admin.inc +++ modules/image/image.admin.inc @@ -156,7 +156,7 @@ function image_style_form($form, &$form_state, $style) { * Validate handler for adding a new image effect to an image style. */ function image_style_form_add_validate($form, &$form_state) { - if (!$form_state['values']['new']) { + if (!$form_state['values']['effects']['new']) { form_error($form['effects']['new']['new'], t('Select an effect to add.')); $form_state['rebuild'] = TRUE; } @@ -168,17 +168,17 @@ function image_style_form_add_validate($form, &$form_state) { function image_style_form_add_submit($form, &$form_state) { $style = $form_state['image_style']; // Check if this field has any configuration options. - $effect = image_effect_definition_load($form_state['values']['new']); + $effect = image_effect_definition_load($form_state['values']['effects']['new']); // Load the configuration form for this option. if (isset($effect['form callback'])) { - $path = 'admin/config/media/image-styles/edit/' . $form_state['image_style']['name'] . '/add/' . $form_state['values']['new']; - $form_state['redirect'] = array($path, array('query' => array('weight' => $form_state['values']['weight']))); + $path = 'admin/config/media/image-styles/edit/' . $form_state['image_style']['name'] . '/add/' . $form_state['values']['effects']['new']; + $form_state['redirect'] = array($path, array('query' => array('weight' => $form_state['values']['effects']['weight']))); } // If there's no form, immediately add the image effect. else { $effect['isid'] = $style['isid']; - $effect['weight'] = $form_state['values']['weight']; + $effect['weight'] = $form_state['values']['effects']['weight']; image_effect_save($effect); drupal_set_message(t('The image effect was successfully applied.')); } diff --git modules/image/image.test modules/image/image.test index 9a7bceb..aa0d4c8 100644 --- modules/image/image.test +++ modules/image/image.test @@ -341,7 +341,7 @@ class ImageAdminStylesUnitTest extends DrupalWebTestCase { // Add each sample effect to the style. foreach ($effect_edits as $effect => $edit) { // Add the effect. - $this->drupalPost($style_path, array('new' => $effect), t('Add')); + $this->drupalPost($style_path, array('effects[new]' => $effect), t('Add')); if (!empty($edit)) { $this->drupalPost(NULL, $edit, t('Add effect')); } @@ -475,7 +475,7 @@ class ImageAdminStylesUnitTest extends DrupalWebTestCase { $this->assertRaw(t('The %style style has been overridden, allowing you to change its settings.', array('%style' => $style_name)), t('Default image style may be overridden.')); // Add sample effect to the overridden style. - $this->drupalPost($edit_path, array('new' => 'image_desaturate'), t('Add')); + $this->drupalPost($edit_path, array('effects[new]' => 'image_desaturate'), t('Add')); drupal_static_reset('image_styles'); $style = image_style_load($style_name); diff --git modules/simpletest/tests/form.test modules/simpletest/tests/form.test index 842f325..283be8d 100644 --- modules/simpletest/tests/form.test +++ modules/simpletest/tests/form.test @@ -978,7 +978,6 @@ class FormsClickedButtonTestCase extends DrupalWebTestCase { } } - /** * Tests rebuilding of arbitrary forms by altering them. */ @@ -1043,3 +1042,32 @@ class FormsArbitraryRebuildTestCase extends DrupalWebTestCase { $this->assertFieldByName('mail', 'bar@example.com', 'Entered mail address has been kept.'); } } + +/** + * Tests the #tree property. + */ +class FormsTreeValueTestCase extends DrupalWebTestCase { + + public static function getInfo() { + return array( + 'name' => 'Form values tree', + 'description' => 'Tests using the tree property to build the form values.', + 'group' => 'Form API', + ); + } + + function setUp() { + parent::setUp('form_test'); + } + + /** + * Tests the tree value form to make sure the value is under the right parent. + */ + function testTreeValues() { + $edit = array( + 'parent[value]' => 'DEFAULT', + ); + $this->drupalPost('form-test/tree-values', $edit, t('Save')); + $this->assertText('Value is under parent.'); + } +} diff --git modules/simpletest/tests/form_test.module modules/simpletest/tests/form_test.module index fb01444..0f9a63a 100644 --- modules/simpletest/tests/form_test.module +++ modules/simpletest/tests/form_test.module @@ -126,6 +126,14 @@ function form_test_menu() { 'type' => MENU_CALLBACK, ); + $items['form-test/tree-values'] = array( + 'title' => 'Tree values test', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('form_test_tree_values'), + 'access callback' => TRUE, + 'type' => MENU_CALLBACK, + ); + return $items; } @@ -1035,3 +1043,36 @@ function form_test_user_register_form_rebuild($form, &$form_state) { drupal_set_message('Form rebuilt.'); $form_state['rebuild'] = TRUE; } + +/** + * Form constructor for the #tree property test form. + */ +function form_test_tree_values($form, $form_state) { + $form['parent'] = array( + '#title' => 'Parent', + '#type' => 'fieldset', + '#tree' => TRUE, + ); + $form['parent']['wrapper'] = array( + '#type' => 'container', + '#tree' => FALSE, + ); + $form['parent']['wrapper']['value'] = array( + '#type' => 'textfield', + '#default_value' => 'DEFAULT', + ); + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Save'), + ); + return $form; +} + +/** + * Submit callback for the #tree property test form. + */ +function form_test_tree_values_submit($form, $form_state) { + if ($form_state['values']['parent']['value'] == 'DEFAULT') { + drupal_set_message('Value is under parent.'); + } +}