3

I am trying to add a custom field to the user login block in drupa 7 for the zen theme. That is, in addition to the username and password fields, I need another field called code. Most of the available documentation on the net is targeted at drupal 6. From what I've read, the template.php file and the block--user--login.tpl.php files need to be modified for the particular theme. Is this right? Also, what functions need to be modified/added?

Any help appreciated.

1 Answer 1

3

Drupal forms don't reside in any particular template. They are defined in the code. When a form is displayed, the code is read, it is then made available for modification by other modules before being rendered by a function called drupal_render() this will then use various template files to build the form which its displayed to the user.

It is fairly simple to add a field to the login form using hook_form_alter().

function hook_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'user_register') {
    $form['myfield'] = array(
      '#type' => 'text', 
      '#title' => t('code'), 
    );
  }
}

What is perhaps more tricky is deciding what to do with this, are you using it for a second form of authentication?

A final note is on where to put the code. Drupal is fairly flexible, in that you can put theme files in modules and functionality in themes. However best practice is to keep features in modules, it leeds to a more manageable system in the long run.

1
  • Thanks for the code. Yup, I need the other field to authenticate against an external database. Any ideas on which should I plug that code into?
    – Checksum
    Jul 7, 2011 at 11:11

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.