Inserting fields into a form
I'm hooking hook_form_MY_FORM_ID_alter in order to add additional fields to the form. The current fields have associative keys ('name', 'mail'). How do I add fields between them? There isn't a PHP call to do an insert into the middle of an array, and while using array_splice works, it won't use associative keys. The numeric keys result in the form looking ok, but when I ask the CAPTCHA module to give me a link on the form, it puts it right between the first field (associative key) and the first that I inserted (numeric key), which I think has something to do with the key problem. Is there an api call I've missed to let me specify where to add fields, or do I have to rebuild the form array in order to do it?
Look at '#weight' On Sun, Sep 6, 2009 at 6:54 PM, Jeff Greenberg <jeff@ayendesigns.com> wrote:
I'm hooking hook_form_MY_FORM_ID_alter in order to add additional fields to the form. The current fields have associative keys ('name', 'mail'). How do I add fields between them? There isn't a PHP call to do an insert into the middle of an array, and while using array_splice works, it won't use associative keys. The numeric keys result in the form looking ok, but when I ask the CAPTCHA module to give me a link on the form, it puts it right between the first field (associative key) and the first that I inserted (numeric key), which I think has something to do with the key problem. Is there an api call I've missed to let me specify where to add fields, or do I have to rebuild the form array in order to do it?
Jerad Bitner wrote:
Look at '#weight'
On Sun, Sep 6, 2009 at 6:54 PM, Jeff Greenberg <jeff@ayendesigns.com <mailto:jeff@ayendesigns.com>> wrote:
I'm hooking hook_form_MY_FORM_ID_alter in order to add additional fields to the form. The current fields have associative keys ('name', 'mail'). How do I add fields between them? There isn't a PHP call to do an insert into the middle of an array, and while using array_splice works, it won't use associative keys. The numeric keys result in the form looking ok, but when I ask the CAPTCHA module to give me a link on the form, it puts it right between the first field (associative key) and the first that I inserted (numeric key), which I think has something to do with the key problem. Is there an api call I've missed to let me specify where to add fields, or do I have to rebuild the form array in order to do it?
Ah, a Blinding Flash of the Obvious! Thanks so much :-)
Jerad Bitner wrote:
Look at '#weight'
Ok, I've changed my hook to include weights for all the fields I'm adding, as well as adding weights to the existing fields. The problem I'm having is that when I dump the form at the time of my hook, the captcha field doesn't exist yet as far as $form is concerned, but somewhere between my hooking it and the form appearing, the captcha field is present (the captcha module uses hook_form_alter). The problem is that the captcha module is putting its field after the name field, which is right at the top of the form. If I assign a weight to it in my hook (keeping in mind the ['captcha'] field doesn't exist yet when I do that) the captcha doesn't show up at all. Is there a way I can be the last one to modify the form, so that the captcha field already exists and I can change its weight?
Change the weight for your module in the system table so it has a higher number than the Captcha module. That way, yours will be the last one to run. Steve Jeff Greenberg wrote:
Jerad Bitner wrote:
Look at '#weight'
Ok, I've changed my hook to include weights for all the fields I'm adding, as well as adding weights to the existing fields.
The problem I'm having is that when I dump the form at the time of my hook, the captcha field doesn't exist yet as far as $form is concerned, but somewhere between my hooking it and the form appearing, the captcha field is present (the captcha module uses hook_form_alter). The problem is that the captcha module is putting its field after the name field, which is right at the top of the form. If I assign a weight to it in my hook (keeping in mind the ['captcha'] field doesn't exist yet when I do that) the captcha doesn't show up at all.
Is there a way I can be the last one to modify the form, so that the captcha field already exists and I can change its weight?
Yes, modules also have weight in the system table which effects their run time order. On Sun, Sep 6, 2009 at 8:03 PM, Jeff Greenberg <jeff@ayendesigns.com> wrote:
Jerad Bitner wrote:
Look at '#weight'
Ok, I've changed my hook to include weights for all the fields I'm
adding, as well as adding weights to the existing fields.
The problem I'm having is that when I dump the form at the time of my hook, the captcha field doesn't exist yet as far as $form is concerned, but somewhere between my hooking it and the form appearing, the captcha field is present (the captcha module uses hook_form_alter). The problem is that the captcha module is putting its field after the name field, which is right at the top of the form. If I assign a weight to it in my hook (keeping in mind the ['captcha'] field doesn't exist yet when I do that) the captcha doesn't show up at all.
Is there a way I can be the last one to modify the form, so that the captcha field already exists and I can change its weight?
By changing weight you mean directly in the table or there is a way to inform drupal that my custom module should be loaded at last. The module listing page at admin/build/modules follow the same weight based order (?) in that case would it mean that it can be controlled by package name? On Mon, Sep 7, 2009 at 8:37 AM, Jerad Bitner <sirkitree@gmail.com> wrote:
Yes, modules also have weight in the system table which effects their run time order.
On Sun, Sep 6, 2009 at 8:03 PM, Jeff Greenberg <jeff@ayendesigns.com>wrote:
Jerad Bitner wrote:
Look at '#weight'
Ok, I've changed my hook to include weights for all the fields I'm
adding, as well as adding weights to the existing fields.
The problem I'm having is that when I dump the form at the time of my hook, the captcha field doesn't exist yet as far as $form is concerned, but somewhere between my hooking it and the form appearing, the captcha field is present (the captcha module uses hook_form_alter). The problem is that the captcha module is putting its field after the name field, which is right at the top of the form. If I assign a weight to it in my hook (keeping in mind the ['captcha'] field doesn't exist yet when I do that) the captcha doesn't show up at all.
Is there a way I can be the last one to modify the form, so that the captcha field already exists and I can change its weight?
Dipen wrote:
By changing weight you mean directly in the table or there is a way to inform drupal that my custom module should be loaded at last. The module listing page at admin/build/modules follow the same weight based order (?) in that case would it mean that it can be controlled by package name? I'm looking through the api without much luck. From what I notice, the packages are alphabetical.
You can change module weights directly via SQL. Basically you set the weight column in the system table. Or you can use this module http://drupal.org/project/moduleweight Although it is for D5, it is a small module and can easily be adapted to D6. On Sun, Sep 6, 2009 at 11:23 PM, Jeff Greenberg <jeff@ayendesigns.com>wrote:
Dipen wrote:
By changing weight you mean directly in the table or there is a way to
inform drupal that my custom module should be loaded at last. The module listing page at admin/build/modules follow the same weight based order (?) in that case would it mean that it can be controlled by package name?
I'm looking through the api without much luck. From what I notice, the packages are alphabetical.
-- Khalid M. Baheyeldin 2bits.com, Inc. http://2bits.com Drupal optimization, development, customization and consulting. Simplicity is prerequisite for reliability. -- Edsger W.Dijkstra Simplicity is the ultimate sophistication. -- Leonardo da Vinci
As mentioned on the Module Weight project page, this functionality is now included in the Utility module (http://drupal.org/project/util) in D6. Steve Khalid Baheyeldin wrote:
You can change module weights directly via SQL. Basically you set the weight column in the system table.
Or you can use this module http://drupal.org/project/moduleweight
Although it is for D5, it is a small module and can easily be adapted to D6.
On Sun, Sep 6, 2009 at 11:23 PM, Jeff Greenberg <jeff@ayendesigns.com <mailto:jeff@ayendesigns.com>> wrote:
Dipen wrote:
By changing weight you mean directly in the table or there is a way to inform drupal that my custom module should be loaded at last. The module listing page at admin/build/modules follow the same weight based order (?) in that case would it mean that it can be controlled by package name?
I'm looking through the api without much luck. From what I notice, the packages are alphabetical.
-- Khalid M. Baheyeldin 2bits.com <http://2bits.com>, Inc. http://2bits.com Drupal optimization, development, customization and consulting. Simplicity is prerequisite for reliability. -- Edsger W.Dijkstra Simplicity is the ultimate sophistication. -- Leonardo da Vinci
Steve Edwards wrote:
As mentioned on the Module Weight project page, this functionality is now included in the Utility module (http://drupal.org/project/util) in D6.
That did it for me :-) Thank you all. I didn't even need to change the captcha field weight after changing my module weight using the Utility module!
participants (5)
-
Dipen -
Jeff Greenberg -
Jerad Bitner -
Khalid Baheyeldin -
Steve Edwards