By 3cwebdev on
I am using form_alter to add a new field into an existing module's form. However, it lists the field at the bottom of the form, after the submit button.
How do I insert the field into the form array between existing fields?
Here is the function;
function mymodule_form_alter($form_id, &$form) {
if ($form_id == 'form_id') {
$form['mymodule'] = array(
'#type' => 'checkbox',
'#options' => t('Enable'),
'#title' => t('title),
'#description' => t('Description.'),
);
}
}
Thanks for any help!
Comments
Hi You can use #weight to
Hi
You can use #weight to specify the position.
eg
Check this link for more information
http://api.drupal.org/?q=api/file/developer/topics/forms_api_reference.h...
Regards
Krishna
Thanks, but doesn't work
Thanks for your response. I have tried using weight and it will only move it above all the other fields. So using weight options only allow me to put the new field before the form or after the form, but not between existing fields.
I believe that I have to insert the form_alter values into the existing array. I have no idea how to do this though...
The Cosmic Gift | Complete Computer Care | Team Hope
Figured it out...
I used the information on this thread http://drupal.org/node/235354#comment-772772 to make a function to insert the new values into the existing array.
The Cosmic Gift | Complete Computer
Care | Team Hope
I think you've pasted the
I think you've pasted the wrong link -- that's just here! ;)
+subscribing
Add weights to existing fields
If this is happening, then the other fields do not have a weight associated with them. I added weight info for all the existing fields in my form_alter and then the order was controllable. Also, I think the submit buttons have a weight of 0 so make sure to use negatives.
thx
+1 for '#weight' => -15,
I got it working
I got it working by adding weights to existing fields that I want lower than my new ones.
for example.
by adding the following code to my form alter, I got it below my new one which had a weight of 1
hope this helps.
Three ways to do it...
The "Completely Correct" way to do it is to group the two elements into a form group, so they don't get inadvertently separated by other scripts, so:
$form[element_to_append_to]becomes:
But you don't always want to create a group. The next possibility is, as already mentioned, to set the weights of all other items at the same level in the form tree, in order to get your item into the right position.
The last possibility is to just insert your item into the array directly after the item you are searching for.
Explained at CivicActions, you can use array_slice and array_merge to do this:
Personally I'd use array_splice instead, since it's most likely a bit faster:
If your element_to_append_to doesn't have the default weight (0), then you should apply its weight to your new form items, $newform, so they stay together.
I should note that with this last option, I get the item inserted twice, even if I check for its presence. I suspect this is an issue with CCK.
what to do for submit field value in database
Hi,
I added a field. But how can I add that value in database.
Do I need to create field manually?
Do I need to use hook_form_validate and hook_form_alter.
Please help
Is it the right way ??
}
form alter parameters placement should be correct..