How to make form field read only? I want prevent any user from being able to change the value of a field, yet I want to display its value.

How can I achieve this?
This can be done via hook_form_alter

function mymodule_form_alter(&amp;$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

   if($form_id == 'user_register_form') {
      $form['field_name']['widget'][0]['value']['#attributes']['readonly'] = 'readonly';
   }

}


Add some css:

input:read-only {
  background-color: #ddd !important;
}


Result: