1

I am going to build a site using Drupal 7.8. I have some knowledge in PHP, but it is my first time I use Drupal, so I would like to ask your help regarding some basic functional questions I have

  1. I want to have 2 tabbed blocks in my first page. One for "log in" and another for "sign up". To create this blocks: For "log in" block it s easy: I will use the out of the box functionality as is (removing create new and request password). For "sign up" block I would like to remove and add some fields. Is there any way to find inside user.module the fields used for "username","password","email" etc. and try to use them in my own module? What are the minimum requirements to use these fields? In other words, if I find the name of these fields, can I use only the names on my module, or I have to override a form/function that maybe contains these fields?

  2. I want to use these fields, but finally for the submission in the database I 'd like to use the default submit functionality. How that could be done? 3.And one last and very newbie question! Forms are implemented using functions or can be stand alone? So to use an existing form, I have to find in which function it s declared or I have to override only that form? I know that these are basic questions I should already know but after searching in google, I didn't manage to find a clear solution that works. Could you please help?

2 Answers 2

1

You can also use fields to add additional fields to a user or user the profile2 module. There's a good chance, that you don't even have to code a line ;-).

If you need: You can change any form in Drupal with hook_form_alter. This way you can add more functionality to the sign up form.

0

The builder function for the user registration form is user_register_form(), whose essential code is the following one:

  $form['#user'] = drupal_anonymous_user();
  $form['#user_category'] = 'register';

  $form['#attached']['library'][] = array('system', 'jquery.cookie');
  $form['#attributes']['class'][] = 'user-info-from-cookie';

  // Start with the default user account fields.
  user_account_form($form, $form_state);

  // Attach field widgets, and hide the ones where the 'user_register_form'
  // setting is not on.
  field_attach_form('user', $form['#user'], $form, $form_state);
  foreach (field_info_instances('user', 'user') as $field_name => $instance) {
    if (empty($instance['settings']['user_register_form'])) {
      $form[$field_name]['#access'] = FALSE;
    }
  }

The most important part of user_account_form() is the following one:

  // Only show name field on registration form or user can change own username.
  $form['account']['name'] = array(
    '#type' => 'textfield', 
    '#title' => t('Username'), 
    '#maxlength' => USERNAME_MAX_LENGTH, 
    '#description' => t('Spaces are allowed; punctuation is not allowed except for periods, hyphens, apostrophes, and underscores.'), 
    '#required' => TRUE, 
    '#attributes' => array('class' => array('username')), 
    '#default_value' => (!$register ? $account->name : ''), 
    '#access' => ($register || ($user->uid == $account->uid && user_access('change own username')) || $admin), 
    '#weight' => -10,
  );

  $form['account']['mail'] = array(
    '#type' => 'textfield', 
    '#title' => t('E-mail address'), 
    '#maxlength' => EMAIL_MAX_LENGTH, 
    '#description' => t('A valid e-mail address. All e-mails from the system will be sent to this address. The e-mail address is not made public and will only be used if you wish to receive a new password or wish to receive certain news or notifications by e-mail.'), 
    '#required' => TRUE, 
    '#default_value' => (!$register ? $account->mail : ''),
  );

  // Display password field only for existing users or when user is allowed to
  // assign a password during registration.
  if (!$register) {
    $form['account']['pass'] = array(
      '#type' => 'password_confirm', 
      '#size' => 25, 
      '#description' => t('To change the current user password, enter the new password in both fields.'),
    );

$register is set by user_account_form() using the following code:

$account = $form['#user'];
$register = ($form['#user']->uid > 0 ? FALSE : TRUE);

If you just need to show those three fields, then it is sufficient that you call user_account_form(), setting $form with the first two lines I reported for user_register_form().

  $form['#user'] = drupal_anonymous_user();
  $form['#user_category'] = 'register';

user_account_form() has its own validation handler (user_account_form_validate()); you don't need to write code for validating the input given from the user.

To re-use a field created from a function, you need to call that function. In the case of $form['account']['name'], if I use the following code in my form build, I would not obtain anything: the field will not be created, nor would I get the field defined by that form builder simply for the fact I am using the same string that function uses.

$form['account']['name'] = array();
0

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.