From 0f73f3ab4288a5867d0fbd3f4c452c792ef15c46 Mon Sep 17 00:00:00 2001 From: Damyon Wiese Date: Wed, 12 Oct 2016 16:50:46 +0800 Subject: [PATCH] MDL-55074 blocks: navigation and settings blocks optional This is a theme config setting. They are forced on for behat always. --- lib/behat/classes/util.php | 3 +++ lib/blocklib.php | 43 ++++++++++++++++++++++++++++++++------ lib/navigationlib.php | 13 ++++++------ lib/outputlib.php | 8 ++++++- theme/boost/config.php | 1 + theme/upgrade.txt | 1 + 6 files changed, 56 insertions(+), 13 deletions(-) diff --git a/lib/behat/classes/util.php b/lib/behat/classes/util.php index 9bb56b8ec19..5cfb8fd177e 100644 --- a/lib/behat/classes/util.php +++ b/lib/behat/classes/util.php @@ -108,6 +108,9 @@ class behat_util extends testing_util { set_config('debug', DEBUG_DEVELOPER); set_config('debugdisplay', 1); + // Force the navigation and settings blocks, even if the theme has made them optional. + set_config('undeletableblocktypes', 'navigation,settings'); + // Disable some settings that are not wanted on test sites. set_config('noemailever', 1); diff --git a/lib/blocklib.php b/lib/blocklib.php index 2f745512651..97e98b7cc0b 100644 --- a/lib/blocklib.php +++ b/lib/blocklib.php @@ -363,14 +363,20 @@ class block_manager { * @return array names of block types that cannot be added or deleted. E.g. array('navigation','settings'). */ public static function get_undeletable_block_types() { - global $CFG; + global $CFG, $PAGE; + $undeletableblocks = false; + if (isset($CFG->undeletableblocktypes)) { + $undeletableblocks = $CFG->undeletableblocktypes; + } else if (isset($PAGE->theme->undeletableblocktypes)) { + $undeletableblocks = $PAGE->theme->undeletableblocktypes; + } - if (!isset($CFG->undeletableblocktypes) || (!is_array($CFG->undeletableblocktypes) && !is_string($CFG->undeletableblocktypes))) { + if ($undeletableblocks === false) { return array('navigation','settings'); - } else if (is_string($CFG->undeletableblocktypes)) { - return explode(',', $CFG->undeletableblocktypes); + } else if (is_string($undeletableblocks)) { + return explode(',', $undeletableblocks); } else { - return $CFG->undeletableblocktypes; + return $undeletableblocks; } } @@ -711,6 +717,10 @@ class block_manager { } public function add_block_at_end_of_default_region($blockname) { + if (empty($this->birecordsbyregion)) { + // No blocks or block regions exist yet. + return; + } $defaulregion = $this->get_default_region(); $lastcurrentblock = end($this->birecordsbyregion[$defaulregion]); @@ -947,9 +957,30 @@ class block_manager { * method, before any output is done. */ public function create_all_block_instances() { + global $PAGE; + + // If there are any un-removable blocks that were not created - force them. + $undeletable = $this->get_undeletable_block_types(); + foreach ($undeletable as $forced) { + if (empty($forced)) { + continue; + } + $found = false; + foreach ($this->get_regions() as $region) { + foreach($this->birecordsbyregion[$region] as $instance) { + if ($instance->blockname == $forced) { + $found = true; + } + } + } + if (!$found) { + $this->add_block_at_end_of_default_region($forced); + } + } foreach ($this->get_regions() as $region) { $this->ensure_instances_exist($region); } + } /** @@ -2272,7 +2303,7 @@ function blocks_add_default_system_blocks() { $page = new moodle_page(); $page->set_context(context_system::instance()); - $page->blocks->add_blocks(array(BLOCK_POS_LEFT => array('navigation', 'settings')), '*', null, true); + $page->blocks->add_blocks(array(BLOCK_POS_LEFT => block_manager::get_undeletable_block_types()), '*', null, true); $page->blocks->add_blocks(array(BLOCK_POS_LEFT => array('admin_bookmarks')), 'admin-*', null, null, 2); if ($defaultmypage = $DB->get_record('my_pages', array('userid' => null, 'name' => '__default', 'private' => 1))) { diff --git a/lib/navigationlib.php b/lib/navigationlib.php index c12cf634f5d..bcdce0cdf2b 100644 --- a/lib/navigationlib.php +++ b/lib/navigationlib.php @@ -3700,25 +3700,26 @@ class flat_navigation extends navigation_node_collection { $course = $PAGE->course; - $PAGE->navigation->build_flat_navigation_list($this); + $this->page->navigation->initialise(); + $this->page->navigation->build_flat_navigation_list($this); // First walk the nav tree looking for "flat_navigation" nodes. if ($course->id > 1) { // It's a real course. - // 'dh' is an unused param used to give this node a different url to the default. - // This is so we don't have 2 nodes in the flat have with the same url (both would be highlighted). - // 'dh' means "don't highlight". - $url = new moodle_url('/course/view.php', array('id' => $course->id, 'dh' => 1)); + $url = new moodle_url('/course/view.php', array('id' => $course->id)); $flat = new flat_navigation_node(navigation_node::create($course->shortname, $url), 0); $flat->key = 'coursehome'; $flat->set_showdivider(true); $this->add($flat); $coursenode = $PAGE->navigation->find_active_node(); + while ($coursenode->type != navigation_node::TYPE_COURSE) { + $coursenode = $coursenode->parent; + } if ($coursenode) { foreach ($coursenode->children as $child) { if ($child->action) { - $flat = new flat_navigation_node($child, 1); + $flat = new flat_navigation_node($child, 0); $this->add($flat); } } diff --git a/lib/outputlib.php b/lib/outputlib.php index 726e99ff0ab..4f496f001da 100644 --- a/lib/outputlib.php +++ b/lib/outputlib.php @@ -344,6 +344,12 @@ class theme_config { */ public $doctype = 'html5'; + /** + * @var string undeletableblocktypes If set to a string, will list the block types that cannot be deleted. Defaults to + * navigation and settings. + */ + public $undeletableblocktypes = false; + //==Following properties are not configurable from theme config.php== /** @@ -524,7 +530,7 @@ class theme_config { $configurable = array( 'parents', 'sheets', 'parents_exclude_sheets', 'plugins_exclude_sheets', 'javascripts', 'javascripts_footer', 'parents_exclude_javascripts', - 'layouts', 'enable_dock', 'enablecourseajax', + 'layouts', 'enable_dock', 'enablecourseajax', 'undeletableblocktypes', 'rendererfactory', 'csspostprocess', 'editor_sheets', 'rarrow', 'larrow', 'uarrow', 'darrow', 'hidefromselector', 'doctype', 'yuicssmodules', 'blockrtlmanipulations', 'lessfile', 'extralesscallback', 'lessvariablescallback', 'blockrendermethod', diff --git a/theme/boost/config.php b/theme/boost/config.php index 65f04bf32fe..44c39f053b7 100644 --- a/theme/boost/config.php +++ b/theme/boost/config.php @@ -149,3 +149,4 @@ $THEME->extrascsscallback = 'theme_boost_get_extra_scss'; $THEME->prescsscallback = 'theme_boost_get_pre_scss'; $THEME->yuicssmodules = array(); $THEME->rendererfactory = 'theme_overridden_renderer_factory'; +$THEME->undeletableblocktypes = ''; diff --git a/theme/upgrade.txt b/theme/upgrade.txt index e8848cbb391..3a55c42cad4 100644 --- a/theme/upgrade.txt +++ b/theme/upgrade.txt @@ -3,6 +3,7 @@ information provided here is intended especially for theme designer. === 3.2 === +* A new theme config 'undeletableblocktypes' allows a theme to define which blocks are deletable. * A new core setting now enables admins to upload the logos of their site. Using the following methods, themers can instantly support branding logos without the need to implement specific theme settings: