Skip to content

Commit

Permalink
MDL-41511 blocks: improved how custom block regions were being rendered.
Browse files Browse the repository at this point in the history
There is a new theme property 'blockrendermethod' that can be set by the
theme in its config.php and tells Moodle what method it is using to render
blocks in the layout files.
Either blocks, or blocks_for_region.
Then when adding custom block regions to a page content we ensure we use
the same method the theme is using elsewhere.

This is really a hack becuase we (I) didn't properly deprecate
blocks_for_region when I added the blocks method.
  • Loading branch information
Sam Hemelryk committed Apr 6, 2014
1 parent 1a727e1 commit 225c418
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 9 deletions.
36 changes: 35 additions & 1 deletion lib/outputlib.php
Expand Up @@ -380,6 +380,13 @@ class theme_config {
*/
public $lessvariablescallback = null;

/**
* Sets the render method that should be used for rendering custom block regions by scripts such as my/index.php
* Defaults to {@link core_renderer::blocks_for_region()}
* @var string
*/
public $blockrendermethod = null;

/**
* Load the config.php file for a particular theme, and return an instance
* of this class. (That is, this is a factory method.)
Expand Down Expand Up @@ -448,7 +455,8 @@ private function __construct($config) {
$configurable = array('parents', 'sheets', 'parents_exclude_sheets', 'plugins_exclude_sheets', 'javascripts', 'javascripts_footer',
'parents_exclude_javascripts', 'layouts', 'enable_dock', 'enablecourseajax', 'supportscssoptimisation',
'rendererfactory', 'csspostprocess', 'editor_sheets', 'rarrow', 'larrow', 'hidefromselector', 'doctype',
'yuicssmodules', 'blockrtlmanipulations', 'lessfile', 'extralesscallback', 'lessvariablescallback');
'yuicssmodules', 'blockrtlmanipulations', 'lessfile', 'extralesscallback', 'lessvariablescallback',
'blockrendermethod');

foreach ($config as $key=>$value) {
if (in_array($key, $configurable)) {
Expand Down Expand Up @@ -1899,6 +1907,32 @@ public function get_all_block_regions() {
public function get_theme_name() {
return get_string('pluginname', 'theme_'.$this->name);
}

/**
* Returns the block render method.
*
* It is set by the theme via:
* $THEME->blockrendermethod = '...';
*
* It can be one of two values, blocks or blocks_for_region.
* It should be set to the method being used by the theme layouts.
*
* @return string
*/
public function get_block_render_method() {
if ($this->blockrendermethod) {
// Return the specified block render method.
return $this->blockrendermethod;
}
// Its not explicitly set, check the parent theme configs.
foreach ($this->parent_configs as $config) {
if (isset($config->blockrendermethod)) {
return $config->blockrendermethod;
}
}
// Default it to blocks.
return 'blocks';
}
}

/**
Expand Down
22 changes: 22 additions & 0 deletions lib/outputrenderers.php
Expand Up @@ -3246,6 +3246,28 @@ public function blocks($region, $classes = array(), $tag = 'aside') {
return html_writer::tag($tag, $content, $attributes);
}

/**
* Renders a custom block region.
*
* Use this method if you want to add an additional block region to the content of the page.
* Please note this should only be used in special situations.
* We want to leave the theme is control where ever possible!
*
* This method must use the same method that the theme uses within its layout file.
* As such it asks the theme what method it is using.
* It can be one of two values, blocks or blocks_for_region (deprecated).
*
* @param string $regionname The name of the custom region to add.
* @return string HTML for the block region.
*/
public function custom_block_region($regionname) {
if ($this->page->theme->get_block_render_method() === 'blocks') {
return $this->blocks($regionname);
} else {
return $this->blocks_for_region($regionname);
}
}

/**
* Returns the CSS classes to apply to the body tag.
*
Expand Down
5 changes: 5 additions & 0 deletions lib/yui/build/moodle-core-blocks/moodle-core-blocks-debug.js
Expand Up @@ -345,6 +345,9 @@ M.core.blockdraganddrop.is_using_blocks_render_method = function() {
var goodregions = Y.all('.block-region[data-blockregion]').size();
var allregions = Y.all('.block-region').size();
this._isusingnewblocksmethod = (allregions === goodregions);
if (goodregions > 0 && allregions > 0) {
Y.log('Both core_renderer::blocks and core_renderer::blocks_for_region have been used.', 'warn', 'moodle-core_blocks');
}
}
return this._isusingnewblocksmethod;
};
Expand All @@ -359,8 +362,10 @@ M.core.blockdraganddrop.is_using_blocks_render_method = function() {
*/
M.core.blockdraganddrop.init = function(params) {
if (this.is_using_blocks_render_method()) {
Y.log('Block drag and drop initialised for the blocks method.', 'info', 'moodle-core_blocks');
new MANAGER(params);
} else {
Y.log('Block drag and drop initialised with the legacy manager (blocks_for_region used).', 'info', 'moodle-core_blocks');
new DRAGBLOCK(params);
}
};
Expand Down
4 changes: 2 additions & 2 deletions lib/yui/build/moodle-core-blocks/moodle-core-blocks-min.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions lib/yui/build/moodle-core-blocks/moodle-core-blocks.js
Expand Up @@ -345,6 +345,8 @@ M.core.blockdraganddrop.is_using_blocks_render_method = function() {
var goodregions = Y.all('.block-region[data-blockregion]').size();
var allregions = Y.all('.block-region').size();
this._isusingnewblocksmethod = (allregions === goodregions);
if (goodregions > 0 && allregions > 0) {
}
}
return this._isusingnewblocksmethod;
};
Expand Down
5 changes: 5 additions & 0 deletions lib/yui/src/blocks/js/blocks.js
Expand Up @@ -343,6 +343,9 @@ M.core.blockdraganddrop.is_using_blocks_render_method = function() {
var goodregions = Y.all('.block-region[data-blockregion]').size();
var allregions = Y.all('.block-region').size();
this._isusingnewblocksmethod = (allregions === goodregions);
if (goodregions > 0 && allregions > 0) {
Y.log('Both core_renderer::blocks and core_renderer::blocks_for_region have been used.', 'warn', 'moodle-core_blocks');
}
}
return this._isusingnewblocksmethod;
};
Expand All @@ -357,8 +360,10 @@ M.core.blockdraganddrop.is_using_blocks_render_method = function() {
*/
M.core.blockdraganddrop.init = function(params) {
if (this.is_using_blocks_render_method()) {
Y.log('Block drag and drop initialised for the blocks method.', 'info', 'moodle-core_blocks');
new MANAGER(params);
} else {
Y.log('Block drag and drop initialised with the legacy manager (blocks_for_region used).', 'info', 'moodle-core_blocks');
new DRAGBLOCK(params);
}
};
Expand Down
2 changes: 1 addition & 1 deletion my/index.php
Expand Up @@ -166,6 +166,6 @@

echo $OUTPUT->header();

echo $OUTPUT->blocks_for_region('content');
echo $OUTPUT->custom_block_region('content');

echo $OUTPUT->footer();
2 changes: 1 addition & 1 deletion my/indexsys.php
Expand Up @@ -63,6 +63,6 @@

echo $OUTPUT->header();

echo $OUTPUT->blocks_for_region('content');
echo $OUTPUT->custom_block_region('content');

echo $OUTPUT->footer();
3 changes: 3 additions & 0 deletions theme/base/config.php
Expand Up @@ -174,3 +174,6 @@
/** List of javascript files that need to included on each page */
$THEME->javascripts = array();
$THEME->javascripts_footer = array();

// Set this to the method you will use in your layout files for
$THEME->blockrendermethod = 'blocks_for_region';
5 changes: 2 additions & 3 deletions user/profile.php
Expand Up @@ -437,9 +437,8 @@

echo html_writer::end_tag('dl');
echo "</div></div>"; // Closing desriptionbox and userprofilebox.
echo '<div id="region-content" class="block-region"><div class="region-content">';
echo $OUTPUT->blocks_for_region('content');
echo '</div></div>';

echo $OUTPUT->custom_block_region('content');

// Print messaging link if allowed.
if (isloggedin() && has_capability('moodle/site:sendmessage', $context)
Expand Down
2 changes: 1 addition & 1 deletion user/profilesys.php
Expand Up @@ -56,6 +56,6 @@

echo $OUTPUT->header();

echo $OUTPUT->blocks_for_region('content');
echo $OUTPUT->custom_block_region('content');

echo $OUTPUT->footer();

0 comments on commit 225c418

Please sign in to comment.