13

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.

Luuklag
  • 3,897
  • 11
  • 38
  • 57
Adrian Ludwin
  • 214
  • 1
  • 2
  • 12

7 Answers7

14

This sounds pretty strange because the manipulation of the form elements' #weight parameter always works reliably for me as advertised. One thing to note, though, is that the weights only affect the order relative to the elements siblings, so if the element you want to move around is on a level below that of the other elements, you'd have to change the weight of the parent element that is on the same level as the ones you want to move against.

To clarify, if you have a hierarchy like so,

$element['foo'];
$element['bar'];
$element['bar']['baz']

you can not move 'baz' relative to 'foo' by setting the weight of 'baz'. You'd have to either set the weight on 'bar' (moving it also), or pull out 'baz' and bring it to the same level as 'foo'.

Another possible reason could be CCK: If you have CCK installed, it allows you to set the order of its own as well as other fields under admin/content/node-type/<yourNodeType>/fields. It changes the order by registering the pre-render callback content_alter_extra_weights(), so this would run after your changes in hook_form_alter.


Edit: Update to answer the question update

The markup field type has a special behavior when used inside fieldsets, which is hinted on in the forms api documentation:

Note: if you use markup, if your content is not wrapped in tags (generally <p> or <div>), your content will fall outside of collapsed fieldsets.

It looks like if it does not only fall outside of collapsed fieldsets, but also refuses to respect the weight in those cases. Wrapping the content of the markup field in <p> tags makes it respect the weight on my machine:

$form['taxonomy']['instructions'] = array(
  '#value' => "<p>These are the instructions</p>",
  '#weight' => -1,
);
htoip
  • 437
  • 5
  • 19
Henrik Opel
  • 19,341
  • 1
  • 48
  • 64
  • Hmmm... I did have CCK enabled at one time but now I have it disabled. Perhaps that's why my original usecase is working now? But as I've said in the updated description, I now have another similar but easier-to-reproduce problem. – Adrian Ludwin Sep 29 '09 at 18:04
  • That business with #markup inside fieldsets is a real WTF... thanks for mentioning it here; it's saved me a fair bit of headscratching! – joachim Jun 22 '11 at 13:50
  • i had also this problem due to order of elements siblings. Got idea from your answer which saved my time. Thanks – Mujtaba Haider Aug 09 '11 at 03:35
  • When debugging code in i.e. debugger elements are shown as they are in same level of hierarchy. But if at form display settings (i.e. for content type) some grouping exists this will not be true and values from $form variable will not reflect real hierarchy. – MilanG Sep 06 '22 at 13:38
3

Sometimes (or always, when weighting CCK elements) the line that works in your hook_form_alter or $form['#after_build'] callback is this one:

$form['#content_extra_fields']['taxonomy']['weight'] = 5;
Madbreaks
  • 19,094
  • 7
  • 58
  • 72
2

Wanted to insert a <div> I could use in JS. This didn't work for me:

  $form['you_as_reviewer']['ui_container'] = array(
    '#type' => 'markup',
    '#value' => '<div id="le_reviewer_tags_ui"/>',
    '#weight' => 5,
  );

Weight was ignored.

This worked:

  $form['you_as_reviewer']['ui_container'] = array(
    '#type' => 'markup',
    '#prefix' => '<div>',
    '#value' => '<div id="le_reviewer_tags_ui"/>',
    '#suffix' => '</div>',
    '#weight' => 5,
  );

Added prefix and suffix.

1

I do not quite understand what it is you want to achieve. Could you maybe clarify? Do you want to change the position of the taxonomy's drop-down on the page?

In the mean time you could install the Drupal Devel module (if you haven't done so yet). Then enable "Display form element keys and weights" from Admin > Devel Settings.

This should help you to debug your problem.

Edit after feedback:

I looked into it some more. The taxonomy weight is set in taxonomy.module on line 556 (Drupal 6.12):

$form['taxonomy']['#weight'] = -3;

To test I also implemented hook_form_alter for my module like this:

function mymodule_form_alter(&$form, $form_state, $form_id) {
      ...
      $form['taxonomy']['#weight'] = -9;
      ...
} 

This works for me i.e. it moves the taxonomy drop-down to the top of the page. I can change the weight and it moves accordingly on the rendered page.

Since you said you tried setting $form['taxonomy']['#weight'] in your original post I can currently think of only two possible checks:

  1. Make sure the cache is cleared before testing. (you can use the Devel module for this)
  2. Check to see if your hook_form_alter is called after taxonomy_form_alter

I you post the code you currently have we could look at it in more detail.


Please note: the weights displayed by the Devel module are not very useful for this situation. The weights of the elements on the "sub-forms" are displayed and not the weight of the "sub-form" itself. E.g. when I set $form['taxonomy']['#weight'] = -9; the -9 is not displayed by the Devel module but rather the weights of the elements inside $form['taxonomy'].

Community
  • 1
  • 1
Heinrich Filter
  • 5,760
  • 1
  • 33
  • 34
  • Hi Heinrich, yes, I'm trying to change the position of the taxonomy drop-down. I don't have devel but I'll install it tonight and report back. Thanks! – Adrian Ludwin Aug 18 '09 at 23:21
  • Hi Heinrich, I installed devel and checked out the weights. Everything seems to be as I intended, but the fieldsets are not arranged in order of ascending weight at all. The first fieldset is taxonomy, which has a weight of 1000. The second is menu (-2), then book outline (10). There are a couple more, then File Attachments (30), Authoring information (20) and publishing information (25). So it's not only the fieldsets I added that are out of order, but the core ones too. Is hook_form_alter too late to change the order of existing fieldsets? – Adrian Ludwin Aug 19 '09 at 12:30
  • Thanks for the clarification RE devel. I am quite sure hook_form_alter's being called after taxonomy_form_alter since I can see all the taxonomy-added elements from within my hook. – Adrian Ludwin Sep 29 '09 at 18:06
0

Have you specified the weight of another field and now your node form is not organized properly? The form api is kinda touchy and altering the form can result in things getting mixed up. I sometimes have to reassign a weight to my submit/preview buttons to get them back at the bottom of the form where they belong.

d3l3t3m3
  • 211
  • 1
  • 10
0

Just to cover all bases, make sure that you are clearing your cache as necessary. Drupal stores forms in it's cache by default, if you have the caching module enabled.

Andrew Sledge
  • 10,163
  • 2
  • 29
  • 30
0

This is working for me

$form['forgot_password']['#markup'] = '<div class="text-align-right"><a class="text-primary" href="/user/password" target="_blank" title="Forgot Password?">Forgot Password?</a></div>';

$form['forgot_password']['#weight'] = 3;

enter image description here

Anoop Singh
  • 121
  • 1
  • 6