Drupal 7

I am trying to add a new button or link to my log in form. The button would simply link to another page outside of the drupal framework. I am looking at the form_alter hook but I am not sure if I completely understand how to implement my update.

do I have to update the template.php page?

The form is the login form on the front page. Any help would be great.

I am pretty sure I need something like the following :

$form['preview'] = array(
  '#type' => 'button',
  '#value' => t('Preview'),
  '#weight' => 19,

But I need to add the link. request.php to the button so when clicked it will go to request.php. Also I am not sure where to add this code and how to direct it to my log in form.

1 Answer 1


You cannot alter a form from a theme's template.php file. You need to create a custom module for that. In this module, you can alter the login form in an implementation of the hook_form_alter() or hook_form_FORM_ID_alter() hooks.

Without some JavaScript, you will not be able to submit the form to a separated URL. What you can do, in your module, is to provide an dedicated validation or submit handler for you button. In this handler, you can redirect to your external URL using drupal_goto().

Something like:

MODULE_form_user_login_alter(&$form, &$form_state) {
  $form['foo'] = array(
    '#type' => 'button',
    '#value' => t('Foo'),
    '#submit' => array('MODULE_user_login_foo_submit'),
    '#limit_validation_errors' => array(),

MODULE_user_login_foo_submit($form, &$form_state) {
  drupal_goto('/path/to/request.php', array('external' => TRUE));
  • I was afraid of that. Do you know of any clear resource on accomplishing this? There is a lot of great documentation on drupal.org but not much in the way of implementation. Thanks +1
    – Denoteone
    Commented Sep 23, 2011 at 6:46
  • Thats great but when I click the form tells me I need to enter something into the name and password fields (even though they are not getting used) I tried adding: $form['user_login_block']['pass'] = array( '#required' => FALSE ); $form['user_login_block']['name'] = array( '#required' => FALSE );
    – Denoteone
    Commented Sep 23, 2011 at 14:16
  • Add '#limit_validation_errors' => array() to the button. Commented Sep 23, 2011 at 14:33
  • Thanks for your help. This worked perfectly. I am going to have to spend some more time with hooks!
    – Denoteone
    Commented Sep 24, 2011 at 23:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.