MDL-41511 blocks: improved how custom block regions were being rendered.
authorSam Hemelryk <sam@moodle.com>
Sun, 23 Feb 2014 21:38:17 +0000 (10:38 +1300)
committerSam Hemelryk <sam@moodle.com>
Sun, 6 Apr 2014 03:17:47 +0000 (15:17 +1200)
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
lib/outputrenderers.php
lib/yui/build/moodle-core-blocks/moodle-core-blocks-debug.js
lib/yui/build/moodle-core-blocks/moodle-core-blocks-min.js
lib/yui/build/moodle-core-blocks/moodle-core-blocks.js
lib/yui/src/blocks/js/blocks.js
my/index.php
my/indexsys.php
theme/base/config.php
user/profile.php
user/profilesys.php

index 8adf217..7a1a2ec 100644 (file)
@@ -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';
+    }
 }
 
 /**
index 991c9ea..e1795bd 100644 (file)
@@ -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.
      *
index e8828e2..8b8ee52 100644 (file)
Binary files a/lib/yui/build/moodle-core-blocks/moodle-core-blocks-debug.js and b/lib/yui/build/moodle-core-blocks/moodle-core-blocks-debug.js differ
index 83c014a..f9aaf37 100644 (file)
Binary files a/lib/yui/build/moodle-core-blocks/moodle-core-blocks-min.js and b/lib/yui/build/moodle-core-blocks/moodle-core-blocks-min.js differ
index 605161a..7e66cbd 100644 (file)
Binary files a/lib/yui/build/moodle-core-blocks/moodle-core-blocks.js and b/lib/yui/build/moodle-core-blocks/moodle-core-blocks.js differ
index da6e26d..b927f1e 100644 (file)
@@ -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;
 };
@@ -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);
     }
 };
index f2e70dc..07a808a 100644 (file)
@@ -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();
index 6c49d29..f21e5ac 100644 (file)
@@ -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();
index 804b972..87463fd 100644 (file)
@@ -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
index 65a2137..e30bc8d 100644 (file)
@@ -437,9 +437,8 @@ if (!empty($CFG->enablebadges)) {
 
 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)
index c014440..a54f92f 100644 (file)
@@ -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();