How to hide certain fields on the User Edit form in Drupal?

16,709

Solution 1

If you use hide() you will remove the field, but hide is more for "delaying" field rendering... Like you hide it, but then (in template file) you print it at some other place. Because, if you don't print it later it won't be rendered in the page, won't be saved, if it's mandatory you'll get validation error and so on.

If you want to hide it, so user can not see it but you want the form to keep previous value of the field use something like:

$form['field_yourfield']['#access'] = FALSE;

and if you want it to be visible, but disabled (user can't change it's value) then:

$form['field_yourfield']['#disabled'] = TRUE;

Solution 2

I am assuming you module name is PROFILE_CHANGE & so that you have used it in the format of hook_form_alter(), where you have replaced hook with your module name profile_change.

You have put 3 '=' sign where you are giving condition to check form id, which is user-profile-form. I have put simple equal sign which is '==' & it's working.

function profile_change_form_alter(&$form, $form_state, $form_id) {
    if ($form_id == 'user-profile-form') {
       hide($form['account']['pass']);
       hide($form['account']['current_pass_required_values']);
       hide($form['account']['current_pass']);
    }
}

Don't use var_dump(), You should always use DEVEL & check the output of $form like dpm($form) just after your hook function for form alter. This will give you all info about form, where ever you have a form on your page.

function profile_change_form_alter(&$form, $form_state, $form_id) {
  dpm($form);
}

Solution 3

Actually I just had to change user-profile-form to user_profile_form in my code for it to work. For some reason, drupal requires underscores.

Share:
16,709

Related videos on Youtube

raeq
Author by

raeq

Updated on September 24, 2022

Comments

  • raeq
    raeq almost 2 years

    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') {
           var_dump ($form);
    
           hide($form['account']['pass']);
           hide($form['account']['current_pass_required_values']);
           hide($form['account']['current_pass']);
        }
    }
    
  • Clive
    Clive about 11 years
    The $form_id is the ID of the form as taken from its PHP function name, not the HTML ID (which isn't guaranteed to be consistent). That's why it contains underscores and not hyphens when you alter the form
  • Chris Ruppel
    Chris Ruppel almost 10 years
    "for some reason" is because dashes and underscores are not equivalent strings. The markup Drupal outputs contains dashes, but internally it uses underscores. You can find the Form ID by viewing your source and finding input[name="form_id"] and using that exact value. Try running this jQuery snippet in your browser console on any Drupal admin form page: jQuery('input[name="form_id"]').val()