0

I have this scenario that I am trying to walk through. I am new to creating custom modules, but I'm working on it (only way to learn...right?)

How would I go about doing this as I'm currently getting an error when trying. I have a role: 'venue admin' that will be responsible for adding other users with the role of 'customer'. I already am using the module: Role Delegation to handle the permissions side of things. But on the User Registration screen, I would like the customer role to be pre-selected and hidden from display.

This is what I have so far:

/**
 * Implements hook_form_FORM_ID_alter().
 *
 */

function mandatory_role_form_user_register_form_alter(&$form, &$form_state,   &$form_id) { 
global $user;

if(in_array('venue admin', $user->roles)) {   
// by default notify user of new account is checked  
    $form['account']['notify']['#default_value'] = TRUE;
// the only role the venue admin role can create is a customer so this is checked and hidden.
    $form['account']['roles']['#access'] = FALSE;
  }
}

obviously, this is not complete and missing the ability to set the role. Any suggestions?

  • If user is not logged in, how can you check in_array(). If user is not logged he will always be an anonymous user. So, it will never come inside of if condition – NTT Aug 30 '16 at 15:06
  • The venue admin is logged in and has access to create other users. I substantiated the global user object and can print inside the condition – MRC Aug 30 '16 at 15:22
  • I Think you have to remove #access line and use hide() function to hide option elements – NTT Aug 30 '16 at 15:27
  • What does the error you are getting look like? – Stefanos Petrakis Aug 30 '16 at 15:44
  • I'm less concerned about hiding the roles as I can do that with css at the very least, but I would like to set the role by default on the user register screen when the admin is logged in. – MRC Aug 30 '16 at 15:47
0

well..after staring at this question and trying a bunch of things on my own I think I stumbled upon the answer. Using HOOK_user_presave did the trick! This helped immensely: https://drupal.stackexchange.com/a/79065/28353

function mandatory_role_user_presave(&$edit, $account, $category) {
global $user;
if(in_array('venue admin', $user->roles)) {
  if($account ->is_new) {
    $roles = user_roles(TRUE);
    $rid = array_search('customer', $roles);
    if($rid != FALSE) {
      $edit['roles'][$rid] = 1;
        }
      }
    }
  }

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.