Commit 0e14311c authored by alexpott's avatar alexpott

Issue #1987952 by brantwynn, oriol_e9g, Freemantus, vuzzbox: Fixed Blocks are...

Issue #1987952 by brantwynn, oriol_e9g, Freemantus, vuzzbox: Fixed Blocks are not rendered in order by weight.
parent 2c02cd82
......@@ -441,6 +441,10 @@ function block_list($region) {
$blocks[$region] = array();
}
uasort($blocks[$region], function($first, $second) {
return $first->weight === $second->weight ? 0 : ($first->weight < $second->weight ? -1 : 1);
});
return $blocks[$region];
}
......
......@@ -46,7 +46,8 @@ public function load() {
// Load only blocks for this theme, and sort them.
// @todo Move the functionality of _block_rehash() out of the listing page.
$entities = _block_rehash($this->theme);
uasort($entities, 'static::sort');
uasort($entities, array($this->entityInfo['class'], 'sort'));
return $entities;
}
......@@ -60,39 +61,6 @@ public function render($theme = NULL) {
return drupal_get_form($this);
}
/**
* Sorts active blocks by region then weight; sorts inactive blocks by name.
*/
protected function sort(Block $a, Block $b) {
static $regions;
// We need the region list to correctly order by region.
if (!isset($regions)) {
$regions = array_flip(array_keys($this->regions));
$regions[BLOCK_REGION_NONE] = count($regions);
}
// Separate enabled from disabled.
$status = $b->get('status') - $a->get('status');
if ($status) {
return $status;
}
// Sort by region (in the order defined by theme .info.yml file).
$aregion = $a->get('region');
$bregion = $b->get('region');
if ((!empty($aregion) && !empty($bregion)) && ($place = ($regions[$aregion] - $regions[$bregion]))) {
return $place;
}
// Sort by weight, unless disabled.
if ($a->get('region') != BLOCK_REGION_NONE) {
$weight = $a->get('weight') - $b->get('weight');
if ($weight) {
return $weight;
}
}
// Sort by label.
return strcmp($a->label(), $b->label());
}
/**
* Implements \Drupal\Core\Form\FormInterface::getFormID().
*/
......
......@@ -172,4 +172,24 @@ public function preSave(EntityStorageControllerInterface $storage_controller) {
$this->set('settings', $this->getPlugin()->getConfiguration());
}
/**
* Sorts active blocks by weight; sorts inactive blocks by name.
*/
public static function sort($a, $b) {
// Separate enabled from disabled.
$status = $b->get('status') - $a->get('status');
if ($status) {
return $status;
}
// Sort by weight, unless disabled.
if ($a->get('region') != BLOCK_REGION_NONE) {
$weight = $a->get('weight') - $b->get('weight');
if ($weight) {
return $weight;
}
}
// Sort by label.
return strcmp($a->label(), $b->label());
}
}
<?php
/**
* @file
* Definition of Drupal\block\Tests\BlockRenderOrderTest.
*/
namespace Drupal\block\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Tests block HTML ID validity.
*/
class BlockRenderOrderTest extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('block');
public static function getInfo() {
return array(
'name' => 'Block Render Order',
'description' => 'Test blocks are being rendered in order by weight.',
'group' => 'Block',
);
}
function setUp() {
parent::setUp();
// Create a test user.
$end_user = $this->drupalCreateUser(array(
'access content',
));
$this->drupalLogin($end_user);
}
/**
* Tests the render order of the blocks.
*/
function testBlockRenderOrder() {
// Enable test blocks and place them in the same region.
$region = 'header';
$test_blocks = array(
'stark.powered' => array(
'weight' => '-3',
'machine_name' => 'powered',
),
'stark.by' => array(
'weight' => '3',
'machine_name' => 'by',
),
'stark.drupal' => array(
'weight' => '3',
'machine_name' => 'drupal',
),
);
// Place the test blocks.
foreach ($test_blocks as $test_block) {
$this->drupalPlaceBlock('system_powered_by_block', array(
'label' => 'Test Block',
'region' => $region,
'weight' => $test_block['weight'],
'machine_name' => $test_block['machine_name'],
));
}
$this->drupalGet('');
$test_content = $this->drupalGetContent('');
$controller = $this->container->get('plugin.manager.entity')->getStorageController('block');
foreach ($controller->loadMultiple() as $return_block) {
$settings = $return_block->get('settings');
$id = $return_block->get('id');
if ($return_block_weight = $return_block->get('weight')) {
$this->assertTrue($test_blocks[$id]['weight'] == $return_block_weight, 'Block weight is set as "' . $return_block_weight . '" for ' . $id . ' block.');
$position[$id] = strpos($test_content, 'block-' . $test_blocks[$id]['machine_name']);
}
}
$this->assertTrue($position['stark.powered'] < $position['stark.by'], 'Blocks with different weight are rendered in the correct order.');
$this->assertTrue($position['stark.drupal'] < $position['stark.by'], 'Blocks with identical weight are rendered in reverse alphabetical order.');
}
}
......@@ -369,9 +369,11 @@ protected function drupalPlaceBlock($plugin_id, array $settings = array()) {
'theme' => config('system.theme')->get('default'),
'label' => $this->randomName(8),
'visibility' => array(),
'weight' => 0,
);
foreach (array('region', 'machine_name', 'theme', 'plugin', 'visibility') as $key) {
foreach (array('region', 'machine_name', 'theme', 'plugin', 'visibility', 'weight') as $key) {
$values[$key] = $settings[$key];
// Remove extra values that do not belong in the settings array.
unset($settings[$key]);
}
$values['settings'] = $settings;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment