0

I'm trying to mark fields on the /user/_uid_/edit screen as disabled. These are custom fields I've created in /admin/config/people/accounts/fields in D8.

I have this code for another node entity type...

function MODULENAME_form_node_survey_edit_form_alter(&$form, $form_state, $form_id) {
  $form['field_content_type']['widget']['#type'] = 'hidden';
}

This works as expected, and on the node/123/edit screen for a that content type, that field is hidden. (works as expected)

(it gets populated by a migration process, and needs to stay how its set, even if users change other fields...)

My problem is, that I'm trying to disable a field on the USER-EDIT screen, such as /user/123/edit.

I'm trying the EXACT same code structure.

function MODULENAME_form_user_edit_form_alter(&$form, $form_state, $form_id) {
    $form['field_organization_reference']['widget'][0]['target_id']['#attributes']['disabled'] = 'disabled';
}

But it doesn't work.

I've tried throwing in class changes, and other things to alter the input fields, but they don't work.

$form['field_organization_reference']['widget'][0]['target_id']['#attributes']['disabled'] = 'disabled';
$form['field_organization_name']['widget'][0]['value']['#attributes']['disabled'] = 'disabled';    
$form['field_organization_reference']['#attributes']['class'][] = 'borkbork';    
$form['field_organization_name']['#attributes']['class'][] = 'borkbork';
$form['field_organization_name']['widget']['#type'] = 'hidden';

All as attempts to try and change something on the user edit form.

But nothing is working.

Is user-edit the correct form id? Is there something else I'm missing

Thoughts? Help? Thank you!

2 Answers 2

2

Try:

function MYMODULE_form_user_form_alter(&$form, $form_state, $form_id) {
  // Your code...
}

Inspecting element, I see the form id is user-form, which needs to be translated to the machine-readable name user_form

Otherwise, you can always do a standard HOOK_form_alter():

function MYMODULE_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'user_form') {
    // Your code...
  }
}

And always clear cache after adding/changing hook names.

1
  • MYMODULE_form_user_form_alter was the big help, thank you!
    – alphex
    Commented Jun 21, 2019 at 3:17
0

Here's the answer I was helped with.

function MODULENAME_form_user_form_alter(&$form, $form_state, $form_id) {

      if (!\Drupal::currentUser()->hasPermission('administer users')) {

        $form['field_organization_name']['widget'][0]['value']['#attributes']['disabled'] = 'disabled';  
        $form['field_organization_reference']['widget'][0]['target_id']['#attributes']['disabled'] = 'disabled';    

      }

}

The important part being **user* in the function name. not user-edit or user_edit.

The next challange was figuring out that the ORG REF field (being a auto complete reference) has a different structure then the name, which is plain text.

I used dpm($form['field_name']) on both, inside the function, and as UID 1 you can see the output of that dpm() call.

The IF check makes sure that admins can still see the fields to edit the user directly if they need to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.