I'm using D7.
I have one content type, let's say Content-A. And, the Content-A has a field whose field type is Entity Reference. Content-A's authors can reference other user with the content through the Entity Reference field.
I want to make authors be unable to remove the entity reference between the content and other users who he already referenced. Does anybody know how to do it? Any advice will be appreciated.
2 Answers
Using Hook_form_FORM_ID_alter
on the node form and disabling the field for users that are not the initial author should work.
Code is untested but shows the general idea behind the solution
HOOK_form_FORM_ID_alter($form, &$form_state){
global $user; // Get the current logged in user information
if($user->uid != $form_state['node']->uid){ // Is the current user not the original author of this node?
$form['FIELD_REFERENCE']['und'][0]['#access'] = 0; // Restrict access to this field (You'll have to check where exactly in the tree to place this).
}
}
-
hi Suranga, thank you for your answer. I tried your way coding on both of template.php and custom module. But it didn't work... Can you tell me what you mean "You'll have to check where exactly in the tree to place this"? May 24, 2015 at 5:14
-
Use this hook in the module layer. Once you have implemented the hook be sure to flush your caches. try printing out the $form['FIELD_REFERENCE'] variable .Where FIELD_REFERENCE is the machine name of the field. This should print a rather long array when viewing an edit page (you might have to refresh the page once). In this long array there should be a #access key somewhere. This key has to be set to 0 to restrict access. If no message is shown be sure to check or your hook/form_id is set correctly. May 24, 2015 at 6:50
You could use the field permissions module for this, give access to add but not edit their value. https://www.drupal.org/project/field_permissions