3

I'm attempting to use the Constant Contact module.
One of the features of this module is that it inserts a check-box into the user registration form. The problem is that it seems to set weights in a way that it interferers with my own implementation of hook_form_alter(), on the registration form. The field order is a mess now: the field for the confirmation of the email address is now the first one.

The Constant Contact module is specifying a weight for the checkbox:

'#weight' => 11,

My implementation of hook_form_alter(), just left my new form element weights blank. I could just massage my functions form weights such that it didn't have the fields out of order, but then I risk to do it again the next time I need to add something to the form or use another module that does likewise.

Is there a 'best practice' way to handle form weights assuming multiple override sources?

1

You can adjust your module weight so that it runs after the offending module. This will allow you to control the weight of all elements in the form, even if another module adds and sets them. But this can get very tricky in the longer term if you rely on weights of modules too much.

you could also use an #after_build function to alter the weights if you have an exact order in mind.

  • The OP is asking about the form field weight, not the module's one. – kiamlaluno Jun 8 '11 at 10:43
  • Have added explanation as to why altering modules weights can help with form item weights. – Jeremy French Jun 8 '11 at 10:57
5

The only thing I usually do and can recommend is to leave "gaps" between your weigts. So instead of 1, 2, 3, 4, 5 or so, I use for example 5, 10, 15, 20, 25.

The idea is that it makes it easier for other modules to move a field to a different place, or move new fields in-between.

3

If multiple override sources are adjusting the weight of your field then definitely there is an order in which they are performing. Your hook_form_alter might be performing before the other. Adjust your module's weight to perform in the last. Util module can help in this regard to adjust module weights.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.