From 225c418fd2d9bc7661a43c292198bab8c5dc209a Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Mon, 24 Feb 2014 10:38:17 +1300 Subject: [PATCH] MDL-41511 blocks: improved how custom block regions were being rendered. 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. --- lib/outputlib.php | 36 +++++++++++++++++- lib/outputrenderers.php | 22 +++++++++++ .../moodle-core-blocks-debug.js | Bin 32708 -> 33132 bytes .../moodle-core-blocks-min.js | Bin 11097 -> 11106 bytes .../moodle-core-blocks/moodle-core-blocks.js | Bin 32376 -> 32435 bytes lib/yui/src/blocks/js/blocks.js | 5 +++ my/index.php | 2 +- my/indexsys.php | 2 +- theme/base/config.php | 3 ++ user/profile.php | 5 +-- user/profilesys.php | 2 +- 11 files changed, 70 insertions(+), 7 deletions(-) diff --git a/lib/outputlib.php b/lib/outputlib.php index 8adf2171b65..7a1a2ec1c51 100644 --- a/lib/outputlib.php +++ b/lib/outputlib.php @@ -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.) @@ -448,7 +455,8 @@ class theme_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)) { @@ -1899,6 +1907,32 @@ class theme_config { 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'; + } } /** diff --git a/lib/outputrenderers.php b/lib/outputrenderers.php index 991c9eace31..e1795bdaf93 100644 --- a/lib/outputrenderers.php +++ b/lib/outputrenderers.php @@ -3246,6 +3246,28 @@ EOD; 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. * diff --git a/lib/yui/build/moodle-core-blocks/moodle-core-blocks-debug.js b/lib/yui/build/moodle-core-blocks/moodle-core-blocks-debug.js index e8828e2761357eb2accc57ffc7e385552254aae3..8b8ee520d919062b417b1d320c5e38ffe84e31fa 100644 GIT binary patch delta 434 zcma)&F;2rk5JeSAPC)sq*p?u&LYGJ>sAxC<4bj>=Uawg1ps^uBfFf}PE`pQ;(BwGW z0d{Q#r2v#^Mw%J@_y26aMxU=o@7sKhP_{y9n6;3x>{~35V?2g(Zf}y|={O$m>U!eX zq{#($HA)s*3VBUJGMg2S%i9*p>)$uaiY1&!*c`QbAQVI%_bus3a*AZ7#K+zQPma!d z^+7Yq=07H0HXnI0y4m@NOlVQ2=W5|QSZ_P4oDI4NNOW}|4c;2+Y946TSNtdK?PPks nJ`l08;T4hL$f)uWjq=KnARU-{sr#r|pY>n*kacr8eZKkzON5cQ delta 23 fcmaFU#B}68NuV=4In% 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; }; @@ -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); } }; diff --git a/my/index.php b/my/index.php index f2e70dce5ea..07a808a13c5 100644 --- a/my/index.php +++ b/my/index.php @@ -166,6 +166,6 @@ if ($currentpage->userid == 0) { echo $OUTPUT->header(); -echo $OUTPUT->blocks_for_region('content'); +echo $OUTPUT->custom_block_region('content'); echo $OUTPUT->footer(); diff --git a/my/indexsys.php b/my/indexsys.php index 6c49d29bab2..f21e5ac61fb 100644 --- a/my/indexsys.php +++ b/my/indexsys.php @@ -63,6 +63,6 @@ $PAGE->set_subpage($currentpage->id); echo $OUTPUT->header(); -echo $OUTPUT->blocks_for_region('content'); +echo $OUTPUT->custom_block_region('content'); echo $OUTPUT->footer(); diff --git a/theme/base/config.php b/theme/base/config.php index 804b97287bb..87463fdd047 100644 --- a/theme/base/config.php +++ b/theme/base/config.php @@ -174,3 +174,6 @@ $THEME->hidefromselector = true; /** 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'; \ No newline at end of file diff --git a/user/profile.php b/user/profile.php index 65a21378f5b..e30bc8d72bd 100644 --- a/user/profile.php +++ b/user/profile.php @@ -437,9 +437,8 @@ if (!empty($CFG->enablebadges)) { echo html_writer::end_tag('dl'); echo ""; // Closing desriptionbox and userprofilebox. -echo '
'; -echo $OUTPUT->blocks_for_region('content'); -echo '
'; + +echo $OUTPUT->custom_block_region('content'); // Print messaging link if allowed. if (isloggedin() && has_capability('moodle/site:sendmessage', $context) diff --git a/user/profilesys.php b/user/profilesys.php index c014440e5ac..a54f92f27dd 100644 --- a/user/profilesys.php +++ b/user/profilesys.php @@ -56,6 +56,6 @@ $PAGE->set_subpage($currentpage->id); echo $OUTPUT->header(); -echo $OUTPUT->blocks_for_region('content'); +echo $OUTPUT->custom_block_region('content'); echo $OUTPUT->footer(); -- 2.43.0