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->type, $field['field_name'], $op...) {
// → $op=='edit' && isset($entity->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->nid);
$tmp->my_field['#attributes'] = array('readonly' => 'readonly'); // also tried to use 'disabled')
node_save($tmp);
I also tried:
$tmp->my_field['#disabled'] = true;
Thanks for any help.
Regards.