Skip to content

Commit

Permalink
MDL-55074 blocks: navigation and settings blocks optional
Browse files Browse the repository at this point in the history
This is a theme config setting.
They are forced on for behat always.
  • Loading branch information
Damyon Wiese committed Oct 21, 2016
1 parent 4a3d816 commit 0f73f3a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 13 deletions.
3 changes: 3 additions & 0 deletions lib/behat/classes/util.php
Expand Up @@ -108,6 +108,9 @@ public static function install_site() {
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);

Expand Down
43 changes: 37 additions & 6 deletions lib/blocklib.php
Expand Up @@ -363,14 +363,20 @@ public function get_installed_blocks() {
* @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;
}
}

Expand Down Expand Up @@ -711,6 +717,10 @@ public function add_block($blockname, $region, $weight, $showinsubcontexts, $pag
}

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]);
Expand Down Expand Up @@ -947,9 +957,30 @@ protected function create_block_instances($birecords) {
* 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);
}

}

/**
Expand Down Expand Up @@ -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))) {
Expand Down
13 changes: 7 additions & 6 deletions lib/navigationlib.php
Expand Up @@ -3700,25 +3700,26 @@ public function initialise() {

$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);
}
}
Expand Down
8 changes: 7 additions & 1 deletion lib/outputlib.php
Expand Up @@ -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==

/**
Expand Down Expand Up @@ -524,7 +530,7 @@ private function __construct($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',
Expand Down
1 change: 1 addition & 0 deletions theme/boost/config.php
Expand Up @@ -149,3 +149,4 @@
$THEME->prescsscallback = 'theme_boost_get_pre_scss';
$THEME->yuicssmodules = array();
$THEME->rendererfactory = 'theme_overridden_renderer_factory';
$THEME->undeletableblocktypes = '';
1 change: 1 addition & 0 deletions theme/upgrade.txt
Expand Up @@ -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:
Expand Down

0 comments on commit 0f73f3a

Please sign in to comment.