2

I know about assigning roles and permission in DRUPAL admin login where admin can add user and even add new roles (check here) to the new users.

I have a basic registration form in my page and while user register I want to assign roles to them for example - employee, superuser and admin roles.

How do I do it, do I have create a new module and may be use the ROLE module functionality in Core DRUPAL.

I have also checked this Link which explains about basic Users, roles and permissions in DRUPAL but it does not answer my question.

So

  1. I need to create new user in Drupal from my own Registration form.

  2. I need to have my own custom roles (eg. employee, superuser and admin roles) - This Link might help me in this

  3. This new user which are registering from my own custom registration form, should be assigned with this custom roles(eg. employee, superuser and admin roles).

Can some please tell me how do I do this in Drupal 7 or Does anyone knows any Drupal modules which can help me do this efficiently or should I go with own PHP code.

which will be more efficient way for doing this.

Thanks in advance :)

3
  • Are you registering users by using user_save() function. Jul 17, 2014 at 6:26
  • I have not started this task i am doing the analyzation. So I am open to any thing .... i am not too sure if i have to use user_save ... but reading from here api.drupal.org/api/drupal/modules%21user%21user.module/function/… it seems user_save is a good option
    – Hitesh
    Jul 17, 2014 at 7:21
  • You can check out below my code..i have created registration form like that Jul 17, 2014 at 7:59

3 Answers 3

2
    function company_form()
    {
         $form['company_username'] =  array(
          '#type' => 'textfield',
          '#title' => 'company-username',
          '#default_value' => '',
              '#required'=>'true' 
      );  
          $form['company_email'] =  array(
          '#type' => 'textfield',
          '#title' => 'company-email',
              '#required'=>'true', 
          '#default_value' => '',
          '#element_validate' => array('generate_special_form_validate'),
      );  
           $form['company_password'] =  array(
          '#type' => 'textfield',
          '#title' => 'company-password',
               '#required'=>'true', 
          '#default_value' => ''
      );  

      $form['submit'] =  array(
          '#type' => 'submit',
          '#title' => '',
          '#value' => 'Submit',
          '#default_value' => ''
      );
      return $form;
    }
    function company_form__validate(&$form_state,$form) {

         if (!valid_email_address($form['input']['email'])) {
         form_set_error($form['input']['email'],'Please enter a valid email address.');
       }
    }
    function company_form_submit($form, &$form_state)
    {


     global $user;
        $company_username = $form_state['input']['company_username'];
        $company_email = $form_state['input']['company_email'];
        $company_password = $form_state['input']['company_password'];        
         $fields = array(
              'name' => $company_username,
              'pass' =>$company_password,
              'mail' => $company_email,
              'init' => $company_email,
              'status' => 1,
              'access' => REQUEST_TIME,
              'roles' => array(
               4 => 'company',
                ),

        );
        $account=user_save('', $fields);  


    if($account->uid)
    {
     drupal_set_message('User Created SuccessFully');
    }

}

Firstly you have to create roles from backend. and then You can Assign as many roles by passing role id and role name in array.

'roles' => array(
               4 => 'company',
                ),

Hope this code helps you.

5
  • Great !!! let me try it from my end .. but it looks like it should work .... Thanks for the answer
    – Hitesh
    Jul 17, 2014 at 8:43
  • it is working here.hope it will work for you too Jul 17, 2014 at 8:51
  • Is it code ... which you have already used for your project
    – Hitesh
    Jul 17, 2014 at 8:52
  • yes i have used it Jul 17, 2014 at 8:54
1

Drupal 7 administrator can assign roles to existing users through standard interface. If you looking for additional functionality (i.e. users can choose their roles) take a look at Auto Assign Role module.

If you need to implement your own specific logic use Form API to build and validate your form through custom module.

1

To do this you might have to use some alter functions.

  1. First, have to alter the user registration form to add a new role option list.
  2. Second, in the user save, add the role id to the user_roles table.


/**
 * Implements hook_form_BASE_ID_alter()
 */
function CUSTOM_MODULE_form_user_register_form_alter(&$form, &$form_state, $form_id) {
  // Get the system roles.
  $roles = array_map('check_plain', user_roles(TRUE));

  $form['roles'] = array(
    '#type' => 'select',
    '#title' => t('Roles'),
    '#default_value' => array(),
    '#options' => $roles,
  );
}

/**
 * Implements hook_user_insert()
 */
function CUSTOM_MODULE_user_insert(&$edit, $account, $category) {
  // Save the role if it's set.
  if (isset($edit['roles'])) {
    // Push the roles to $account object so that user_save can get the role property.
    $account->roles[] = $edit['roles'];
  }
}

Your Answer

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

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