From 8cb2cbbf98921a9df18e10e43475c7b54e7b3905 Mon Sep 17 00:00:00 2001 From: Mike Grant Date: Mon, 9 Feb 2015 09:41:32 +0000 Subject: [PATCH] MDL-37470 blocklib: Respect block weight and offset --- lib/blocklib.php | 30 +++++++++++++++++------------- lib/tests/blocklib_test.php | 20 ++++++++++++++++++++ 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/lib/blocklib.php b/lib/blocklib.php index 831fb1393ee..1b3b6f01fad 100644 --- a/lib/blocklib.php +++ b/lib/blocklib.php @@ -781,19 +781,23 @@ class block_manager { } /** - * Convenience method, calls add_block repeatedly for all the blocks in $blocks. + * Convenience method, calls add_block repeatedly for all the blocks in $blocks. Optionally, a starting weight + * can be used to decide the starting point that blocks are added in the region, the weight is passed to {@link add_block} + * and incremented by the position of the block in the $blocks array * * @param array $blocks array with array keys the region names, and values an array of block names. - * @param string $pagetypepattern optional. Passed to @see add_block() - * @param string $subpagepattern optional. Passed to @see add_block() + * @param string $pagetypepattern optional. Passed to {@link add_block()} + * @param string $subpagepattern optional. Passed to {@link add_block()} + * @param boolean $showinsubcontexts optional. Passed to {@link add_block()} + * @param integer $weight optional. Determines the starting point that the blocks are added in the region. */ public function add_blocks($blocks, $pagetypepattern = NULL, $subpagepattern = NULL, $showinsubcontexts=false, $weight=0) { + $initialweight = $weight; $this->add_regions(array_keys($blocks), false); foreach ($blocks as $region => $regionblocks) { - $weight = 0; - foreach ($regionblocks as $blockname) { + foreach ($regionblocks as $offset => $blockname) { + $weight = $initialweight + $offset; $this->add_block($blockname, $region, $weight, $showinsubcontexts, $pagetypepattern, $subpagepattern); - $weight += 1; } } } @@ -2193,12 +2197,12 @@ function blocks_find_block($blockid, $blocksarray) { // Functions for programatically adding default blocks to pages ================ -/** - * Parse a list of default blocks. See config-dist for a description of the format. - * - * @param string $blocksstr - * @return array - */ + /** + * Parse a list of default blocks. See config-dist for a description of the format. + * + * @param string $blocksstr Determines the starting point that the blocks are added in the region. + * @return array the parsed list of default blocks + */ function blocks_parse_default_blocks_list($blocksstr) { $blocks = array(); $bits = explode(':', $blocksstr); @@ -2209,7 +2213,7 @@ function blocks_parse_default_blocks_list($blocksstr) { } } if (!empty($bits)) { - $rightbits =trim(array_shift($bits)); + $rightbits = trim(array_shift($bits)); if ($rightbits != '') { $blocks[BLOCK_POS_RIGHT] = explode(',', $rightbits); } diff --git a/lib/tests/blocklib_test.php b/lib/tests/blocklib_test.php index 44a18a694eb..38e3790c0f6 100644 --- a/lib/tests/blocklib_test.php +++ b/lib/tests/blocklib_test.php @@ -286,6 +286,26 @@ class core_blocklib_testcase extends advanced_testcase { $this->assertContainsBlocksOfType(array($blockname, $blockname), $blocks); } + public function test_adding_blocks() { + $this->purge_blocks(); + + // Set up fixture. + $regionname = 'a-region'; + $blockname = $this->get_a_known_block_type(); + $context = context_system::instance(); + + list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname), + $context, 'page-type'); + + $blockmanager->add_blocks(array($regionname => array($blockname, $blockname)), null, null, false, 3); + $blockmanager->load_blocks(); + + $blocks = $blockmanager->get_blocks_for_region($regionname); + + $this->assertEquals('3', $blocks[0]->instance->weight); + $this->assertEquals('4', $blocks[1]->instance->weight); + } + public function test_block_not_included_in_different_context() { $this->purge_blocks(); -- 2.11.4.GIT