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']);
unset($form_state['field']['field_non_foo']);
}
return $form;
}

Comments

luckyape’s picture

The problem with the function was improperly passing of the $form arg - missing &

function registermodule_form_user_profile_form_alter(&$form, &$form_state)

ethiaa’s picture

Eventually got it working for no visible reason. Code remained the same. Hey ho.

function mymodule_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
  // do something (anything) so I know it's worked
  $form['terms_of_use'] = array(
    '#type' => 'checkbox', 
    '#title' => t("I agree with the website's terms and conditions."), 
    '#required' => TRUE,
  );
}
geek-merlin’s picture

cache clear helps
:-)