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..

Comments

alan d.’s picture

Version: 7.x-1.0-beta2 » 7.x-1.x-dev
Category: support » feature

Currently 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.

alan d.’s picture

Title: Limit entities shown.. » User bundle filers for Roles and Status
amitaibu’s picture

Category: feature » support
Status: Active » Fixed

I'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.

alan d.’s picture

Category: support » feature
Status: Fixed » Closed (won't fix)

I 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.

amitaibu’s picture

Status: Closed (won't fix) » Active

Damien, what do you say?

amitaibu’s picture

> 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

alan d.’s picture

Excellent. I am falling behind on my current work, but if I get time to do this correctly, I'll post my work.

modstore’s picture

I would find this feature useful as well.

alan d.’s picture

Status: Active » Needs review
StatusFileSize
new3.95 KB

Here 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 :)

Status: Needs review » Needs work

The last submitted patch, entityreference-user-role-status-filters-1354482-9.patch, failed testing.

jaarong’s picture

I'm kind of surprised you can't filter the list of options on a user reference, subscribing to this issue.

agileadam’s picture

The 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.

roundbox’s picture

I 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!

murz’s picture

Patch from #9 applies and works for me good, will be good to see it in core!

bachbach’s picture

Hi,
#9 works for me, thanks Alan D !

mallezie’s picture

Status: Needs work » Reviewed & tested by the community

I also can confirm the patch is working. Applies cleanly, though mentions an offset.
But seems ready to commit to dev-version

damien tournoud’s picture

Status: Reviewed & tested by the community » Needs work

Ok, 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:

+      // No native JOIN support, so tag and record.
+      $db_select = db_select('users', 'u')->fields('u', array('uid'));
+      $db_select->join('users_roles', 'users_roles', 'u.uid = users_roles.uid');
+      $db_select->condition('users_roles.rid', array_keys($referenceable_roles), 'IN');
+      $uids = $db_select->execute()->fetchCol();
+      // No matches! Set to an impossible uid.
+      if (empty($uids)) {
+        $query->propertyCondition('uid', -1);
+      }
+      else {
+        $query->propertyCondition('uid', $uids, 'IN');
+      }

... 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!

awm’s picture

The patch works for me too but Is the any other way we can do it without this patch? In a custom module or so?

amitaibu’s picture

Status: Needs work » Needs review
StatusFileSize
new4.34 KB

Moved condition for user-roles into entityFieldQueryAlter()

Lets also add tests...

Status: Needs review » Needs work

The last submitted patch, 1354482-er-user-roles-19.patch, failed testing.

jorgensean’s picture

Applied 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.

dmadruga’s picture

Applied patch (manually) to "7.x-1.0+4-dev" and it worked beautifully.

Paul B’s picture

Status: Needs work » Needs review
StatusFileSize
new5.73 KB

This version should pass the tests I believe.

univate’s picture

23: 1354482-er-user-roles-23.patch queued for re-testing.

mmakela’s picture

Any estimation when this will be in core (thinking should I patch now or just wait..)?

alan d.’s picture

Issue summary: View changes

Posted by wipeout_dude on November 28, 2011 at 7:40pm

Think that answers your question ;)

mmakela’s picture

Oh, 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).

ricovandevin’s picture

StatusFileSize
new4 KB

As 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.

Status: Needs review » Needs work

The last submitted patch, 28: 1354482-er-user-roles-28.patch, failed testing.

schifazl’s picture

Any news? Actually I'm sticking with user_reference, but I prefer to keep as least modules as possible

fenstrat’s picture

Status: Needs work » Needs review
StatusFileSize
new5.73 KB

The 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).

fenstrat’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

#31 is just a re-roll of #23 so RTBC.

Also, this is almost back porting the feature from D8.

minoroffense’s picture

Need 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.

joe_carvajal’s picture

I 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!