Why does the '#weight' property sometimes not have any effect in Drupal forms?

Posted by Adrian on Stack Overflow See other posts from Stack Overflow or by Adrian
Published on 2009-08-18T02:47:43Z Indexed on 2010/03/16 10:36 UTC
Read the original article Hit count: 249

Filed under:

Hello, I'm trying to create a node form for a custom type. I have organic groups and taxonomy both enabled, but want their elements to come out in a non-standard order. So I've implemented hook_form_alter and set the #weight property of the og_nodeapi subarray to -1000, but it still goes after taxonomy and menu. I even tried changing the subarray to a fieldset (to force it to actually be rendered), but no dice. I also tried setting

$form['taxonomy']['#weight'] = 1000

(I have two vocabs so it's already being rendered as a fieldset) but that didn't work either.

I set the weight of my module very high and confirmed in the system table that it is indeed the highest module on the site - so I'm all out of ideas. Any suggestions?

Update:

While I'm not exactly sure how, I did manage to get the taxonomy fieldset to sink below everything else, but now I have a related problem that's hopefully more manageable to understand. Within the taxonomy fieldset, I have two items (a tags and a multi-select), and I wanted to add some instructions in hook_form_alter as follows:

$form['taxonomy']['instructions'] = array(
  '#value' => "These are the instructions",
  '#weight' => -1,
);

You guessed it, this appears after the terms inserted by the taxonomy module. However, if I change this to a fieldset:

$form['taxonomy']['instructions'] = array(
  '#type' => 'fieldset', // <-- here
  '#title' => 'Instructions',  // <-- and here for good measure
  '#value' => "These are the instructions",
  '#weight' => -1,
);

then it magically floats to the top as I'd intended. I also tried textarea (this also worked) and explicitly saying markup (this did not).

So basically, changing the type from "markup" (the default IIRC) to "fieldset" has the effect of no longer ignoring its weight.

© Stack Overflow or respective owner

Related posts about drupal