1

So I have three types of users - admin, LA admin and users. I am trying to set it up so that admins and LA admins cannot edit the username, password and timezone for users. I am talking about the default user edit form for admins and the form ID is "user-profile-form".

I have created a custom module but this doesn't seem to be working. Any idea what I might be doing wrong?

Even the var_dump does not seem to be outputting. I have cleared the cache and verified that the module is enabled.

function profile_change_form_alter(&$form, $form_state, $form_id) {
    if ($form_id === 'user-profile-form') {
    // Use var_dump  to dump the $form array to see the fields. 
        var_dump ($form);

        hide($form['account']['pass']);
        hide($form['account']['current_pass_required_values']);
        hide($form['account']['current_pass']);
    }
}
1

2 Answers 2

3

You need to replace form id user-profile-form with user_profile_form

Your code should look like the following:

function profile_change_form_alter(&$form, &$form_state, $form_id) {
    // changed form id
    if ($form_id === 'user_profile_form') {
        // Use var_dump  to dump the $form array to see the fields. 
        var_dump ($form);

        hide($form['account']['pass']);
        hide($form['account']['current_pass_required_values']);
        hide($form['account']['current_pass']);
    }
}
3
  • You can use also $form['account']['pass']['#access'] = FALSE;
    – milkovsky
    Jun 12, 2013 at 19:37
  • @milkovsky True. Also, unset($form['account']['pass']);
    – Aboodred1
    Jun 12, 2013 at 19:52
  • $form['account']['pass']['#access'] = FALSE; using this line resetting the user role from admin to normal.
    – NeiL
    Dec 21, 2016 at 13:17
0

This code will work if you want to disable the field instead of hiding them.

function MODULENAME_form_alter(&$form, &$form_state, $form_id) {
  global $user;
  if ($form_id == 'user_profile_form') {
    if ($user->uid != 1) {
      $form['field_joining_date']["#disabled"] = TRUE;
    }
  }
}

Replace the 'field_joining_date' by your custom field name.

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.