In Views, enabling the multiple argument option in the Roles contextual filter breaks the query. A little digging revealed that the role IDs, which are strings, are being converted to integers, resulting in no return results.
Steps to reproduce:
- Create a view of Users
- Add a contextual filter for User: Roles
- In the contextual filter configuration, enable the Allow multiple values option under More
- Test the view by providing a role id in preview.
On displaying the query, I can see that the argument is being converted to zero, yielding no matches. In my test case, I provided an argument of 'contributor', but I can see that the query is matching against the value '0' (user__roles_value_0.roles_target_id = '0').
SELECT users_field_data.uid AS uid
FROM
{users_field_data} users_field_data
INNER JOIN {user__roles} user__roles_value_0 ON users_field_data.uid = user__roles_value_0.entity_id AND user__roles_value_0.roles_target_id = '0'
WHERE (user__roles_value_0.roles_target_id = '0') AND (users_field_data.status = '1')
I discovered that, by explicitly setting the plugin's numeric setting to FALSE in Drupal\user\Plugin\views\argument\RolesRid, the filter appears to work as intended.
$this->definition['numeric'] = FALSE;
The query now returns as intended:
SELECT users_field_data.uid AS uid
FROM
{users_field_data} users_field_data
INNER JOIN {user__roles} user__roles_value_0 ON users_field_data.uid = user__roles_value_0.entity_id AND user__roles_value_0.roles_target_id = 'contributor'
WHERE (user__roles_value_0.roles_target_id = 'contributor') AND (users_field_data.status = '1')
Steps to remedy:
- Explicitly set the numeric definition property to FALSE in the RolesRid plugin definition.
I have attached an initial patch.
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | user-role-contextual-filter-2.patch | 495 bytes | bbox1 |
| user-role-contextual-filter.patch | 596 bytes | bbox1 |
Comments
Comment #2
bbox1 commentedOn further investigation, it looks like the numeric definition value comes from
Drupal\user\UserViewsData::getViewsData().Attached is a new patch.
Comment #6
quietone commentedI tested on Drupal 9.4.x and was not able to reproduce the problem. I then found it was fixed in a later issue #3132145: Views contextual filter: "allow multiple" doesn't work for user roles filter., in Drupal, 9.2.
Therefore, closing as outdated. If this is incorrect reopen the issue, by setting the status to 'Active', and add a comment explaining what still needs to be done.
Thanks!