Problem/Motivation

BlockPageVariant::build() marks the region as #sorted after building its blocks, even when hook_block_build_alter() has changed their weights. The blocks then render in their original order.

Steps to reproduce

  1. On a Drupal Standard install, add this hook to an enabled custom module:
    function example_block_build_alter(array &$build, BlockPluginInterface $block): void {
      if ($block->getPluginId() === 'user_login_block') {
        $build['#weight'] = -10;
      }
    }
  2. Rebuild caches and visit /system/401 while logged out.
Expected:
The login form appears above the message.
Actual:
The login form stays below "Log in to access this page."

before

Proposed resolution

Make sure the region is only marked as #sorted if the final block weights are still in order.
Add a regression test.

Remaining tasks

Review.

User interface changes

Blocks now render in the order of their weights as changed by hook_block_build_alter:

after

Introduced terminology

None.

API changes

None.

Data model changes

None.

Release notes snippet

BlockPageVariant now respects changes to #weight made by hook_block_build_alter() when ordering blocks within a region.

Issue fork drupal-3639328

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

pancho created an issue. See original summary.

pancho’s picture

Status: Active » Needs review