I have a node (group) with an entity reference field that is used to link it to other nodes (stations). The relationship between them is 1:N (one group : many stations).
The problem is that I need to limit the edit widget so that certain users can see (and also edit) only specific station links. I have no problems with limiting which stations can be added, I know that I can easily do that via custom validation + entityreference selection widget.
The real trouble is with limiting which already existing references to stations can be deleted / viewed. Imagine that I have this structure:
user1 user2 Group1 -station1 -station2 //User1 is allowed to see (and edit) only station1, user2 only station2.
Then I'd need to edit the Group1 edit form in a way that when user1 access it he/she only sees the station1 in the stations references widget (and can therefore remove only that referece). Following the same principle, when user2 access the group1 edit form, he/she can only see (and edit) the station2 reference.
I know how to limit the deletion itself via setting the '#disable' property of the individual reference...
$form['entity_reference_field_name']['und']['target_id']['#disabled'] = true;
...but that doesn't hide, just disables it for removal.
There's also the possibility of setting the '#access' property of it which actually hides the record, the problem is that it looks just terribly ugly (and like a clear 'hack').
$form['entity_reference_field_name']['und']['target_id']['#access'] = false;
I can't unset the reference because it would delete it.
So, does anyone have any idea how to it? Preferably without having to clone the entity reference module and editing its core.