The issue I am running into involves the following:
I have a taxonomy vocabulary with several entries. A node can be tagged with any one of these taxonomy terms. In the view I add a filter for "Content: Has taxonomy term" and choose the vocabulary. Now I expose the view to the user and make it into a Group Filter using dropdown.
I make three different options (ex: Patients, Doctors, Nurses) all with several different taxonomy terms selected. So the Doctor option would have "Primary Care Doctor" and "Specialty Doctor" and so on. Likewise there would be different taxonomy terms such as "Critical Patient" or "Permenant Patient" for the Patient option in the dropdown.
The SQL query outputs something like the following:
SELECT node.nid AS nid, node.title AS node_title, 'node' AS field_data_field_people_node_entity_type
FROM
{node} node
INNER JOIN {taxonomy_index} taxonomy_index ON node.nid = taxonomy_index.nid
WHERE (( (node.status = '1') AND (node.type IN ('profile_page')) AND (taxonomy_index.tid = '1') ))
What is happening is it is outputting the index of the select tag. If the user were to select the second option in the dropdown (say Doctors) than the taxonomy_index = '2' and if the user selects the third option it is '3'. Instead it should be querying for each possible taxonomy term using the taxonomy terms specified in the Group Filter multiselect.
I haven't gone through all of the View code yet, but I will update this post as I figure out where it is going wrong.
Comments
Comment #1
seanmrafferty CreditAttribution: seanmrafferty commentedThe problem appears to be located in the function called add_filter in views/includes/handlers.inc. The $value for the where clause of the filter comes from $this->handler->value. But if you are using Grouped Filters (and in this case they are taxonomy terms), this value is simply the generic index (i.e. the key) in the array e.g. 1, 2, 3, etc. If you are using Grouped Filters, the $value should be an array of tid's from the corresponding taxonomy terms.
I created a patch to check if Grouped Filters are being used. If so, I get the $value from $this->handler->group_info which contains all the tid's that need to be added to the where clause of the sql query.
I'm not sure if this the correct way to solve this problem, but it seems to work.
Comment #3
seanmrafferty CreditAttribution: seanmrafferty commentedThe same tests fail when run without my patch. Not sure how to handle that.
Comment #4
seanmrafferty CreditAttribution: seanmrafferty commentedPrevious patch was using incorrect test to determine if filters were grouped.
Comment #6
jiakomo CreditAttribution: jiakomo commentedHello,
is this and #1810148: Grouped exposed taxonomy term filters do not work because the group key is added to the query and not the taxonomy ID the same issues?
Comment #7
seanmrafferty CreditAttribution: seanmrafferty commentedThanks jiakomo. #1810148: Grouped exposed taxonomy term filters do not work because the group key is added to the query and not the taxonomy ID does appear be the same the issue. We will try that patch and see if it resolves the issue. While my patch fixes the issue, it doesn't pass automated testing yet. I'm still learning the views code so I can create a proper patch.
Comment #8
seanmrafferty CreditAttribution: seanmrafferty commentedDuplicate. #1810148: Grouped exposed taxonomy term filters do not work because the group key is added to the query and not the taxonomy ID appears to be a better solution.
Comment #9
seanmrafferty CreditAttribution: seanmrafferty commentedUnfortunately, it appears we still need this patch for our project. #1810148: Grouped exposed taxonomy term filters do not work because the group key is added to the query and not the taxonomy ID alone does not solve the problem we are having. The previous version of this patch failed when being applied to some branches of Views 7.x-3.x.
Comment #10
seanmrafferty CreditAttribution: seanmrafferty commentedComment #12
seanmrafferty CreditAttribution: seanmrafferty commentedUpdated to ensure right $value is set in all views scenarios.
Comment #13
seanmrafferty CreditAttribution: seanmrafferty commentedComment #14
seanmrafferty CreditAttribution: seanmrafferty commentedPatch 6 was dependent upon patch 5. Made patch 7 independent.
Comment #15
CvW CreditAttribution: CvW commentedI have a similar problem with a view (in views 7.x-3.7)
The view is as follows:
The generated query is the same, with and without patch:
In the third JOIN the tid still points to the grouped filter id, not to the group of tid's in the filter.
Comment #16
Kiwa CreditAttribution: Kiwa commentedTried the patch in #14 with views version 7.x-3.10 and it solved the problem for me.
Comment #17
hunchang CreditAttribution: hunchang commentedApplied the patch in 7.x-3.8, it is working, and just upgraded to 7.x-3.10, seems like working properly for me.
Comment #20
youfei.sun CreditAttribution: youfei.sun commentedThis issue persists in d8 core
8.2.x-dev
Updating issue. And hopefully the fix in d7 can be ported.
Also, a bug in 8.2.x-dev https://www.drupal.org/node/2369119 is preventing this issue to be reproduced. Apply the #145 patch on that thread before trying to reproduce this issue.
Comment #21
youfei.sun CreditAttribution: youfei.sun commentedComment #22
youfei.sun CreditAttribution: youfei.sun commentedI've made a patch based on the d7 work previously, hope this will fix the problem.
Comment #23
youfei.sun CreditAttribution: youfei.sun commentedComment #24
dagmarThis doesn't respect Drupal Coding Standars.
Also,
if (isset($options['is_grouped']) && $options['is_grouped'] ...
could be replaced byif (!empty($options['is_grouped'] && ..
Comment #25
mpdonadioNit, but the `else {` should be on a new line (Drupal coding standards doesn't use cuddled elses).
This needs a test to demonstrate the problem (posted as a test-only) and included in the patch to show that it fixes it.
Comment #26
Lendude@youfei.sun thanks for looking into this!
Since #2369119: Fatal error when trying to save a View with grouped filters using other than string values only prevents saving the View it should be possible to reproduce and test this when just using the preview and a pre-configured View.
Comment #27
youfei.sun CreditAttribution: youfei.sun commentedThanks for the code review, currently I am dead busy, looking to get a chance at December to get the essential work done so this can be fixed in core.
Comment #29
YesCT CreditAttribution: YesCT commentedFrom #20, preventing this issue from being reproduced sounds like it is blocked on #2576927: Grouped exposed taxonomy filter fails validation for autocomplete widget. So postponed.
Comment #30
Blanca.Esqueda CreditAttribution: Blanca.Esqueda as a volunteer and at Portage CyberTech commentedI'm able to reproduce the issue:
After applying the patch #2369119: Fatal error when trying to save a View with grouped filters using other then string values.
I'm using a taxonomy in my grouped filter - to limit the terms to be displayed on the dropdown -.
The dropdown displays the titles correctly but the values selected for the grouped option are not going to the query correctly.
As sample on my grouped filter interface added two options:
Option1 equal to 54 (tid of the term selected)
Option2 equal to 69 (tid of the term selected)
But when I try to filter the view using those options, the results are not the correct ones.
Checked the query itself and the condition for that filter uses the values of the dropdown (incremental pointer) instead of the real values:
Option1 --- taxonomy_target_id = 1 (instead of 54)
Option2 --- taxonomy_target_id = 2 (instead of 69)
------
Also, something else that I noticed is that it is not passing more than one value.
Added a grouped option selecting two values, and when I checked the query still only passing one value and the wrong one.
Attached three images for clarity:
1.- grouping filter interface
2.- kint displaying the filter elements.
3.- View Query with an option selected on the filter. (from my previous comment).
------
Note: Patch on #22 fix the issue.
Comment #31
rsbarbano CreditAttribution: rsbarbano commentedLogged just to say that Patch on #22 fix the issue.
I was going crazy. Thanks.
Comment #33
smazAlso confirming that the patch in #22 fixed the issue for me too.
This seemed to only be an issue for me when the group filter was single select, not multiple.
I didn't need the patch mentioned in #20, but I am adding the grouped filters to my view via a hook.
Cheers!
Comment #34
aluzzardi@smaz are you sure that worked?
For me just start to get all entries as if I don't have any filter.
Comment #37
attisanstill an issue. Fixed #22 patch by checking if value is countable.
Comment #39
interdruper CreditAttribution: interdruper at Interdruper commentedPatch #37 tried to fix this warning from patch #22 under PHP 7.2:
Warning: count(): Parameter must be an array or an object that implements Countable en Drupal\views\ManyToOneHelper->addFilter() (line 311 of /core/modules/views/src/ManyToOneHelper.php)
but #37 triggers a PHP error:
rror: Call to undefined function Drupal\views\is_countable() en Drupal\views\ManyToOneHelper->addFilter() (línea 311 de /core/modules/views/src/ManyToOneHelper.php)
Attached patch #39 to 8.8.x that properly fixes the warning from #22.
Comment #42
LendudeBack to needs work for tests
Comment #43
Krzysztof DomańskiSee also #2559961: ManyToOneHelper ignores group configuration for some cases, #1810148: Grouped exposed taxonomy term filters do not work because the group key is added to the query and not the taxonomy ID and #1766338: Incorrect filter group OR behavior, LEFT JOIN changed to INNER JOIN.
Comment #44
MatroskeenThanks everyone for the contribution here. There is another issue, which is trying to solve the same problem, but in a different way: #1810148: Grouped exposed taxonomy term filters do not work because the group key is added to the query and not the taxonomy ID.
It's older, it has more followers and most importantly, I think it follows a better approach.
I think we can mark this one as a duplicate and merge our efforts into one issue. You're welcome to follow #1810148: Grouped exposed taxonomy term filters do not work because the group key is added to the query and not the taxonomy ID!
Thanks!
Comment #45
LendudeTransferred credit, so this can be closed as a duplicate, thanks for all the work here!