3

I have a situation where I would like a specific user role be able to add users without assigning them a password but I can't seem to find a way to do this. Has anyone done this before? I feel like this is something commonly done. Any help would be appreciated.

Here is the module I have and currently, this code just outputs at the top of the screen for all pages? This is my first module.

Here is my hide_some_fields.module file

function hide_some_fields_form_alter(&$form, $form_state, $form_id) 
{

  global $user;

  if($form_id == 'user_register_form') 
  {                    
    if(in_array('operator', array_values($user->roles))) 
    { 
      unset($form['account']['pass']);  

    }
  }
}

Here is my .info file as well:

name = Hide Some Fields
description = Hides some fields. The password field when the operator adds patients. 
core = 7.x
0
function modulename_form_alter(&$form, $form_state, $form_id) {
  global $user;

  if($form_id == 'user_register_form') {
    if (in_array('your_role', array_values($user->roles))) {
      unset($form['account']['pass']);
    }
  }
}

This should work for you - just remember to change modulename to your module.

  • Thanks for a quick response. Would this go in a custom module? – Mike May 18 '12 at 15:19
  • 1
    Yeah, throw it in a custom module. If you have any problems just let me know. – Rawkode May 18 '12 at 15:20
  • Did you think about the password field that's required when people want to log in? This seems like a bad idea, give them a default password in order to allow them to log in to the site. – JurgenR May 18 '12 at 15:29
  • Users without a password cannot log-in - you could hook on to user_save to provide a default password as JurgenR is suggesting. – Rawkode May 18 '12 at 15:31
  • 1
    Seems like there's no <?php tag? – JurgenR May 18 '12 at 15:48
2

People need a password in order to log in to the site.
Therefore you need to allways insert a password when creating new users.
You can add a default password and set the field as hidden on a specific role but new users will need to know the default password in order to log in.


function MYMODULE_form_alter(&$form, $form_state, $form_id) {
  global $user;
  if($form_id == 'user_register_form') {

    $default_value = isset($form_state['build_info']['args'][0]['pass'] ? $form_state['build_info']['args'][0]['pass'] : 'DEFAULT PASSWORD';

    if (in_array('YOUR ROLE', array_values($user->roles))) {
      $form['account']['pass']['#default_value'] = $default_value;
      $form['account']['pass']['#access'] = FALSE;
    }
  }
}

EDIT:

Ok I took the time to work this out and therefor I want to adjust my solution:
In order to get this properly working you need to override the full $form['account']['pass'] array. This is because it has the password_confirm type as default, which has the extra confirm field, keeping this type will result in an empty password field on the node submit.
Clive is absolutely right, using a #default_value on a password field results in an error so we need to use the #value. Thas was the unsupported operand types error.
Also, in my first answer I added a condition so see if the form is used to edit a user but now I know that the registration form and edit form are two different forms calling the same user_account_form so we can remove the condition.


function mymodule_form_alter(&$form, $form_state, $form_id) {
  global $user;
  if($form_id == 'user_register_form') {
    if (in_array('USER ROLE', array_values($user->roles))) {
      $form['account']['pass'] = array(
        '#type' => 'password',
        '#size' => 25,
        '#access' => FALSE,
        '#value' => 'DEFAULT PASSWORD',
      );
    }
  }
}

I tested this code and it works like a charm so if you have any problems with it feel free to contact me.

  • looks good, trying this now. – Mike May 18 '12 at 16:10
  • This does not work: Fatal error: Unsupported operand types in /home/mghen/public_html/mobile_mashavu/includes/form.inc on line 2326 ... Can we assign it a default value since it is a password field? – Mike May 18 '12 at 16:20
  • 1
    Password elements can't have a #default_value (see the FAPI reference), try using #value instead – Clive May 18 '12 at 16:46
  • Due to comments of Mike and @Clive I edited my answer. – JurgenR May 18 '12 at 17:50

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.