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

Comments

litwol’s picture

Project: » Drupal core
Version: » 7.x-dev
Component: Code » forms system
jeff00seattle’s picture

Version: 7.x-dev » 6.10

This issue is experience in Drupal 6.10.

sun’s picture

Title: hook_form() and #weight: Fickle node title placement » Node title form element uses weird #weight
Version: 6.10 » 7.x-dev
Component: forms system » node system
Status: Active » Needs review
Issue tags: -weight, -hook_form
StatusFileSize
new804 bytes

Because http://api.drupal.org/api/function/node_form/6 explicitly defines this behavior:

  if (!isset($form['title']['#weight'])) {
    $form['title']['#weight'] = -5;
  }

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.

sun’s picture

Priority: Normal » Minor
StatusFileSize
new904 bytes

Changed my mind. Actually, I think this makes sense. Just lacks documentation, and an extra isset().

andypost’s picture

Status: Needs review » Reviewed & tested by the community

Just get this bug with titleless node form, thanx

sun’s picture

#4: drupal.node-title-weight.4.patch queued for re-testing.

sun’s picture

Version: 7.x-dev » 8.x-dev

Although 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).

andypost’s picture

Version: 8.x-dev » 7.x-dev
Priority: Minor » Normal

@sun this is a bug and should be fixed in D7!
node_form() should not set #weight for none-existent title

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Committed to HEAD. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.