Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
Attempt to override query settings on a views display (e.g. attachment) throws the following error:
TypeError: explode(): Argument #2 ($string) must be of type string, array given in explode() (line 329 of /.../core/modules/views/src/Plugin/views/query/Sql.php)
Steps to reproduce
- Create a view or edit an existing view
- Add a new page display
- Expand the Advanced fieldset and click Query settings: Settings
- Set the "For" dropdown to "This page (override)"
- Click "Apply (this display)"
Proposed resolution
Prevent Drupal\views\Plugin\views\query\Sql::submitOptionsForm()
from processing the values twice.
Comment | File | Size | Author |
---|---|---|---|
#11 | 3231861-11.patch | 3.46 KB | Lendude |
#11 | interdiff-3231861-8-11.txt | 790 bytes | Lendude |
#8 | 3231861-8.patch | 4.09 KB | Lendude |
#8 | interdiff-3231861-3-8.txt | 967 bytes | Lendude |
#4 | 3231861-3.patch | 3.86 KB | Lendude |
Comments
Comment #2
Lendude@marksmith thanks for reporting this, it is very easy to reproduce. But as far as I can tell it has nothing to do with SQL rewrite. You can just set the display to overridden and not change any settings, and errors will be shown.
As far as I can tell this is because of the handling of the query_tags field, which falls back to an empty array when overriding while all the logic expects a string. Here is a fix and a test for this.
Comment #3
LendudeBit of a clean up for the test which had some copy/pasta in there.
Comment #4
LendudeNow with a test only patch that works
Comment #6
Spokje- Failing test fails with the error described in the IS
- Patch comes back green
- Added code (except for the test) checks if
options['query_tags']
is an array, if so, it uses explode/implode onoptions['query_tags']
, otherwise it usesoptions['query_tags']
itself.RTBC for me.
Comment #7
GuyPaddock CreditAttribution: GuyPaddock at Inveniem commentedWhy doesn't the method simply return early if it detects that the value is an array? There's no need to run it back through filtering and setting again in the form state.
I proposed a slight variation on this for #3232745: Manually-added Query Tags in "Query Settings" are Lost when Changing Override Status of Query Options for any Display. Would be good if you could add a comment as to why it might be an array, for future devs to understand what's happening.
For reference, my changes for the duplicate issue:
https://git.drupalcode.org/project/drupal/-/merge_requests/1190/diffs
Comment #8
Lendude@GuyPaddock thanks for the review! Took your comment and fix from #3232745: Manually-added Query Tags in "Query Settings" are Lost when Changing Override Status of Query Options for any Display and modified slightly.
Comment #9
mstrelan CreditAttribution: mstrelan at PreviousNext commentedI've reviewed the patch, tested locally and updated the issue summary. Updating status to RTBC.
Comment #10
GuyPaddock CreditAttribution: GuyPaddock at Inveniem commentedThanks, @lendude!
Question, though -- why is the change to buildOptionsForm() necessary? I can't envision any reason why $this->options['query_tags'] should be something other than an array since the submit handler now guarantees it.
Comment #11
Lendude@GuyPaddock you are right, that is no longer needed, took it out and now the test stays green.
Comment #12
GuyPaddock CreditAttribution: GuyPaddock at Inveniem commentedLGTM!
Comment #13
GuyPaddock CreditAttribution: GuyPaddock at Inveniem commentedComment #14
alexpottCommitted and pushed 32eb238f75 to 9.3.x and 156c4c17e5 to 9.2.x. Thanks!