MDL-41551 blocks: added tracking and recognition of custom block regions

This commit is contained in:
Sam Hemelryk
2014-04-06 15:17:47 +12:00
parent 225c418fd2
commit 292dcf047f
4 changed files with 105 additions and 18 deletions
+39 -7
View File
@@ -394,12 +394,30 @@ class block_manager {
/**
* Add a region to a page
*
* @param string $region add a named region where blocks may appear on the
* current page. This is an internal name, like 'side-pre', not a string to
* display in the UI.
* @param string $region add a named region where blocks may appear on the current page.
* This is an internal name, like 'side-pre', not a string to display in the UI.
* @param bool $custom True if this is a custom block region, being added by the page rather than the theme layout.
*/
public function add_region($region) {
public function add_region($region, $custom = true) {
global $SESSION;
$this->check_not_yet_loaded();
if ($custom) {
if (array_key_exists($region, $this->regions)) {
// This here is EXACTLY why we should not be adding block regions into a page. It should
// ALWAYS be done in a theme layout.
debugging('A custom region conflicts with a block region in the theme.', DEBUG_DEVELOPER);
}
// We need to register this custom region against the page type being used.
// This allows us to check, when performing block actions, that unrecognised regions can be worked with.
$type = $this->page->pagetype;
if (!isset($SESSION->custom_block_regions)) {
$SESSION->custom_block_regions = array($type => array($region));
} else if (!isset($SESSION->custom_block_regions[$type])) {
$SESSION->custom_block_regions[$type] = array($region);
} else if (!in_array($region, $SESSION->custom_block_regions[$type])) {
$SESSION->custom_block_regions[$type][] = $region;
}
}
$this->regions[$region] = 1;
}
@@ -409,9 +427,23 @@ class block_manager {
*
* @param array $regions this utility method calls add_region for each array element.
*/
public function add_regions($regions) {
public function add_regions($regions, $custom = true) {
foreach ($regions as $region) {
$this->add_region($region);
$this->add_region($region, $custom);
}
}
/**
* Finds custom block regions associated with a page type and registers them with this block manager.
*
* @param string $pagetype
*/
public function add_custom_regions_for_pagetype($pagetype) {
global $SESSION;
if (isset($SESSION->custom_block_regions[$pagetype])) {
foreach ($SESSION->custom_block_regions[$pagetype] as $customregion) {
$this->add_region($customregion, false);
}
}
}
@@ -746,7 +778,7 @@ class block_manager {
* @param string $subpagepattern optional. Passed to @see add_block()
*/
public function add_blocks($blocks, $pagetypepattern = NULL, $subpagepattern = NULL, $showinsubcontexts=false, $weight=0) {
$this->add_regions(array_keys($blocks));
$this->add_regions(array_keys($blocks), false);
foreach ($blocks as $region => $regionblocks) {
$weight = 0;
foreach ($regionblocks as $blockname) {