drupaldrupal-7user-profilehook-form-alter

Drupal 7 Edit User Page Override


I need to override the 'user/%user/edit' page for a specific user role, say 'foo_role'. I have successfully created the new user registration form that explicitly selects the fields to be used in creating a new foo_role user however, because there are additional user fields not applicable to foo_role the edit page for this role is not correct.

This is my best attempt, but it fails:

function registermodule_form_user_profile_form_alter($form, &$form_state){
        global $user;
        if(in_array("foo_role", $user->roles)){
            unset($form['field_non_foo']);
        }   
        return $form;
 }

Solution

  • Alter hooks use variable reference to modify variable contents in memory. Because of that, you also don't need to return a value in alter hooks.

    Should look like this:

    function registermodule_form_user_profile_form_alter(&$form, &$form_state){
      global $user;
    
      if(in_array("foo_role", $user->roles)){
        $form['field_non_foo'] = FALSE;
      }   
    }