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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Chi created an issue. See original summary.

Chi’s picture

Status: Active » Needs review
FileSize
440 bytes

Quick fix.

Chi’s picture

vicheldt’s picture

Assigned: Unassigned » vicheldt

I'll do the review.

vicheldt’s picture

Status: Needs review » Reviewed & tested by the community

I tested the changes that @Chi made and it works fine, it's a small change the patch is fine.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 2: exposed_filter_block-3230681-2.patch, failed testing. View results

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

andregp’s picture

Assigned: vicheldt » andregp

I'll try to work on this issue.

andregp’s picture

Assigned: andregp » Unassigned
Status: Needs work » Needs review
FileSize
510 bytes

Here is a quick change to the code for this issue. I just added a conditional to check if the value is null.

Eugene Bocharov’s picture

Thank 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.

hmendes’s picture

Status: Needs review » Reviewed & tested by the community

The patch from @2 looks good, and as the tests now passed, changing this issue back to RTBC.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 2: exposed_filter_block-3230681-2.patch, failed testing. View results

Eugene Bocharov’s picture

Status: Needs work » Reviewed & tested by the community

There was a problem in testing due to Composer update to 2.2 https://www.drupal.org/project/drupal/issues/3255836

Getting back to RTBC.

catch’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

This could use some test coverage.

hmendes’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
FileSize
4.87 KB
5.3 KB

Adding tests

The last submitted patch, 15: 3230681-test_only.patch, failed testing. View results

joachim’s picture

Status: Needs review » Reviewed & tested by the community
alexpott’s picture

Status: Reviewed & tested by the community » Needs work

I'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

    $content = $build['#block']->getPlugin()->build();
    // Remove the block entity from the render array, to ensure that blocks
    // can be rendered without the block config entity.
    unset($build['#block']);
    if ($content !== NULL && !Element::isEmpty($content)) {

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:

$output = $this->view->display_handler->viewExposedFormBlocks() ?? [];

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

alexpott’s picture

Looking a bit more... the code would become:

    $output = $this->view->display_handler->viewExposedFormBlocks() ?? [];
    // Provide the context for block build and block view alter hooks.
    // \Drupal\views\Plugin\Block\ViewsBlock::build() adds the same context in
    // \Drupal\views\ViewExecutable::buildRenderable() using
    // \Drupal\views\Plugin\views\display\DisplayPluginBase::buildRenderable().
    if (!empty($output)) {
      $output += [
        '#view' => $this->view,
        '#display_id' => $this->displayID,
      ];
    }

So less logic in the if for the win.

andregp’s picture

Status: Needs work » Needs review
FileSize
1.05 KB
5.73 KB

Addressing #18 and #19.
Changed the code on ViewsExposedFilterBlock.php and left it without tests.

Chi’s picture

Then, why do we need empty there?

- if (!empty($output)) {
+ if (!$output) {
andregp’s picture

Sorry, I did a silly mistake on patch #20.

Answering @Chi: The code previously was

if (is_array($output) && !empty($output)) {

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.

catch’s picture

Return type hints (eventually) instead of tests to maintain sounds like a good plan here.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

jobsons’s picture

Assigned: Unassigned » jobsons

Assigning it!

jobsons’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
798 bytes

I 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.

catch’s picture

Status: Reviewed & tested by the community » Needs work

@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.

narendra.rajwar27’s picture

Status: Needs work » Needs review
FileSize
1.17 KB
728 bytes

Re-rolling and Updating patch.

jobsons’s picture

Assigned: jobsons » Unassigned

Unassigning the task

lucassc’s picture

Assigned: Unassigned » lucassc
lucassc’s picture

Assigned: lucassc » Unassigned
Status: Needs review » Reviewed & tested by the community

I 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.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Discuseed 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!

  • alexpott committed f6bd3a5 on 10.1.x
    Issue #3230681 by andregp, hmendes, narendra.rajwar27, Chi, jobsons,...

  • alexpott committed fd9ecc1 on 10.0.x
    Issue #3230681 by andregp, hmendes, narendra.rajwar27, Chi, jobsons,...

  • alexpott committed 80e342f on 9.5.x
    Issue #3230681 by andregp, hmendes, narendra.rajwar27, Chi, jobsons,...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.