MDL-37470 blocklib: Respect block weight and offset

This commit is contained in:
Mike Grant 2015-02-09 09:41:32 +00:00
parent 7357af2b0c
commit b609413154
2 changed files with 37 additions and 13 deletions

View File

@ -750,19 +750,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;
}
}
}
@ -2123,12 +2127,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);
@ -2139,7 +2143,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);
}

View File

@ -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();