I find a bug while working on the Drupal 8.7.10 version and reproduces the same bug in version 8.8.5.

Steps to reproduce the bug:
1. Install fresh Drupal 8.8.5.
2. Create 2 roles, for example 'student' and 'teacher'.
3. Create users, which will have one of new created roles.

4. Create view to show users.
create view

5. Add new view display: Entity Reference.
create view

6. Select 'User: Name' as Search fields in Format -> Format -> Settings.
create view

7. Add Role contextual filter and check "Allow multiple values" checkbox.
create view

I expected to see the list of users who have one of the given roles.

But it show following sql query:
create view

instead of given role id's, I saw '0' in the query.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mashot7 created an issue. See original summary.

mashot7’s picture

Issue summary: View changes
le72’s picture

I confirm the issue. It does not depend on display type. Same for block or page display. Any ideas, patches?

le72’s picture

Title: Contextual filter: allow multiple not working with user roles filter. » Views contextual filter: "allow multiple" doesn't work for user roles filter.
Priority: Normal » Major
osab’s picture

Looks like need to be fixed parameter $force_int for unpackArgumentValue().

Result of local testing attached.

Despite that, the patch is working, but maybe need to check how to fix properly $this->definition['numeric'], now it has TRUE value even for the string context filter?

osab’s picture

Status: Active » Needs review
osab’s picture

FileSize
63.06 KB

added screenshot with testing OR with AND condition in the contextual filter.

codersukanta’s picture

The issue was with the Roleid plugin definition. Previously role id is used to be a numeric value but it's not numeric anymore.

atul4drupal’s picture

The patch at #8 worked perfectly for me.
Its RTBC 1 for the patch at #8.

Let more people give it a try and see for themselves before moving issue status to RTBC.

Lendude’s picture

Version: 8.8.5 » 8.8.x-dev
Component: views.module » user.module
Status: Needs review » Needs work
Issue tags: -user roles, -views contextual filter, -entity reference view, -allow multiple values, -sql query +Needs tests

Nice find, and thanks for the work on this.

You should never (hardly ever?) have to use an alter hook inside core, since we should just be supplying the correct data to begin with.

In this case, the change should be made in \Drupal\user\UserViewsData

That currently says:

$data['user__roles']['roles_target_id']['argument'] = [
      'id' => 'user__roles_rid',
      'name table' => 'role',
      'name field' => 'name',
      'empty field name' => $this->t('No role'),
      'zero is null' => TRUE,
      'numeric' => TRUE,
    ];

So that 'numeric' seems wrong, and we should change it there.

Also, this needs an automated test.

codersukanta’s picture

Assigned: Unassigned » codersukanta

Thanks, @Lendude

I will create a new patch for this.

codersukanta’s picture

Assigned: codersukanta » Unassigned
Status: Needs work » Needs review
FileSize
491 bytes

The issue was with the Roleid plugin definition. Previously role id is used to be a numeric value but it's not numeric anymore.

Here is the updated patch.

Lendude’s picture

Status: Needs review » Needs work
+++ b/core/modules/user/src/UserViewsData.php
@@ -233,7 +233,7 @@ public function getViewsData() {
-      'numeric' => TRUE,
+      'numeric' => FALSE,

If anything, we should just leave this out and not set it too FALSE.

But looking at this some more, the better fix would probably be to not have \Drupal\user\Plugin\views\argument\RolesRid extend ManyToOne but use \Drupal\views\Plugin\views\argument\StringArgument and 'many to one' set to TRUE. But that might be too big of a change?

But don't bother rolling any of that into a patch unless we add some test coverage first, because there might be some details we are overlooking here.

This handler only has a Unit test, I feel we need a functional test for this

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.

codersukanta’s picture

Replacing ManyToOne with StringArgument is working fine except the AND(role1,role2) condition.
Passing role as (role1,role2) should work as AND statement in the query however its been treated as OR statement. Looks like the AND clause is completely missing.

The codes block from the query function in StringArgument class at line number 239 is causing the issue.

if (count($this->value) > 1) {
      $operator = 'IN';
      $argument = $this->value;
    }
    else {
      $operator = '=';
    }

Passing more than one arguments in contextual filter always replace $operator with IN statement. I am strugling to find a way to have AND clause added here any help or direction will be appreciated.

mohit_aghera’s picture

@Lendude
Adding a few functional test cases to evaluate if the patch works with all the scenarios. Patch is passing on local.

\Drupal\views\Plugin\views\argument\StringArgument and 'many to one' set to TRUE. But that might be too big of a change?

I think I can review a few things in that direction.

Besides, I think we can switch the issue to 9.2.x branch.

Status: Needs review » Needs work

The last submitted patch, 16: test-only-3132145-16.patch, failed testing. View results

Spokje’s picture

Status: Needs work » Needs review

Above test failures where caused by a broken HEAD. This was fixed in #3207086: [HEAD BROKEN] Consistent failure in MonthDatePluginTest. Ordered retest and put this issue back to RTBC per #16.

Status: Needs review » Needs work

The last submitted patch, 16: test-only-3132145-16.patch, failed testing. View results

mohit_aghera’s picture

Status: Needs work » Needs review
Spokje’s picture

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

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

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

dmgaye96’s picture

Hello I want to display like this image with the contextual filters of drupal but I can not display the title description and number of module of each course at once

le72’s picture

#16 looks great 👍🏻

Lendude’s picture

Status: Needs review » Needs work

@mohit_aghera great stuff!

The fix seems to do the trick, so disregard my comment in #13, lets go with this.

  1. +++ b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_user_role_contextual_filter.yml
    @@ -0,0 +1,219 @@
    +  entity_reference:
    +    display_plugin: entity_reference
    +    id: entity_reference
    

    Any reason to add and use the entity reference display? Or could we just use the default display?

  2. +++ b/core/modules/views/tests/src/Functional/Plugin/ContextualFiltersStringTest.php
    @@ -0,0 +1,109 @@
    + * Test contextual filters with allow multiple values setting for user roles.
    

    maybe quote 'allow multiple values'?

  3. +++ b/core/modules/views/tests/src/Functional/Plugin/ContextualFiltersStringTest.php
    @@ -0,0 +1,109 @@
    +    $this->assertSession()->pageTextContains('user1');
    +    $this->assertSession()->pageTextContains('user3');
    +    $this->assertSession()->pageTextContains('user5');
    

    In all these cases we want to check for all users. We need a pageTextNotContains for the users we expect to be hidden. The way it is setup now, it could be showing all the users in every case and the test would still pass

mohit_aghera’s picture

Thanks for the review @Lendude.
1. I think "entity_reference" display might have came through when I cloned some views while writing test case.
It is not necessary. Removed it.
Though there is a difference in a format like "Entity Reference list" won't be available in the default view. However, we can safely ignore it because title will still be displayed and we want to compare the title only, irrespective of format.

Test cases are still passing with relevant changes.

2. Fixed the code

3. Added assertions to validate that titles shouldn't exist which doesn't specify criteria.

Lendude’s picture

Status: Needs review » Reviewed & tested by the community

Looks great, thanks!

alexpott’s picture

Version: 9.3.x-dev » 9.2.x-dev
Status: Reviewed & tested by the community » Fixed

Committed and pushed eb2ed2340f to 9.3.x and 392faadb1d to 9.2.x. Thanks!

Nice to have this fixed and tested.

  • alexpott committed eb2ed23 on 9.3.x
    Issue #3132145 by mohit_aghera, codersukanta, osab, mashot7, Lendude:...

  • alexpott committed 392faad on 9.2.x
    Issue #3132145 by mohit_aghera, codersukanta, osab, mashot7, Lendude:...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.