drupal-7user-registrationdrupal-hooks

Drupal 7 - how do I create a custom registration form


I am new to Drupal development. I want to create a registration form for visitors of my site. Default registration page only has two fields: Username and Email address.

  1. How can I add more fields to it, e.g password, picture and timezone. I also want to capture tow other information - Date of Birth and Gender. These two fields are not available in default users table. How can I tie these information with a user account? Do I have to create a new table and put these information there referencing the uid of user table? If it is possible how can I pull the joined record?

  2. Is there any possibility that I create a new content type for this purpose but records still go to default users table and can be used for login?

  3. If 2 above is not possible I probably have to use hook_form_alter but where should I put this function?

  4. When creating a custom registration form shall I use default registration page i.e. /user/register and customize it?

I am sorry if above questions look very childish and silly! Hope you will consider my my newbie status. If possible please help me with a step by step solution.

Thanks!

UPDATE

To accomplish the requirement I created a custom module called user_signup and in user_signup.module file I have written the following code.

<?php
/*
Implements hook_menu()
*/

function user_signup_menu(){
  $items = array();
	$items['user/signup'] = array(
		'title' => 'Sign Up',
		'page callback' => 'drupal_get_form',
		'page arguments' => array('user_signup_registration_page'),
		'access arguments' => array('access content'),
	);
  return $items;
}

function user_signup_registration_page($form, &$form_state){
  $form['name'] = array(
        '#title' => 'Username',
        '#description' => 'choose a username',
        '#type' => 'textfield',
        '#required' => TRUE,
    );
    $form['mail'] = array(
        '#title' => 'Email',
        '#description' => 'enter a valid email address',
        '#type' => 'textfield',
        '#required' => TRUE,
    );
    $form['pass'] = array(
      '#title' => 'Password',
      '#description' => 'Enter a strong password',
      '#type' => 'password',
      '#required' => TRUE
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Create Account'),
    );
    return $form;
}

function user_signup_registration_page_submit($form, &$form_state){
  $new_user_id = db_insert('users')
    ->$fields(array( **//this is line number 45**
      'name' => $form_state['values']['name'],
      'mail' => $form_state['values']['mail'],
      'pass' => $form_state['values']['pass'],
    ))
    ->execute();

    drupal_set_message(t('New user created'));
}

Everything works perfectly but when I hit the submit button I am getting this error:

Fatal error: Method name must be a string in D:\xampp\htdocs\imdbind\sites\all\modules\user-signup\user_signup.module on line 45

I have marked line number 45 in above code snippet as **//this is line number 45**. I did not find any difference when comparing my code with theirs. What I am doing wrong?


Solution

  • Just change line 45 with following From:

        ->$fields(array( **//this is line number 45**
    

    To

        ->fields(array( **//this is line number 45**