Updated: Comment #32
Problem/Motivation
When targeting user entities Entity reference does not allow filtering by user role nor user status. D8 version allows filtering by role (not status).
Proposed resolution
Add filters for user role and status. General approach approved in #17 and further implemented in #19.
Remaining tasks
Review and commit patch in #31.
User interface changes
Add filters for user role and status when targeting User entities.
API changes
None.
Original report by @wipeout_dude
Hi,
I am just starting a new development (my first with Drupal) and want to use Entity Reference to reference a user from a content type.. It works but there doesn't seem to be any way to limit the list of available choices.. All users are listed whether they are active or not or from any roles..
Is there a way to limit the list of users that are displayed by active users only and in certain roles?
Thanks..
| Comment | File | Size | Author |
|---|---|---|---|
| #31 | 1354482-31-entityreference-user_roles_filter.patch | 5.73 KB | fenstrat |
Comments
Comment #1
alan d. commentedCurrently this is not possible, but filters in status and role would be great. :)
Marked #1412716: Allow the Autocomplete Widget for Users to filter the users by role. as duplicate.
Comment #2
alan d. commentedComment #3
amitaibuI'm not sure this is a feature that should be in ER core. You can get, by implementing hook_entity_query_alter() in your custom module, adding those conditions.
Comment #4
alan d. commentedI know of 3 or 4 ways to do it myself, but ask the average user and it would be an impossible task.
Entity References is the logical replacement for References and by not having these filters is a clear regression. These two features are core in CCK User Reference and D7 References.
(Time permitting) Would you be interested in a patch? One that modifies EntityReferenceHandler_user::settingsForm() et al, a sub-module or as a completely separate module? I have 70 hours left of scoped work to get out by Monday and filtering two separate fields based on different roles is included, so I do not know how clean my solution for this requirement project will be.
Sorry to sound pushy, but would you reconsider / get Damien position on this.
Comment #5
amitaibuDamien, what do you say?
Comment #6
amitaibu> Entity References is the logical replacement for References and by not having these filters is a clear regression. These two features are core in CCK User Reference and D7 References.
Good argument, I actually think it then deserves to be in core
Comment #7
alan d. commentedExcellent. I am falling behind on my current work, but if I get time to do this correctly, I'll post my work.
Comment #8
modstore commentedI would find this feature useful as well.
Comment #9
alan d. commentedHere is a starter. It seems to work fine, but I highly dislike not being able to do a join on the user_roles table. Can you do joins using the EntityFieldQuery? (Maybe post here if you can: #1446600: EntityFieldQuery (pseudo-)join support).
I was not fully sure how to handle the anonymous user and developerd on 7.x-1.0-beta4 and then merged into dev. So I would normally mark this as needs work, but I see what the tests say :)
Comment #11
jaarong commentedI'm kind of surprised you can't filter the list of options on a user reference, subscribing to this issue.
Comment #12
agileadamThe patch from #9 applied cleanly to 7.x-1.0-beta5. Everything appears to be working great. I have limited an entity reference (user) select list to a single role.
Comment #13
roundbox commentedI haven't quite figured out how to add a patch. Any chance you could share the patched contents of your EntityReference_SelectionHandler_Generic.class.php file with me so I can see if I can get it to work? I have added enough to get the User Role selection when creating the field but not in the output display when I go to use it.
Thank you!
------
Got it finally!
Comment #14
murzPatch from #9 applies and works for me good, will be good to see it in core!
Comment #15
bachbach commentedHi,
#9 works for me, thanks Alan D !
Comment #16
mallezieI also can confirm the patch is working. Applies cleanly, though mentions an offset.
But seems ready to commit to dev-version
Comment #17
damien tournoud commentedOk, I am in support of adding this feature, even if it is not natively part of the Entity model of the User.
But the current implementation cannot go in as is:
... that needs to be re-written as part of
EntityReference_SelectionHandler_Generic_user::entityFieldQueryAlter()which allow modifying the SQL query generated by EFQ. Pulling the completely list of users cannot possibly scale :)Keep up the good work!
Comment #18
awm commentedThe patch works for me too but Is the any other way we can do it without this patch? In a custom module or so?
Comment #19
amitaibuMoved condition for user-roles into
entityFieldQueryAlter()Lets also add tests...
Comment #21
jorgensean commentedApplied patch to 7.x-1.x-dev, EFQ populating select lists by role beautifully.
Applied to 7.x-1.0-rc3 - still working.
Aside from testing issues, working as designed.
Comment #22
dmadruga commentedApplied patch (manually) to "7.x-1.0+4-dev" and it worked beautifully.
Comment #23
Paul B commentedThis version should pass the tests I believe.
Comment #24
univate commented23: 1354482-er-user-roles-23.patch queued for re-testing.
Comment #25
mmakela commentedAny estimation when this will be in core (thinking should I patch now or just wait..)?
Comment #26
alan d. commentedThink that answers your question ;)
Comment #27
mmakela commentedOh, I noticed the patch in #24 and thought that this is not in core yet, but might be in near future (since I think that limiting users as discussed is not working in newest drupal 7 installation I have made).
Comment #28
ricovandevin commentedAs far as I can see the patch is not yet in core for version 7.x-1.1. Patch #24 was not applying correctly to this version. Attached a reroll of the patch against 7.x-1.1.
Comment #30
schifazl commentedAny news? Actually I'm sticking with user_reference, but I prefer to keep as least modules as possible
Comment #31
fenstratThe re-roll of #23 in #28 didn't include the test hunk. So here's #23 re-rolled against 7.x-1.x-dev (also applies with some offset to 7.x-1.1).
Comment #32
fenstrat#31 is just a re-roll of #23 so RTBC.
Also, this is almost back porting the feature from D8.
Comment #33
minoroffense commentedNeed to go over some scenarios with this patch in place. Ensure we aren't introducing some kind of information disclosure security issue (e.g. generate a list of users of a given role or status). I don't think that's the case but I want to open up a discussion and see if there's someway we might be causing a problem.
Comment #34
joe_carvajalI confirm patch at #31 is working as a charm for me.
Thank @fenstrat for the reroll! and also thanks the rest of people for the effort!