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']);
}
}
You would have to write some form of hook_form_alter() in a custom module and manually force the field to be hidden, like shown in this example. You'll be glad to hear that in Drupal 8 you no longer need to write any code to hide fields in a form!
In the content type's "display fields" tab, edit the display of the field for e.g. 'full node' to be editable. Create a new node of this content type and save it. Now go and visit this node. You will find that you can edit the field.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With