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
BlockPluginInterface
defines build()
method that is supposed to return an array.
However, ViewsExposedFilterBlock
returns NULL when a view does not have exposed filters.
Proposed resolution
Return empty array instead of NULL in ViewsExposedFilterBlock
.
Comment | File | Size | Author |
---|---|---|---|
#29 | interrdiff_22-29.txt | 728 bytes | narendra.rajwar27 |
#29 | 3230681-29.patch | 1.17 KB | narendra.rajwar27 |
#27 | 14601738-27.patch | 798 bytes | jobsons |
Comments
Comment #2
Chi CreditAttribution: Chi commentedQuick fix.
Comment #3
Chi CreditAttribution: Chi commentedComment #4
vicheldt CreditAttribution: vicheldt at CI&T commentedI'll do the review.
Comment #5
vicheldt CreditAttribution: vicheldt at CI&T commentedI tested the changes that @Chi made and it works fine, it's a small change the patch is fine.
Comment #8
andregp CreditAttribution: andregp at CI&T commentedI'll try to work on this issue.
Comment #9
andregp CreditAttribution: andregp at CI&T commentedHere is a quick change to the code for this issue. I just added a conditional to check if the value is null.
Comment #10
Eugene Bocharov CreditAttribution: Eugene Bocharov as a volunteer commentedThank you @andregp for working at this. But the #2 does exactly the same but it's more concise. Drupal 9 shouldn't support php lower than 7.3, so we can use that short syntax
return $output ?? []
.I think the cause of the test errors for #2 was not in the patch itself, but rather some issue with the test system, like this https://www.drupal.org/project/drupal/issues/3203712 . I rerun tests and it passes all without errors now. So, I think we should go with #2.
Comment #11
hmendes CreditAttribution: hmendes at CI&T commentedThe patch from @2 looks good, and as the tests now passed, changing this issue back to RTBC.
Comment #13
Eugene Bocharov CreditAttribution: Eugene Bocharov as a volunteer commentedThere was a problem in testing due to Composer update to 2.2 https://www.drupal.org/project/drupal/issues/3255836
Getting back to RTBC.
Comment #14
catchThis could use some test coverage.
Comment #15
hmendes CreditAttribution: hmendes at CI&T commentedAdding tests
Comment #17
joachim CreditAttribution: joachim at Factorial GmbH commentedComment #18
alexpottI'm not sure this change is necessary or actually buying us anything. The test is certainly giving us more to maintain.
In \Drupal\block\BlockViewBuilder::preRender() we do
So we explicit handle block plugins that return a NULL on the build.
I've looked at quite a few blocks in core (& contrib) and they do all return arrays.
I think the we should consider making the following change:
I.e. always define $output as an array. And not add a test and wait for return typehints to be added everywhere for compiler based enforcement. Going to ping @catch because this is different from #14
Comment #19
alexpottLooking a bit more... the code would become:
So less logic in the
if
for the win.Comment #20
andregp CreditAttribution: andregp at CI&T commentedAddressing #18 and #19.
Changed the code on ViewsExposedFilterBlock.php and left it without tests.
Comment #21
Chi CreditAttribution: Chi commentedThen, why do we need
empty
there?Comment #22
andregp CreditAttribution: andregp at CI&T commentedSorry, I did a silly mistake on patch #20.
Answering @Chi: The code previously was
Checking if $output was an array and if it was not empty. As now we convert $output to [] when it's null it will be always an array (either empty or not), so we just remove the
is_array($output)
from the if statement.Comment #23
catchReturn type hints (eventually) instead of tests to maintain sounds like a good plan here.
Comment #25
jobsons CreditAttribution: jobsons at CI&T commentedHi, I'll do the review!
Comment #26
jobsons CreditAttribution: jobsons at CI&T commentedAssigning it!
Comment #27
jobsons CreditAttribution: jobsons at CI&T commentedI reviewed 3230681-22.patch, it looks nice to me, it guarantees the variable $output to be always an array. As mentioned on comment #23, I also inserted a return type hint to build() method.
Comment #28
catch@jobsons #27 is missing the change from #22, this needs one patch with all necessary changes in order to be committed. Additionally, core issues need review by someone other than the patch author or a committer before they can be marked RTBC (i.e. in general, if you wrote the patch, especially the most recent version of it, you shouldn't also RTBC it).
Not sure why #27 isn't applying, but the return type hint looks right to me otherwise.
Comment #29
narendra.rajwar27Re-rolling and Updating patch.
Comment #30
jobsons CreditAttribution: jobsons at CI&T commentedUnassigning the task
Comment #31
lucasscComment #32
lucasscI applied patch in #29 for branch 9.5.x and it worked for me.
- Changes from #22 were included, as reviewed in #28;
- @hmendes' test was replaced for type-hints, as suggested in #18 and #23;
- Tests are passing and no code standards problems were introduced with the changes;
- The proposed resolution to return empty array instead of NULL in ViewsExposedFilterBlock was performed.
Taking this into account, I think it can be mark as RTBC.
Comment #33
alexpottDiscuseed with @catch we agreed to add the return typehint in 10.x because it is a major release and allowed because it is a plugin and therefore internal. Backported to 9.5.x without the typehint.
Committed and pushed f6bd3a527a to 10.1.x and fd9ecc1746 to 10.0.x. Thanks!
Committed 80e342f and pushed to 9.5.x. Thanks!