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:

  1. Create a view of Users
  2. Add a contextual filter for User: Roles
  3. In the contextual filter configuration, enable the Allow multiple values option under More
  4. 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:

  1. Explicitly set the numeric definition property to FALSE in the RolesRid plugin definition.

I have attached an initial patch.

Comments

BeauTownsend created an issue. See original summary.

bbox1’s picture

StatusFileSize
new495 bytes

On further investigation, it looks like the numeric definition value comes from Drupal\user\UserViewsData::getViewsData().

Attached is a new patch.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
quietone’s picture

Status: Active » Closed (outdated)
Issue tags: +Bug Smash Initiative

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