Views allows magic query arguments to be used and translates them when the query is run so you can cache the original query, but keep the functionality of adding in the current user's ID for example.
The problem is it does not account for IN queries who have an array of values in their arguments entry. This causes views_query_views_alter()
to try and use an array as an array key in the following piece of code:
// Replaces substitutions in tables.
foreach ($tables as $table_name => $table_metadata) {
foreach ($table_metadata['arguments'] as $replacement_key => $value) {
if (isset($substitutions[$value])) { // Value is an array for IN-queries!
$tables[$table_name]['arguments'][$replacement_key] = $substitutions[$value];
}
}
}
Proposed fix:
// Replaces substitutions in tables.
foreach ($tables as $table_name => $table_metadata) {
foreach ($table_metadata['arguments'] as $replacement_key => $value) {
if (!is_array($value)) {
if (isset($substitutions[$value])) {
$tables[$table_name]['arguments'][$replacement_key] = $substitutions[$value];
}
}
else {
foreach ($value as $sub_key => $sub_value) {
if (isset($substitutions[$sub_value])) {
$tables[$table_name]['arguments'][$replacement_key][$sub_key] = $substitutions[$sub_value];
}
}
}
}
}
Although the patch should obviously also contain a test (or amended test) to show that this breaks on any view that is tagged with 'views' and is using an IN condition.
Comment | File | Size | Author |
---|---|---|---|
#22 | drupal-2744069-22.patch | 5.81 KB | kristiaanvandeneynde |
#22 | interdiff-18-22.txt | 543 bytes | kristiaanvandeneynde |
Comments
Comment #2
kristiaanvandeneyndeSpin-off from #2658438-34: Array valued 'extra' values generate invalid SQL in Views joins
Comment #3
achtonI hope this is not totally unwelcome, but here is a patch containing that exact change, minus tests.
I had the exact same problem with Views and Group over in #2779801: Php Warning on group/{groupid}/nodes when multiple content plugins are installed and found the same solution as here, so I am adding this patch to start with, in order to ease up local testing.
Comment #4
dawehnerComment #5
achtonThe patch in #3 does not apply cleanly, this one does.
Comment #6
dawehnerI've just seen this on production, really annoying :)
Feel free to ping me on any communication way, when you need some way of help with those tests.
Comment #7
kristiaanvandeneyndePerhaps this could be taken care of by a few lines of code in /core/modules/views/tests/src/Kernel/Plugin/JoinTest.php. That's the test covering the issue where this bug was uncovered.
Comment #8
dawehnerI would be fine with that :)
Comment #10
LendudeJust kicking the testbot into motion for #5
Comment #11
dawehnerNeeds work for writing some tests :)
Comment #13
johnreytanquinco CreditAttribution: johnreytanquinco commentedI have the same problem when viewing the groups. Patch #5 works for me. Thanks! :D
Comment #14
AdamPS CreditAttribution: AdamPS commented(Edited to fix linked issue, thanks to @kristiaanvandeneynde #15 for pointing out.)
This bug is blocking Administer Users by Role, see #2853289: Warning: Illegal offset type in isset or empty in views_query_views_alter(). Patch #5 solves.
Comment #15
kristiaanvandeneyndeI think AdamPS meant to link to #2853289: Warning: Illegal offset type in isset or empty in views_query_views_alter().
Comment #16
kristiaanvandeneyndeWell this just broke the Group module tests in a funny way: #2867710-6: Move storage related group content functionality to the GroupContent storage
Comment #17
kristiaanvandeneynde@dawehner: Searching the code base for 'views_substitutions' it seems there is zero test coverage for argument substitution right now. Where would this best be added? My previous suggestion of JoinTest is definitely wrong in this case.
Edit: Or maybe not, I mean I could easily break said test in a way that the attached patch fixes it. But that wouldn't be a pure test.
Comment #18
kristiaanvandeneyndeAttached is a patch which fixes the problem as proposed in the issue summary and also adds a test that tests the basic relationship handler when an IN-condition is added to the join.
Comment #20
kristiaanvandeneyndeAwesome, starting to get the hang of this :)
Comment #21
dawehnerGreat test coverage!
Nitpick of the month: I believe there should be a new line here :P
I think its not ideal to have the test coverage in here, but its a fair compromise for now. We also add some test coverage for other real world problems.
Comment #22
kristiaanvandeneyndeAdded the space. Also agree about the test coverage. At least it makes sure we can't have a regression in relationship handlers.
Comment #23
kristiaanvandeneyndeIssue credit should also include ohthehugemanatee, see #2871712: Warning: Illegal offset type in isset or empty in views_query_views_alter()
Comment #24
dawehnerGood point!
Comment #28
catchCommitted/pushed to 8.4.x and cherry-picked to 8.3.x. Thanks!
Comment #30
kristiaanvandeneyndeSweet, thanks! Going to see far fewer bug reports about this in the Group module issue queue now :)