0

I want to create separate user types. By "type", I don't mean just roles. Let me explain by an example.

User comes to my website and he wants to register either as "User" or "Editor". Both these profiles will have very different fields and different permissions.

Now, I can use profile2 module to create different profiles and restrict them to different roles. Furthermore, I can use Autoassignrole module to assign the desired role to the user during registration. This is all fine.

Now, what I want is separate links for registration for both roles. When user clicks on any one of them, only the relevant "profile2" profile should be shown in the form. The problem is that on the default registration form, both profiles are shown.

One could say that I could just uncheck the option in the "profile2" profiles and not show them during registration, and user can enter his profile after registration. But here is the thing: I want some of the profile fields to be entered during registration itself. This is required for my case.

So, now you get what I mean by "type"? It is surprising there is no straightforward solution to this basic functionality. How are you achieving this?

  • 1
    There is a patch for profile 2 that addresses that. I think it is at Restrict profile2 type per roles. If not, google it. – J. Reynolds Nov 30 '14 at 15:23
  • Thanks Reynolds. This patch is a useful addition but still doesn't fully serve the purpose. The patch has been committed to devel version, so I tested it there. It is definitely restricting profiles according to roles but still while creating a new user there is no option to restrict the display of profile forms according to the role user chooses to create. – shivams Nov 30 '14 at 16:12
  • 1
    you could create a Rule that would limit the available options using the URL as the defining element. www.mysite.com/user/register?type=type1 , etc. Then set the value of the field based on which type is showing - using one of them as a default – Geoff Nov 30 '14 at 22:49
  • @Geoff: that's smart! I'll try that out. But does Rules have the setting to limit? – shivams Dec 1 '14 at 9:17
  • 1
    I was thinking about this further - you are probably better off doing a direct form adjustment on registration - would be only a few lines of code in template.php and much less resource heavy than Rules - if I can get something to work, I will post as an answer – Geoff Dec 1 '14 at 15:50
1

This should work (at least it did for me). Put this code in your template.php and adjust accordingly.

I created a user field called 'type' as a select list (for future consideration). You could just as easily use a hidden field or a text field if you don't want the user to be aware of a difference (NOT FOR SECURITY PURPOSES - THIS IS VERY INSECURE). Make it mandatory.

given a url of mysite.com/user/register?type=one this will assign the field named 'type' to 'one', otherwise it will assign it 'two' by default.

I added some comments to help with the code, but you can remove that later. Obviously you will need to adapt this to the profile selection - I don't profile2 setup on my test site so I can't really get into that, but this is the basics, and I can help guide through the rest.

function theme_form_alter(&$form, &$form_state, $form_id) // change theme to your theme name
{
    if($form_id == 'user_register_form')  // only check on user registration form
    {
      $urlGet = drupal_get_query_parameters(); // get URL parameters ?type=one, etc
      if($urlGet['type'] == 'one') // check ?type= against expected
      {   
        $form['field_type']['und']['#options'] = array('One' => 'One'); // set select list option(s)
        //$form['field_type']['und']['#options'] = array('One' => 'One', 'Three' => 'Three'); // for multiple values
      } else
      {
        $form['field_type']['und']['#options'] = array('Two' => 'Two'); // default value
      }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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