I am trying to create a custom made form for creating a user account and granting a default role access to that user. It all works except for the role assignment. I am using drupal_execute so that I can get the functionality of the user_register form. The problem is that unless I am logged in already with administrative access, the code to set the role assignment doesn't work. I imagine that is because those fields don't even exist on the form for non-administrative users.

Here is what I am doing now and I am very happy with how it works except for the role assignment part.

...set variables above...
$form_state=array();
$form_state['values']['name']=  $myloginname;
$form_state['values']['mail']=  $myemail;
$form_state['values']['conf_mail']=  $myemail;
$form_state['values']['pass']['pass1']=  $mypassword;
$form_state['values']['pass']['pass2']=  $mypassword;
$form_state['values']['notify']=1;

$form_state['values']['roles'][11]=11;  //this part only works if executed by a user who already is logged
$form_state['values']['roles'][12]=12;  //in with administrative rights.

$form_state['value']['op'] = t('Create new account');
drupal_execute('user_register', $form_state);
...check error status and inform the user...

Is there another way this can be done so that an annonymous user can create an account and be granted a specific role assignment automatically?

Comments