I have an entity reference field that I am using within a "Review" content type. For general use, the widget type is set to select list, and it is populated by a view. In the field, users select from a list of their "Bands" (another content type).
Sometimes a review's content must to be moderated/edited by an administrator. However, when an admin goes in to edit the node, the select list is populated with the administrator's bands. As such, the admin can only select from their own bands, and not from anybody else's bands. This means that the original band that is associated with the review cannot be selected, and the review can no longer be associated with the correct "Band". As a result, the node cannot be edited by moderators, because if saved, the original reference is replaced with a new, incorrect one.
I'm wondering if any of the following three scenarios is possible:
- Change the widget type from "select list" to "autocomplete", but only for administrators. This way, the administrator can choose from all bands, and select the original one for inclusion in the review. This seems like the most plausible option.
- Populate the select list with the author's bands instead of the current admin's bands.
- Don't allow the admin to alter the content's of the entity reference field, thereby keeping the original selection intact.
Any help with this issue would be greatly appreciated.
Comments
Comment #1
mrweiner commentedI think I've figured out how to target the field's widget, and to do this based on the admin role, but the module doesn't seem to be recognizing the widget type for some reason. The reason I say this is that when the module is enabled, the targeted field disappears in the node edit form. I figure this means it's getting targeted correctly, but can't define how to display itself. I think this might be because the autocomplete widget is defined by the Entity Reference module itself, and not by Core. Do I have to tell the module where to find this widget type? Or is there another reason that this might be happening?
Comment #2
mrweiner commentedSo, I've figured out part of the issue, and have been able to change the widget from a select list into an auto complete field. However, the issue still exists that admins are only allowed to enter values that exist in the original select list. This is because the only allowed options are still the admins' own bands, from the view that populates the options in the select list. I now need to figure out how to alter the settings for the field.
I have been unsuccessfully working on this for hours, because I'm not sure what elements to target. I've tried altering the settings through
$form_stateusinghook_form_alter, which apparently doesn't work because$form_stateis called after submission or something like that, thereby removing the altered settings. My latest attempt is below, throughhook_field_widget_properties_alter. But, no matter what I try, I can't get these settings to stick. I'm guessing it's because you can't directly alter$contextor something.If anybody has any suggestions, I'd really appreciate some help. Latest attempt:
Comment #3
Leeteq commentedIMO, this should be its own module that lets us (optionally) set roles-based access to any widget.
BTW:
http://drupal.stackexchange.com/questions/37771/change-entity-reference-...