Problem/Motivation
When creating exposed grouped filters in a view, if a group is named and using autocomplete widget to add group items (can be taxonomy terms or users), the form throws the error on save:
The value is required if label for this item is defined.
Here is the screenshot of the error:

The problem behind this is that array of arrays is not recognized here:
$min_values = $operators[$group['operator']]['values'];
$actual_values = count(array_filter($group['value'], 'static::arrayFilterZero'));
In case autocomplete, it has the following data format:
[
0 => [
' target_id' => 1
] ,
]
but the code above expects it to be:
[
1 => 1
]
so it doesn't pass the filtering in static::arrayFilterZero
Affected plugins:
- \Drupal\user\Plugin\views\filter\Name (#2920039: Views' User Name exposed group filter validation)
- \Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTid (this issue)
Steps to reproduce
- Install Drupal with "Standard" profile
- Open content view (/admin/structure/views/view/content)
- Add an exposed grouped filter by "Tags" (Taxonomy). Make sure the group item is using autocomplete widget
- Add at least one item to the group configuration
- Submit
Proposed resolution
Convert values into array with ids, which is expected by base filter plugin.
Remaining tasks
1) Wait for #1810148: Grouped exposed taxonomy term filters do not work because the group key is added to the query and not the taxonomy ID;
2) Review/commit;
| Comment | File | Size | Author |
|---|---|---|---|
| #65 | 2576927-65.patch | 8.54 KB | rubens.arjr |
Issue fork drupal-2576927
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
tkoleary commentedComment #3
tkoleary commentedComment #4
tkoleary commentedComment #5
tkoleary commentedComment #6
tkoleary commentedComment #7
tkoleary commentedComment #8
mikeker commentedI thin the underlying problem is that the autocomplete widget is completely broken in this case. If you switch to the dropdown widget, the grouped filter works correctly. As such I don't think this qualifies as Major (isolated impact, has a workaround).
Agreed! And it's missing "the" before "title." Attached patch fixes that, but does nothing about the underlying problem. I'll look into that later...
Comment #12
mikeker commentedgrr...
Comment #14
lendude@mikeker I'm looking into this, but can I suggest that we move your patch to a new issue? I think it's a good documentation fix and RTBC in itself. Fixing the TaxonomyIndexTid filter handler for grouping is going to take much more work to fix (it's a mess).
Anyway, this needs more work.
Comment #15
lendudeFirst stab at a fix partial. I didn't include the patch in #8 (see #14).
Manual testing lets me save the value and it rebuilds the form like it should. The filter looks good too, but it doesn't actually filter anything when you select a value. So that needs work. And tests.
Comment #16
mikeker commented#14: @Lendude, makes sense -- I've filed a followup in #2633678: Improve grouped filter form and fix validation problems.
Agreed, TaxonomyIndexTid is going to be a bit of work... Thanks for taking that on and good luck!
Comment #17
lendudeNow with working filter. Still needs tests.
Comment #18
lendudeNow with tests. When this is fixed though you run into #2369119: Fatal error when trying to save a View with grouped filters using other than string values. So you can't actually save the View until that is fixed.
Because of that I now only test the output in the preview because that works fine.
Interdiff is the test only patch.
Comment #22
mikeker commentedRerolled #18.
Comment #25
lendude@mikeker thanks for the reroll! forgot to remove a couple of merge tags.
Comment #28
dagmarNeeds a re-roll
Comment #29
lendudearray() => [] reroll, nothing else.
edit: not sure why that got uploaded twice, same patch, ignore one.
Comment #30
dagmarThis !empty should not be neccesary
I never saw this pattern in views before. I know this probably will work without side effects, but usually what we do is call the parent method at the beginning and then do the other modifications.
Hm. Is this the only way we have to check that a field is using autocomplete?
Double parenthesis here
Comment #31
kbasarab commentedUpdated this for 8.4.x and 8.3.x.
Comment #32
kbasarab commentedRerolls for 8.3.5 support.
Comment #33
mikeker commentedLet's see what the testbots have to say.
Comment #35
ericshell commented#32 has worked for me.
Comment #37
golddragon007 commented#31's 8.4.x doesn't work for me, I get this error when I tried to filter counted relationship data.
Before patch:
InvalidArgumentException: The configuration property display.page_search_ideas.display_options.filters.title.value.value doesn't exist. in Drupal\Core\Config\Schema\ArrayElement->get() (line 76 of D:\phptest\theideaproject\web\core\lib\Drupal\Core\Config\Schema\ArrayElement.php).
After patch:
InvalidArgumentException: The configuration property display.page_search_ideas.display_options.filters.title.value.min doesn't exist. in Drupal\Core\Config\Schema\ArrayElement->get() (line 76 of D:\phptest\theideaproject\web\core\lib\Drupal\Core\Config\Schema\ArrayElement.php).
Comment #45
rfmarcelino commentedCore 9.1.8 has this applied. I would recommend changing the status to Fixed.
Comment #46
lendude@rfmarcelino nope, this is still broken, new Test-only patch to show this still breaks
reroll and update to remove deprecated methods.
Comment #47
lendudeFixed CS
Comment #49
matroskeenI reproduced the same issue when was investigating another one: #1810148: Grouped exposed taxonomy term filters do not work because the group key is added to the query and not the taxonomy ID.
It looks like it's applicable to every filter, which is using autocomplete widget for group items. For instance, I have the same issue when trying to add a filter by Author, which is using
\Drupal\user\Plugin\views\filter\Nameclass.It makes me think that the fix itself should be either in another place, or we should also take care of other plugins in addition to
\Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTid.I'm just updating the issue summary, because and I don't have any code suggestions for now.
Comment #50
matroskeenI also meant to change the status :)
@lendude, please let me know if you'd like to continue here. Otherwise, we can swap and I'll try to come up with some patch.
Comment #52
matroskeenAfter further investigation, I agree with the approach taken by @lendude in previous patches - value normalization should happen in
valueValidatemethod.I applied the same changes to
\Drupal\user\Plugin\views\filter\Nameclass, so we should probably need a test coverage for this plugin as well.I also had to revert some changes in
\Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTidthat are already covered by #1810148: Grouped exposed taxonomy term filters do not work because the group key is added to the query and not the taxonomy ID. Unfortunately, tests here won't pass until we land #1810148: Grouped exposed taxonomy term filters do not work because the group key is added to the query and not the taxonomy ID.I also created another issue that I faced along the way: #3250352: Username views filter should not process default value twice .
Next steps:
1) Add similar test coverage for\Drupal\user\Plugin\views\filter\Nameplugin;2) Transfer issue credits from #2920039: Views' User Name exposed group filter validation and close it as a duplicate;3) Resume when #1810148: Grouped exposed taxonomy term filters do not work because the group key is added to the query and not the taxonomy ID is in;
Comment #53
matroskeenRemoving references to
\Drupal\user\Plugin\views\filter\Nameplugin that will be fixed in #2920039: Views' User Name exposed group filter validation.We'll resume here when #1810148: Grouped exposed taxonomy term filters do not work because the group key is added to the query and not the taxonomy ID is done.
Comment #54
matroskeenComment #55
matroskeenIt looks like a test failure is a random one.
Comment #57
matroskeenIt was a bit hard to rebase the MR after #1810148: Grouped exposed taxonomy term filters do not work because the group key is added to the query and not the taxonomy ID and #3248295: Taxonomy tests should not rely on Classy. I decided to start from scratch and create a patch.
Comment #59
matroskeenAdded one more condition to handle the issue reported in #3280477: Name and TaxonomyIndexTid filter plugins have incorrect default values in grouped filters.
Comment #60
lendudeReally nitty nitpicks only, probably only removing stuff I added myself in the first place :D
Might be nice to point to what is expecting this
Not sure we need this comment? Seems pretty obvious what this does :)
We can do === here I think?
Hmmmm borderline unrelated I think, but lets keep it :) No action required.
Comment #61
matroskeenDone! Following the example on #2920039: Views' User Name exposed group filter validation I also changed the usage of static methods to term storage methods.
Technically, this is out of scope, but why don't we change it here? :)
Comment #62
lendudeThis seems to be longer than 80 chars but the bot doesn't seem to be tripping over this, so ¯\_(ツ)_/¯
Comment #63
larowlanDown to more minor nits here
$query->execute can return NULL, which would cause an error for ::loadMultiple
But I see that's an existing coddepath so perhaps follow-up?
let's return early inside this if and avoid the else while we're touching this code, it makes it much easier to read as the cyclic complexity is reduced
We can write this as
$tids = array_column($item['value'], 'target_id');same here re array_column
Comment #64
matroskeen1) I'm not sure about this. The interface declares the following:
I also found few more cases matching this pattern:
loadMultiple($query->execute()).Can it really return NULL in some cases?
2) Done
3-4) Good call on using
array_columnI also made some minor rearrangements around the lines I was already modifying, hopefully I didn't go too far :)
Comment #65
rubens.arjr commentedIn 9.4 there was a bug:
TypeError: Illegal offset type in Drupal\taxonomy\Plugin\views\filter\TaxonomyIndexTid->validateExposed() (line 364 of /var/www/docroot/core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php)I'm sending a new patch fixing this bug.
Comment #66
matroskeenCan we add/edit the test to see the bug and make sure it was caught?
Comment #67
matroskeen@rubens.arjr, can you add some steps to reproduce the issue mentioned in your comment?
As you might see, I queued a test of my previous patch for Drupal 9.4.x and it's green. Therefore, your issue probably requires some additional steps. It might be worth moving this into a follow-up, but we need to see what's the root cause.
Comment #68
smustgrave commentedThis issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.
Tested #64 as #65 steps were not provided.
Can confirm the issue described in the IS and the steps were perfect.
Applied patch
Now am able to save the group filter without issue.
Searching for loadMultiple($query->execute()); only none test file I saw was TermStorage.php
Just to be extra safe could we do something like