Hi
I am not sure if this the correct location to log issues with hook_form().
I am finding that the placement of a node's title within a form is fickle and I want to understand why.
I want to place another form item before a node's title form item, and this is the code.
If $weight is set to 0, then even though the $form['project_customer_id']
is of lower #weight (0)
and the $form['title']
's #weight (1)
, the $form['title']
appears before the $form['project_customer_id']
.
However, if $weight is lowered to -5, the position of these form items switches.
And if $weight is lowered to -6, then the position of $form['title']
sinks to the bottom (if there are multiple more form items) and placed just above $form['body']
.
Why would this be so?
$weight = 0;
$form['project_customer_id'] = array(
'#type' => 'select',
'#title' => t('Customer :: Location :: Region'),
'#default_value' => $node->project_customer_id,
'#options' => $select_customers,
'#required' => TRUE,
'#weight' => $weight++, // Used to sort the list of form elements before being output;
);
if ($type->has_title) {
$form['title'] = array(
'#type' => 'textfield',
'#title' => check_plain($type->title_label),
'#required' => TRUE,
'#default_value' => $node->title,
'#weight' => $weight++,
);
}
This is the var dump of the above generated code and notice the $weight values:
[project_customer_id] => Array
(
[#type] => select
[#title] => Customer :: Location :: Region
[#default_value] => 0
[#options] => Array
(
[0] => Choose...
[7] => Care :: Seattle :: West
[6] => EIT :: Seattle :: West
[4] => Engineering :: Seattle :: West
[5] => Internal :: Seattle :: West
)
[#required] => 1
[#weight] => 0
)
[title] => Array
(
[#type] => textfield
[#title] => Project Name
[#description] => Project Name must be unique.
[#required] => 1
[#default_value] => PTP for Alarm Systems (NRC & DDC)
[#weight] => 1
)
Thanks
Jeff in Seattle
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | drupal.node-title-weight.4.patch | 904 bytes | sun |
| #3 | drupal.node-title-weight.3.patch | 804 bytes | sun |
Comments
Comment #1
litwol commentedComment #2
jeff00seattle commentedThis issue is experience in Drupal 6.10.
Comment #3
sunBecause http://api.drupal.org/api/function/node_form/6 explicitly defines this behavior:
Sorry, this is the intended - and undocumented - behavior for Drupal 6. We can only try to fix this for D7.
Let's see what breaks.
Comment #4
sunChanged my mind. Actually, I think this makes sense. Just lacks documentation, and an extra isset().
Comment #5
andypostJust get this bug with titleless node form, thanx
Comment #6
sun#4: drupal.node-title-weight.4.patch queued for re-testing.
Comment #7
sunAlthough badly needed, this is D8 material according to the rules (I had to learn today). It may be backported at a later point in time (though that's unlikely).
Comment #8
andypost@sun this is a bug and should be fixed in D7!
node_form() should not set #weight for none-existent title
Comment #9
webchickCommitted to HEAD. Thanks!