Commit 8c02d4ec authored by Dries's avatar Dries

- Patch #45530 by Morbus: filter_form shouldn't default to #weight 0

When a form element doesn't specify a #weight, it is assumed internally as #weight 0. However, to ensure that our form elements display visually *as they were defined in the array* we, in form_builder, count the number of elements, divide by 1000, and set that as the weight:

# Assign a decimal placeholder weight to preserve original array order
if (!isset($form[$key]['#weight'])) {
  $form[$key]['#weight'] = $count/1000;
}

The above code will set the #weights of elements that have not defined a weight to something like 0 (first element in array definition), 0.001, 0.002, and so on. However, anytime a form element *explicitly* defines a #weight of 0, that #weight is kept at exactly 0, which would cause that form element to appear BEFORE the elements that didn't have a #weight defined (and thus received a #weight such as 0.002).

Consider the following pseudo example:

$form['game_title'] = array(
    '#type' => 'textfield',
    ...
    );
$form['game_description'] = array(
    '#type' => 'textarea',
    ...
    );
$form['game_format'] = filter_form(variable_get('game_format', NULL));
return $form;

Here, we're not definiing weights on our two textfields. We then add an filter_form. The second parameter of the filter_form is $weight, which defaults to 0. After this $form hits form_builder, we have weights 0 (game_title), 0.001 (game_description), and 0 (filter_form) respectively. This is then sorted by weight, which causes filter_form (the third element in the array) to appear BEFORE game_description (0 is lighter than 0.001).

The short lesson is: explicitly defining #weight 0 for a form element is probably a bad idea. This patch changes the default #weight of filter_form to NULL, instead of 0, and also removes any other explicit setting of #weight to 0 in core.
parent 4a7abb95
......@@ -325,7 +325,6 @@ function block_admin_configure($module = NULL, $delta = 0) {
'#type' => 'fieldset',
'#title' => t('Block specific settings'),
'#collapsible' => true,
'#weight' => 0,
);
foreach ($settings as $k => $v) {
......@@ -343,7 +342,6 @@ function block_admin_configure($module = NULL, $delta = 0) {
'#type' => 'fieldset',
'#title' => t('User specific visibility settings'),
'#collapsible' => true,
'#weight' => 0,
);
$form['user_vis_settings']['custom'] = array(
'#type' => 'radios',
......@@ -355,7 +353,6 @@ function block_admin_configure($module = NULL, $delta = 0) {
'#type' => 'fieldset',
'#title' => t('Page specific visibility settings'),
'#collapsible' => true,
'#weight' => 0,
);
$access = user_access('use PHP for block visibility');
......
......@@ -325,7 +325,6 @@ function block_admin_configure($module = NULL, $delta = 0) {
'#type' => 'fieldset',
'#title' => t('Block specific settings'),
'#collapsible' => true,
'#weight' => 0,
);
foreach ($settings as $k => $v) {
......@@ -343,7 +342,6 @@ function block_admin_configure($module = NULL, $delta = 0) {
'#type' => 'fieldset',
'#title' => t('User specific visibility settings'),
'#collapsible' => true,
'#weight' => 0,
);
$form['user_vis_settings']['custom'] = array(
'#type' => 'radios',
......@@ -355,7 +353,6 @@ function block_admin_configure($module = NULL, $delta = 0) {
'#type' => 'fieldset',
'#title' => t('Page specific visibility settings'),
'#collapsible' => true,
'#weight' => 0,
);
$access = user_access('use PHP for block visibility');
......
......@@ -230,7 +230,7 @@ function blog_form(&$node) {
$form['title'] = array('#type' => 'textfield', '#title' => t('Title'), '#required' => TRUE, '#default_value' => $node->title, '#weight' => -5);
$form['body_filter']['body'] = array('#type' => 'textarea', '#title' => t('Body'), '#default_value' => $node->body, '#rows' => 20, '#required' => TRUE);
$form['body_filter']['filter'] = filter_form($node->format, 1);
$form['body_filter']['filter'] = filter_form($node->format);
return $form;
}
......
......@@ -230,7 +230,7 @@ function blog_form(&$node) {
$form['title'] = array('#type' => 'textfield', '#title' => t('Title'), '#required' => TRUE, '#default_value' => $node->title, '#weight' => -5);
$form['body_filter']['body'] = array('#type' => 'textarea', '#title' => t('Body'), '#default_value' => $node->body, '#rows' => 20, '#required' => TRUE);
$form['body_filter']['filter'] = filter_form($node->format, 1);
$form['body_filter']['filter'] = filter_form($node->format);
return $form;
}
......
......@@ -253,7 +253,7 @@ function book_form(&$node) {
$form['title'] = array('#type' => 'textfield', '#title' => t('Title'), '#required' => TRUE, '#default_value' => $node->title, '#weight' => -5);
$form['body_filter']['body'] = array('#type' => 'textarea', '#title' => t('Body'), '#default_value' => $node->body, '#rows' => 20, '#required' => TRUE);
$form['body_filter']['format'] = filter_form($node->format, 1);
$form['body_filter']['format'] = filter_form($node->format);
$form['log'] = array(
'#type' => 'textarea', '#title' => t('Log message'), '#default_value' => $node->log, '#weight' => 5,
......
......@@ -253,7 +253,7 @@ function book_form(&$node) {
$form['title'] = array('#type' => 'textfield', '#title' => t('Title'), '#required' => TRUE, '#default_value' => $node->title, '#weight' => -5);
$form['body_filter']['body'] = array('#type' => 'textarea', '#title' => t('Body'), '#default_value' => $node->body, '#rows' => 20, '#required' => TRUE);
$form['body_filter']['format'] = filter_form($node->format, 1);
$form['body_filter']['format'] = filter_form($node->format);
$form['log'] = array(
'#type' => 'textarea', '#title' => t('Log message'), '#default_value' => $node->log, '#weight' => 5,
......
......@@ -343,7 +343,6 @@ function comment_configure() {
'#title' => t('Viewing options'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => 0,
);
$form['viewing_options']['comment_default_mode'] = array(
......@@ -387,7 +386,6 @@ function comment_configure() {
'#title' => t('Posting settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => 0,
);
$form['posting_settings']['comment_anonymous'] = array(
......@@ -1333,7 +1331,7 @@ function comment_form($edit, $title = NULL) {
}
$form['comment_filter']['comment'] = array('#type' => 'textarea', '#title' => t('Comment'), '#rows' => 15, '#default_value' => $edit['comment'] ? $edit['comment'] : $user->signature, '#required' => TRUE);
$form['comment_filter']['format'] = filter_form($edit['format'], 1);
$form['comment_filter']['format'] = filter_form($edit['format']);
$form['cid'] = array('#type' => 'value', '#value' => $edit['cid']);
$form['pid'] = array('#type' => 'value', '#value' => $edit['pid']);
......
......@@ -343,7 +343,6 @@ function comment_configure() {
'#title' => t('Viewing options'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => 0,
);
$form['viewing_options']['comment_default_mode'] = array(
......@@ -387,7 +386,6 @@ function comment_configure() {
'#title' => t('Posting settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => 0,
);
$form['posting_settings']['comment_anonymous'] = array(
......@@ -1333,7 +1331,7 @@ function comment_form($edit, $title = NULL) {
}
$form['comment_filter']['comment'] = array('#type' => 'textarea', '#title' => t('Comment'), '#rows' => 15, '#default_value' => $edit['comment'] ? $edit['comment'] : $user->signature, '#required' => TRUE);
$form['comment_filter']['format'] = filter_form($edit['format'], 1);
$form['comment_filter']['format'] = filter_form($edit['format']);
$form['cid'] = array('#type' => 'value', '#value' => $edit['cid']);
$form['pid'] = array('#type' => 'value', '#value' => $edit['pid']);
......
......@@ -750,7 +750,7 @@ function check_markup($text, $format = FILTER_FORMAT_DEFAULT, $check = TRUE) {
* @return
* HTML for the form element.
*/
function filter_form($value = FILTER_FORMAT_DEFAULT, $weight = 0, $parents = array('format')) {
function filter_form($value = FILTER_FORMAT_DEFAULT, $weight = NULL, $parents = array('format')) {
if ($value == FILTER_FORMAT_DEFAULT) {
$value = variable_get('filter_default_format', 1);
}
......
......@@ -750,7 +750,7 @@ function check_markup($text, $format = FILTER_FORMAT_DEFAULT, $check = TRUE) {
* @return
* HTML for the form element.
*/
function filter_form($value = FILTER_FORMAT_DEFAULT, $weight = 0, $parents = array('format')) {
function filter_form($value = FILTER_FORMAT_DEFAULT, $weight = NULL, $parents = array('format')) {
if ($value == FILTER_FORMAT_DEFAULT) {
$value = variable_get('filter_default_format', 1);
}
......
......@@ -593,7 +593,7 @@ function forum_form(&$node) {
}
$form['body_filter']['body'] = array('#type' => 'textarea', '#title' => t('Body'), '#default_value' => $node->body, '#rows' => 20, '#required' => TRUE);
$form['body_filter']['format'] = filter_form($node->format, 1);
$form['body_filter']['format'] = filter_form($node->format);
return $form;
}
......
......@@ -593,7 +593,7 @@ function forum_form(&$node) {
}
$form['body_filter']['body'] = array('#type' => 'textarea', '#title' => t('Body'), '#default_value' => $node->body, '#rows' => 20, '#required' => TRUE);
$form['body_filter']['format'] = filter_form($node->format, 1);
$form['body_filter']['format'] = filter_form($node->format);
return $form;
}
......
......@@ -1632,9 +1632,6 @@ function node_form_array($node) {
if (!isset($form['title']['#weight'])) {
$form['title']['#weight'] = -5;
}
if (!isset($form['body']['#weight'])) {
$form['body']['#weight'] = 0;
}
// If this is a new node, fill in the default values.
$node_options = variable_get('node_options_'. $node->type, array('status', 'promote'));
......
......@@ -1632,9 +1632,6 @@ function node_form_array($node) {
if (!isset($form['title']['#weight'])) {
$form['title']['#weight'] = -5;
}
if (!isset($form['body']['#weight'])) {
$form['body']['#weight'] = 0;
}
// If this is a new node, fill in the default values.
$node_options = variable_get('node_options_'. $node->type, array('status', 'promote'));
......
......@@ -94,7 +94,7 @@ function page_form(&$node) {
$form['title'] = array('#type' => 'textfield', '#title' => t('Title'), '#required' => TRUE, '#default_value' => $node->title, '#weight' => -5);
$form['body_filter']['body'] = array('#type' => 'textarea', '#title' => t('Body'), '#default_value' => $node->body, '#rows' => 20, '#required' => TRUE);
$form['body_filter']['format'] = filter_form($node->format, 1);
$form['body_filter']['format'] = filter_form($node->format);
$form['log'] = array(
'#type' => 'textarea', '#title' => t('Log message'), '#default_value' => $node->log, '#weight' => 5,
......
......@@ -94,7 +94,7 @@ function page_form(&$node) {
$form['title'] = array('#type' => 'textfield', '#title' => t('Title'), '#required' => TRUE, '#default_value' => $node->title, '#weight' => -5);
$form['body_filter']['body'] = array('#type' => 'textarea', '#title' => t('Body'), '#default_value' => $node->body, '#rows' => 20, '#required' => TRUE);
$form['body_filter']['format'] = filter_form($node->format, 1);
$form['body_filter']['format'] = filter_form($node->format);
$form['log'] = array(
'#type' => 'textarea', '#title' => t('Log message'), '#default_value' => $node->log, '#weight' => 5,
......
......@@ -87,7 +87,7 @@ function story_validate($node) {
function story_form(&$node) {
$form['title'] = array('#type' => 'textfield', '#title' => t('Title'), '#required' => TRUE, '#default_value' => $node->title, '#weight' => -5);
$form['body_filter']['body'] = array('#type' => 'textarea', '#title' => t('Body'), '#default_value' => $node->body, '#rows' => 20, '#required' => TRUE);
$form['body_filter']['format'] = filter_form($node->format, 1);
$form['body_filter']['format'] = filter_form($node->format);
return $form;
}
......@@ -87,7 +87,7 @@ function story_validate($node) {
function story_form(&$node) {
$form['title'] = array('#type' => 'textfield', '#title' => t('Title'), '#required' => TRUE, '#default_value' => $node->title, '#weight' => -5);
$form['body_filter']['body'] = array('#type' => 'textarea', '#title' => t('Body'), '#default_value' => $node->body, '#rows' => 20, '#required' => TRUE);
$form['body_filter']['format'] = filter_form($node->format, 1);
$form['body_filter']['format'] = filter_form($node->format);
return $form;
}
......@@ -1170,7 +1170,7 @@ function user_register_submit($form_id, $form_values) {
function user_edit_form($uid, $edit) {
// Account information:
$form['account'] = array('#type' => 'fieldset', '#title' => t('Account information'), '#weight' => 0);
$form['account'] = array('#type' => 'fieldset', '#title' => t('Account information'));
if (user_access('change own username') || user_access('administer users')) {
$form['account']['name'] = array('#type' => 'textfield', '#title' => t('Username'), '#default_value' => $edit['name'], '#maxlength' => 55, '#description' => t('Your full name or your preferred username: only letters, numbers and spaces are allowed.'), '#required' => TRUE);
}
......
......@@ -1170,7 +1170,7 @@ function user_register_submit($form_id, $form_values) {
function user_edit_form($uid, $edit) {
// Account information:
$form['account'] = array('#type' => 'fieldset', '#title' => t('Account information'), '#weight' => 0);
$form['account'] = array('#type' => 'fieldset', '#title' => t('Account information'));
if (user_access('change own username') || user_access('administer users')) {
$form['account']['name'] = array('#type' => 'textfield', '#title' => t('Username'), '#default_value' => $edit['name'], '#maxlength' => 55, '#description' => t('Your full name or your preferred username: only letters, numbers and spaces are allowed.'), '#required' => TRUE);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment