Setting a field readonly rather than invisible from hook_field_access()? I have a field that should not be visible when editing, in certain cases (depending on node state and user).
It works fine using hook_field_access():

function mymodule_field_access($op, $field, $entity_type, $entity, $account) {
  if ( ...conditions on $entity_type, $entity-&gt;type, $field['field_name'], $op...) {
    // → $op=='edit' &amp;&amp; isset($entity-&gt;nid) so only when modifying existing content
    return FALSE;
  }
  return TRUE;
}


As said it works fine, and the field is not displayed at all when access is not granted.

But in some particular conditions I would like to let the field visible but readonly (grey-ed, or anything preventing to modify it) rather than hiding it.

I tried to return TRUE and before to set according attributes to the field, but until now I failed to make it works as expected. What I tested:

$tmp = mode_load($entity-&gt;nid);
$tmp-&gt;my_field['#attributes'] = array('readonly' =&gt; 'readonly'); // also tried to use 'disabled')
node_save($tmp);


I also tried:

$tmp-&gt;my_field['#disabled'] = true;


Thanks for any help.

Regards.
Try something like that :

Replace FORM_ID with the real form ID.

function MYMODULE_form_FORM_ID_alter(&amp;$form, &amp;$form_state) {

  if(!user_access(ADMIN_USER)){
      $form['your_field_name']['#disabled'] = TRUE;
    }

    $form['your_field_name']['#access'] = TRUE;
}