0

I have a content type that has a mix of user-editable fields and auto-populated fields (from an external source). I would like to mark those fields as disabled right when I create them so the user cannot overwrite them. I know there are readonly modules and ways to do this in hook_form_alter but I'm curious if I can short circuit having to do this on the fly and just have it disabled by default. Thanks.

3 Answers 3

0

No, fields don't have a 'disabled' setting by default.

You could however use hook_field_access to restrict access to editing them

/**
 * Implements hook_field_access
 */
function MODULE_field_access($op, $field, $entity_type, $entity, $account) {
  // Define read only fields
  $readonly = array('field_my_field_1', 'field_my_field_2');
  if ($op == 'edit' && in_array($field['field_name'], $readonly)) {
    // Disable edit.
    return FALSE;
  }
  return TRUE;
}
3
  • There is another condition to this issue - the field is a list_text field which I'm populating via an external database. The field shows up fine in display view complete with populated data, however, since list_text requires a selection from an available values list, in edit view this field always defaults to None - which means when they save, it overwrites the data with None. Even if I use hook_field_access, won't it still default to None in the form view and even though greyed out won't it still save as None?
    – Oddible
    Commented Apr 8, 2014 at 2:52
  • No, the field won't show on edit view and the user won't be able to set its value. Commented Apr 8, 2014 at 3:33
  • Thanks @David, unfortunately this didn't work. It successfully doesn't show the fields on the form but when I save it still saves as if the fields were there. So for instance, I have a list field ('Things') which is populated nightly from a database update. I have set this field to return FALSE and it does NOT show on the form, however when I hit Save on the edit form the field is cleared and is now empty. The problem is that my widget for this field isn't showing the data in edit view (apparently even when it isn't on the form) it always shows 'None').
    – Oddible
    Commented Apr 8, 2014 at 19:02
0

If you want a non-code way, would https://drupal.org/project/field_permissions not help? It would remove them from the edit screen and then only display them in the display field.

The only issue is whether fiel permissions will let you complete the value using an external source. But if that works okay then it would save some coding.

0

I believe you can able to do it while defining them using field_create_instance .. If you see the documentation..

settings: each omitted setting is given the default value specified in hook_field_info().

You can define settings saying disabled => TRUE

'settings' => array('disabled' => TRUE),

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.