Currently chosing a role under "User roles that can be referenced" has no effect.
In the custom node all users are shown in the userreference field.

I tracked down the problem to function _userreference_potential_references($field)

line 261: $field['referenceable_roles'] = array_intersect(user_roles(1), $field['referenceable_roles']);

This could not work since user_roles(1) returns an array like
[2]->"Authenticated user"
[6]->"Site Admin"

and $field['referenceable_roles'] is something like
[6]->"6"
[2]->"0"
[4]->"0"

so array_intersect returns the values of user_roles(1) thus deleting $field['referenceable_roles'].

I'm not sure what this line of code is good for anyway, but it seems that the autor isn't quite sure too:
// filter invalid values that seems to get through sometimes ??

:-)

Well, anyway here is a patch, so that the intended functionality remains, since I like the idea of checking a user provided input against some rules:

Substitute line 261 and line 262:

$field['referenceable_roles'] = array_intersect(user_roles(1), $field['referenceable_roles']);
$roles = array_keys(array_filter($field['referenceable_roles']));

by

$roles = array_keys(array_intersect_key(array_filter($field['referenceable_roles']),user_roles(1)));

Explanation:
array_filter($field['referenceable_roles'])
array_filter() will remove all the entries of input that are equal to FALSE -> all unchecked roles (they have the value "0") will be removed, which makes much more sense to do before an array_intersect_key() than afterwards.

array_intersect_key(array_filter($field['referenceable_roles']),user_roles(1))
array_intersect_key() returns an array containing all the values of array_filter($field['referenceable_roles']) which have matching keys in user_roles(1) -> only values which correspond to a checked and existing role remain in the array

array_keys()
This rebuilds the array, so that the former keys become values, which we need in the SQL statement later.

--------
By the way, wouldn't it be better if
line 259: if (isset($field['referenceable_roles'])) {

is something like
if (is_array($field['referenceable_roles'])) {

because we use array functions on $field['referenceable_roles'] ?

Please review my solution, since it took me quite some time to figure out what was going on and currently I only see arrays, arrays, arrays. Perhaps I missed something important.

Comments

yched’s picture

Status: Needs review » Fixed

I committed a slightly different fix.
Thanks for reporting this, and taking the time to identify.

You're probably right about is_array, BTW

Anonymous’s picture

Status: Fixed » Closed (fixed)