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

krishnarp’s picture

Hi
You can use #weight to specify the position.
eg

 $form['parent'] = array(
  '#type' => 'select',
  '#title' => t('Parent'),
  '#default_value' => ($node->parent ? $node->parent : arg(4)),
  '#options' => book_toc($node->nid),
  '#weight' => -15,
  '#description' => t('The parent that this page belongs in. Note that pages whose parent is <top-level> are regarded as independent, top-level books.'),
);

Check this link for more information
http://api.drupal.org/?q=api/file/developer/topics/forms_api_reference.h...

Regards
Krishna

3cwebdev’s picture

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

3cwebdev’s picture

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

joachim’s picture

I think you've pasted the wrong link -- that's just here! ;)

+subscribing

inversed’s picture

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.

khaled_webdev’s picture

+1 for '#weight' => -15,

jng12’s picture

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

   // this is the new one
    $form['expire_date'] = array(
      '#type' => 'date',
      '#title' => t('Set Expiration Date for this Group'),
      '#description' => t('Change this date to set Expiration of Role.'),
	  '#default_value' => $date_default_value,
	  '#weight' => 1,
    );

  // add weights to submit and delete
    $form['submit']['#weight'] = 20;
    $form['delete']['#weight'] = 20;

hope this helps.

Dewi Morgan’s picture

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:

    $form[new_group]
    $form[new_group][element_to_append_to]
    $form[new_group][appended_element]

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:

    // +1 to append, otherwise it prepends.
    $pos = array_search('element_to_append_to', array_keys($form_values))+1; 
    $form_values = array_merge(array_slice($form_values, 0, $pos), $newform, array_slice($form_values, $pos));

Personally I'd use array_splice instead, since it's most likely a bit faster:

    // +1 to append, otherwise it prepends.
    $pos = array_search('element_to_append_to', array_keys($form_values))+1; 
    array_splice($form_values, $pos, 0, $newform);

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.

dks786’s picture

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

nitin.k’s picture

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.'),
    );
  }

}

form alter parameters placement should be correct..

function mymodule_form_alter($form, &$form_state, $form_id) {
  if ($form_id == 'form_id') {
	
    $form['mymodule'] = array(
     '#type' => 'checkbox',
	 '#options' => t('Enable'), // This must be an array.. !! Added below
       
    // '#options' => drupal_map_assoc(array(t('Enable'))),

     '#title' => t('title),
     '#description' => t('Description.'),
    );
  }
}