tags:

views:

644

answers:

6

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.

A: 

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'].

Heinrich Filter
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
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
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
+4  A: 

This sounds pretty strange, as 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 weigth on my machine:

$form['taxonomy']['instructions'] = array(
  '#value' => "<p>These are the instructions</p>",
  '#weight' => -1,
);
Henrik Opel
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
Updated answer to address updated question
Henrik Opel
Thanks, that's perfect!
Adrian
A: 

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.

robotoverlord
A: 

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
Thanks - I had disabled caching. Heinrich seems to have nailed it though.
Adrian
A: 

Sometimes, the line that works in your hook_form_alter is this one:

$form['#content_extra_fields']['taxonomy']['weight'] = 5;
Tomás Pica
A: 

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.

Kieran Mathieson

related questions