Using Drupal 7, I'm trying to utilize Drupal Form API in combination with hook_form_alter to place my added inputs at the beginning of the form.

I thought the best way to do this was to create a new form element like so:

$form['program_list'] = array(
    '#type' => 'select', 
    '#title' => t('Select a preferred Program'), 
    '#options' => $program_list,
    '#weight' => -1,

I read the documentation, and it looks like there is a #weight attribute. I tried setting the weight to either 0 and negative integers, but my added inputs are still at the bottom.

I then used var_dump() to check the weights of my Webform-generated inputs. They were as I expected: a 0-based, incremental weight index.

I also tried placing the elements in $newform then using $form = $newform + $form, but that as well placed my new inputs at the bottom of the form.

What am I doing wrong?


I was facing the same problem but found the answer thanks to this post: http://drupal.org/node/235354

Problem was that the field I was adding to my web form was not in a group when all the already existing fields were part of the group 'submitted' maybe because this is a webform. So setting the weight to -1000 didn't change anything.

function webform_boncommande_form_webform_client_form_6721_alter(&$form, $form_state, $form_id)  {

    $form['my_new_field_name'] = array (
      '#type' => 'checkbox',
      '#title' => t ('My new field is after the others'),
      '#weight' => -1000,


To correctly make it work I just needed to create my new field inside the group 'submitted' like that :

$form['submitted']['my_new_field_name'] = array (...
| improve this answer | |

The #weight property is indeed the right way to go, it might be that another module that's processed after yours is moving some of the other elements back above yours.

If this is the case you could either try changing the #weight to a very low number (I normally go for -1000 when what I'm doing doesn't seem to be having the desired effect), or ensuring that your module's hooks are processed after any others.

The second option basically involves setting the weight of your module to a higher number than the others in the system table. See How to update a module's weight for more information.

| improve this answer | |
  • Thanks for the response Clive. I have tried setting the new input, $form['program_list'], to weight -1000. I've also set my module's row in the system table to weight 2999. I've confirmed that there are no higher weights in that table. I also flushed all caches, but the element remains at the bottom! I'm going out on a limb; here's a pastebin, in case there is anything that pops out about the $form. pastebin.com/Ygkh8Pij – Morgan Delaney Feb 15 '12 at 23:45
  • Weird! What form are you trying to alter out of interest? Maybe there's a clue in that – Clive Feb 15 '12 at 23:50
  • I've created a Webform node. The ID is webform_client_form_[nid]. Webform module via drupal.org/project/webform. – Morgan Delaney Feb 16 '12 at 0:30

I've done a terrible hack JS job. Before reading this answer, please see if Clive's answer will work.

My JavaScript for getting my uncooperative selects to move to the top is:

jQuery(document).ready(function() {
  var inputs = jQuery('select[name=CampusID], select[name=ProgramID]').parents('.form-item');
| improve this answer | |

For the next person who comes along with this problem (specific to altering Webform nodes):

The behaviour is caused by the webform-form.tpl.php template. Since I wanted something to display before the webform components, I just overrode the template and drupal_render()'d my part of the form first.

(You can just stick your modifications into the $form['submitted'] array, but then they start getting processed/stored along with the submission, which was undesirable in my case.)

| improve this answer | |

For adding markup to the beginning of the form, this works (but was not easy to find):

 $form['#prefix'] = '<h2>A title</h2>';
| improve this answer | |

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.