Active
Project:
Drupal core
Version:
main
Component:
field system
Priority:
Minor
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
29 Aug 2011 at 06:10 UTC
Updated:
22 Oct 2020 at 07:26 UTC
Jump to comment: Most recent
API page: http://api.drupal.org/api/drupal/modules--field--field.api.php/function/...
The documentation says returning false will not allow that operation. For example the following disables editing of the field:
function grom_field_access($op, $field, $entity_type, $entity, $account) {
if ($op == 'view') {
return true;
}
if ($field['field_name'] == 'field_it_notes') {
return false;
}
return true;
}
Is it possible to limit the field so its read only when editing of the field is requested?
Comments
Comment #1
grom358 commentedNot a bug. My code sample was incorrect
Comment #2
grom358 commentedComment #2.0
grom358 commentedFixed code sample
Comment #3
yched commentedI don't think I get this. You can specify access for the 'edit' $op.
Comment #4
grom358 commentedYeah but if you don't give it access for the 'edit' $op then it doesn't show at all. I would like it to show up read only. So the user can see the value of the field but can't change it.
Comment #5
grom358 commentedComment #6
yched commentedAh got it.
This is more complex than it sounds. This means either :
- displaying the field value in a disabled widget. Would require all widgets to support a 'disabled' state, which is currently not in the contract for 'being a widget',
- displaying the field value using a formatter, but then - which formatter should be used ? Would require some way of choosing the formatter.
Comment #7
larowlansubs
Comment #8
decipheredJust adding my 2c from discussion in IRC:
I agree with the disabled state approach, it gives the Widget developer control on how the field should act when disabled.
However, that doesn't look to be the way that grom358 is leaning, so in the case of a Formatter, there would need to be a Display mode for the form as there are many formatters that wouldn't make any sense in the back-end (such as a very large Image Style).
Ultimately, some in between would also be nice, have the default formatter for the back-end/form display act differently from the standard default formatter. An example would be an Image field, the default for use in the front-end is to render it as an Image, the default for the back-end could be just the files URI.
Cheers,
Deciphered.
Comment #9
joachim commentedMy feeling is that a widget that's visibly disabled is much better UX than outputting a formatter.
Tagging for input from the usability team.
Comment #10
grom358 commentedIf I make the widgets support #disabled state, how to control when it should be disabled? The following is code as proof of concept:
With this its controlled through the field permissions. Then using modules like Field Permissions and Rules can setup making a field view only when editing the node.
Comment #11
yched commented'disabled' widgets would work by having field_default_form() do the field_access() checks and specifying #disabled = true in the base $element provided to hook_field_widget_form()
Comment #12
grom358 commentedUpdated code. Works with field group module. Setting the form state wasn't needed for my test case but added it in case it may be needed elsewhere.
Comment #13
Bojhan commentedehh?
Comment #14
larowlansee also
http://drupal.org/project/field_extrawidgets
Comment #15
sheldon rampton commentedI had a situation where I needed to be able to show some fields on the node edit form, but only enable editing of those fields for certain user roles. Other user roles are supposed to be able to view those fields while editing but not modify them. the field_extrawidgets module was not a good fit for my needs because it doesn't support making fields read-only on a per-user-role basis. I therefore wrote my own down-and-dirty solution by adding the following code to my "job" module:
Comment #16
sheldon rampton commented@yched I think my use case -- where some user roles should be able to view but not edit certain fields while editing -- is likely to come up from time to time as something that people want. I realize that some widgets may not support a disabled state, but most if not all of the standard field types in core support #disabled, so I think it should be possible to support this for all field types that recognize a #disabled attribute. For any field types that don't recognize #disabled, the failover is pretty benign.
It would be easier for the field permissions module to support this if Drupal's node access options included a "display as read-only during editing" option. I've started a ticket that suggests this: http://drupal.org/node/1391544
Comment #16.0
sheldon rampton commentedChanged issue to feature request
Comment #17
anrikun commentedOn D7, you may try the Field Readonly module.
Comment #18
amateescu commentedThis functionality has been available in D7 contrib for more than two years in the Field extra widgets module, as noted by @larowlan in #14 ...
Comment #19
sheldon rampton commented@amateescu: But as I noted in #15, the Field Extra widgets module does not support making fields read-only on a per-user-role basis. On the website I was building when I posted my comment two years ago, they wanted some user roles to be able to see the read-only widget when editing, and they wanted other user roles to actually be able to edit that field.
I wrapped up that project a long time ago, so this is no longer an issue for me in any current projects. Out of curiosity, though, does the Field Readonly module provide that granularity on a user-role basis with respect to field editing/viewing when editing content?
Comment #20
anrikun commented@Sheldon: As stated on the project page, the Field Readonly module works in conjunction with any module like Field Permissions that makes some fields non-accessible. It simply displays them on edit forms as read-only items instead of letting them hidden.
It fully respects Field Permissions' settings.
Comment #21
amateescu commented@Sheldon, I don't think that functionality needs to be provided by a module, all that's needed is a implementation of
hook_field_widget_properties_alter()that changes the widget type based on the user role.Comment #30
andypostThere's contrib now https://www.drupal.org/project/config_readonly
Comment #32
sherkajon commentedSo far I have achieved read only mode of the fields on edit mode by using Field Permissions in combination with Read-only Fields modules.