1

The following code is adding <div>'s and link to the form action. How can I add them before the form?

function ajax_register_form_user_login_alter(&$form, &$form_state, $form_id) {
  $form['bigletters'] = array(
    '#type' => 'item',
    '#markup' => 'Big letters',
  );

  $form['smallletters'] = array(
    '#type' => 'item',
    '#markup' => 'Small letters',
  );

  $form['links'] = array(
    '#type' => 'item',
    '#markup' => '<a href="http://example.com"></a>',      
  );
}
2

If you need the form elements you are adding to appear before the other ones, you should use the #weight property, and give them a negative value, since the default value is zero. Differently, the form elements you add to a form via hook_form_alter() are normally appended to the form, with the exception of when there is a form element with a positive #weight value higher than the #weight value you assigned to your form element.

user_login() doesn't set the weight of its form elements. The following code should work.

function ajax_register_form_user_login_alter(&$form, &$form_state, $form_id) {
  $form['bigletters'] = array(
    '#type' => 'item',
    '#markup' => 'Big letters',
    '#weight' => -10,
  );

  $form['smallletters'] = array(
    '#type' => 'item',
    '#markup' => 'Small letters',
    '#weight' => -8,
  );

  $form['links'] = array(
    '#type' => 'item',
    '#markup' => '<a href="http://example.com"></a>',      
    '#weight' => -6,
  );
}

If the code doesn't work, then there is probably another module that alters the weight of the other form elements.

| improve this answer | |
  • this solution worked, but can i somehow post it out of form? – AlexEfremo Jul 7 '13 at 9:07
  • You cannot, since with hook_form_alter() you are altering a form. To add something outside a form, you should alter the page output, for example with hook_page_alter(), but I don't see any reason to write more complicate code. If the problem is targeting some HTML elements, you can use a different CSS selector. – kiamlaluno Jul 7 '13 at 9:27
  • you right. i made it with css. thx again! – AlexEfremo Jul 7 '13 at 9:30
  • Sorry for bothering again, but can you give me an example how to make it with hook_page_alter()? – AlexEfremo Jul 7 '13 at 10:03

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.