Drupal - How to hide a custom user field in user profile?
I think Field Permissions is what you are looking for. Here is a quote about it (from the module's project page):
... allows site administrators to set field-level permissions to edit, view and create fields on any entity.
Features:
- Enable field permissions on any entity, not just nodes.
- Role-based field permissions allowing different viewing patterned based on what access the user has.
- Author-level permissions allow viewing and editing of fields based on who the entity owner is.
- Permissions for each field are not enabled by default. Instead, administrators can enable these permissions explicitly for the fields where this feature is needed.
- Field permissions overview
To hide the field from the user profile form you can set the #access
property of a field to FALSE using hook_form_FORMID_alter
.
The following snippet hides the field field_organisation
from the user profile form for non-admins:
function YOURCUSTOMMODULE_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
$current_user = user_uid_optional_load();
if($current_user->uid != 1) {
$form['field_organisation']['#access'] = FALSE;
}
}
See also this similar question on drupalanswers
You can also hide the field from the user profile page (not the form) using template_preprocess_user_profile
The following snippet hides the field field_organisation
from the user page for non-admins:
function YOURCUSTOMMODULE_preprocess_user_profile(&$vars) {
$current_user = user_uid_optional_load();
if($current_user->uid != 1) {
unset($vars['user_profile']['field_organisation']);
}
}