I have a need to allow users to edit some editablefields, but I don't want to provide them full Node Update access / permission.
This problem is also described in for D6-version in:
#1021778: Editing fields without update access (D6) , which adds a new permission in the module itself, and
#488816: Requires "Edit Any" Permission, which also includes some comments on $node->status.
Since the D7-code/solution is much shorter, I created this separate issue.
The following patch allows editablefields in e.g. Views with EITHER Node_update_access OR field_edit_access:
// See if access to this form element is restricted,
// if so, skip widget processing and just set the value.
- if (!entity_access('update', $entity_type, $entity) || !field_access('edit', $field, $entity_type, $entity)) {
+ if (!entity_access('update', $entity_type, $entity) && !field_access('edit', $field, $entity_type, $entity)) {
// Can't edit.
return editablefields_fallback_formatter($entity_type, $entity, $field, $instance, $langcode, $items, $display);
Then, in custom code, I implemented hook_field_access().
/**
* Implements hook_permission().
*/
function my_module_permission() {
return array(
'edit editable fields' => array(
'title' => t('edit editable fields while viewing content'),
),
);
}
function my_module_field_access($op, $field, $entity_type, $entity, $account) {
if( $op == 'edit' && $field['field_name'] == 'field_my_field' ) {
$access = user_access('edit editable fields', $account);
return $access;
}
}
Comments
Comment #1
johnvHere is a proper patch.
Comment #2
Taxoman commentedMarked #1391906: Edit own content permission not applied? as a duplicate of this issue, since the patch is here.
Comment #3
skylord commented+1 for this patch.
BTW, "field_access" returns TRUE be default, so it will work OK without custom hook_field_access() implementation. Also keep in mind https://drupal.org/project/field_permissions and other field access modules - custom code is generally not needed at all.
Comment #4
emattias commentedHere's the patch in #1, made to work with the current HEAD.
Comment #5
emattias commentedComment #6
emattias commentedHere's an update to the #4 patch. This new patch makes it so that if you have entity_access but not field_access you get the fallback. The #4 patch didn't show that fallback in that case.
Comment #7
FrancoNogarin commented+1 for this patch :D
Comment #8
bsarchive commented!! This patch risks allowing users to edit all editable fields.
Because hook_field_access returns TRUE by default, if you put this patch in, all users will be able to edit all fields unless you've denied them access to those fields through hook_field_access or some other means.
Comment #9
Leeteq commentedComment #10
lchang commentedMaybe we can use the Field Permissions module.
Comment #11
Mouna Hammami commentedHello,
#10 works fine.
this is a similar patch for 7.x-1.0-alpha2+14-dev version
Comment #12
bisonbleu commentedThanks @Mouna Hammami, patch in #11 works for me
Comment #13
osopolarThe patch works fine, but in my opinion it should not introduce a new dependency, as this would not be compatibility with existing installations.
Instead there should be a check if the field-permission module is enabled or not. If not, entity_access() should be used. If it is present, then there should be an option (for each entity bundle) to choose which type of access check should be used (field_access or entity_access), entity_access should be default to not change current behavior by just enabling the module.