I have 1 field on user registration page that should not be changed after account registration. It needs to be hidden or disabled. Which module can do this ?
8 Answers
You can try Field Permissions Module
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.
-
How can it differentiate between user creation and edit when it's the same field? I'm not saying it can't, I genuinely would like to know how. And I guess OP would, too.– MołotSep 11, 2015 at 11:06
-
It will be role based field permission, need to give permission only for anonymous(not sure will this condition works for the case).– Bipin KSep 11, 2015 at 11:47
If you want to do it in custom module
my_module_form_user_profile_form_alter(&$form, &$form_state) {
// Since it is on registration form, field might be reqruied.
$form['your_field']['#required'] = FALSE;
$form['your_field']['#access'] = FALSE;
}
-
-
2@Smalution You don't have to - for registration form it would be
my_module_form_user_register_form_alter
, notmy_module_form_user_profile_form_alter
.– MołotSep 11, 2015 at 11:12 -
1hook is hook_form_FORM_ID_alter FORM_ID for registration and edit is diefferent– arpitrSep 11, 2015 at 11:12
-
Ok got it. I have checked when user register its form id "user-register-form" and when user edit its form id "user-profile-form" normally.– DRUPWAYSep 11, 2015 at 11:34
-
Try with this code:
function MODULENAME_form_alter(&$form_state, $form_id){
$form[$field]['#access'] = FALSE;
$form[$field]['#disabled'] = TRUE;
}
-
2
-
Make field visibility property for anonymous users only. So Value will be stored at time of registration. Then no one can access that field. Once you install field permission module, you will find this option to apply to only anonymous user only.
function module_form_alter(&$form_state, $form_id) {
if (isset($form['title'])) {
$form['title']['#required'] = FALSE;
$form['title']['#type'] = 'value';
}
}
Try using hook_form_alter
, see the example below:
function MODULENAME_form_alter(&$form_state, $form_id){
global $user;
//$form_id :: user detail edit form id
if($user->uid != 0 && $form_id == "required_form_id"){
//$field :: requied field name
$form[$field]['#access'] = FALSE;
}
}
place this code in a custom module, field will be able on new content but will be disabled to edit it.
if ($form_id == 'YOUR_FORM_ID') {
//dsm($form);
// for user form use '#user' instead of '#node' and uid instead of nid
if (isset($form['#node']) && isset($form['#node']->nid)){
// Prevent editing a field once the node has been created
// hiding the field entirely on the edit form.
$form['field_name']['#access'] = FALSE;
// disable update field on the edit form (gray color).
$form['field_name']['#disabled'] = TRUE;
}
}
I actually think, that #disabled is not required, when you already have #access to false. Also #disabled is not a really good thing because it adds disabled=disabled to the form field which you can easily delete as a user.
What you want to achieve is:
- Access false
- the field value which the user gave for example at the registration form is still there.
You can achieve this in your module with the following code:
use \Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_form_FORM_ID_alter() for form 'user_form' (user edit form).
* - disable the change of field field_MY_FIELD (Registrierungstyp)
*/
function YOUR_MODULE_form_user_form_alter(&$form, FormStateInterface $form_state, $form_id)
{
// in the example you see also how to make the administrator role can still change the field
$currentuser = \Drupal::currentUser();
$roles = $currentuser->getRoles();
$authenticated = $currentuser->isAuthenticated();
if ($authenticated &&
!in_array('administrator', $roles)
) {
$form['field_MY_FIELD']['#access'] = FALSE;
$form['field_MY_FIELD']['#default_value'] = TRUE;
}
}