Themes

Adding more block regions to custom theme

 
 
Picture of Joseph Conradt
Adding more block regions to custom theme
 

Hi,

I'm trying to add more block regions to a custom theme of mine.  I followed this forum post https://moodle.org/mod/forum/discuss.php?d=225619 but I can't seem to get it working.  Specifically, the region_has_content for my custom block region is empty.  Here's my code:

<?php
$hasdashadmin = $PAGE->blocks->region_has_content('dash-admin', $OUTPUT);
echo(empty($hasdashadmin)); // 1
if ($hasdashadmin) { ?>
<div id="region-admin" class="block-region">
     <div class="region-content">
          <?php echo $OUTPUT->blocks_for_region('dash-admin') ?>
     </div>
</div>
<?php } ?>

 

And in my config:

'frontpage' => array(
     'file' => 'frontpage.php',
     'regions' => array('side-pre', 'side-post', 'dash-admin')
)

So, as far as I can tell I followed all the code and just changed out the name.  But nothing shows up.  Any ideas?  I'm using Moodle 2.5.

 

Thanks in advance!

 
Average of ratings: -
It's only an avatar...
Re: Adding more block regions to custom theme
Group DevelopersGroup Documentation writersGroup Particularly helpful MoodlersGroup Testers

Hi, I am so sorry to say that there is no real documentation for adding custom block regions, for the simple fact they don;t really work that well from 2.3 upwards. 

However all you need to do is copy the same notation as side-pre or side-post renaming your custom region accordingly.

So in at the top of your layout/general.php file you would have something like...

$hassideadmin = (empty($PAGE->layout_options['noblocks']) && $PAGE->blocks->region_has_content('side-admin', $OUTPUT));
$showsideadmin = ($hassideadmin && !$PAGE->blocks->region_completely_docked('side-admin', $OUTPUT));

Then wherever you want to add your new block region you need write it similar to this:

                <?php if ($hassideadmin)) { ?>
                <div id="region-admin" class="block-region">
                    <div class="region-content">
                            <?php echo $OUTPUT->blocks_for_region('side-admin'); ?>
                    </div>
                </div>
                <?php } ?>

EDIT: You must use the word 'side' as in side-admin

Hope this helps?

Mary

 
Average of ratings: -
Picture of Joseph Conradt
Re: Adding more block regions to custom theme
 

Thanks for the reply, Mary.  I will give it a try.  

What do you mean by they don't really work that well?  What I'm trying to accomplish is having only one block in a different place on the page.  It will not be docked and I'm not allowing more blocks to be added.  Do you think there will be problems?  I'm targeting 2.5.

Thanks again!

 
Average of ratings: -
It's only an avatar...
Re: Adding more block regions to custom theme
Group DevelopersGroup Documentation writersGroup Particularly helpful MoodlersGroup Testers

That should be interesting to find out. It did not work in Aardvark Postit in 2.2+, which was one of the themes that used it in 2.1 where it worked OK.  But since then there have been page layout problems. If the only region declared in your config is going to be 'side-admin' for a given page then it may work.

So as I said at the start it will be interesting to see if it :

  1. works unconditionally
  2. works without errors

Cheers

Mary

 
Average of ratings: -
Picture of Joseph Conradt
Re: Adding more block regions to custom theme
 

OK I got it working if I set the side-admin region to the default region.

'frontpage' => array(
     'file' => 'frontpage.php',
     'regions' => array('side-admin'),
     'defaultregion' => 'side-admin',
)

However, I only want the admin block to show in my side-admin region... and it's putting the navigation block in my side-admin block; obviously because its the default.  When I comment out the defaultregion option I get the same result as before and nothing shows up.  

This may be a dumb question... but do I have to put blocks in the region before it shows up?  I originally assumed that if there were no blocks to begin with, the "add block" dropdown would show up.  

Any thoughts?

 

Thank you.

Joe Conadt

 
Average of ratings: -
It's only an avatar...
Re: Adding more block regions to custom theme
Group DevelopersGroup Documentation writersGroup Particularly helpful MoodlersGroup Testers

Hi Joe, The dictates where the ADD BLOCKS sits. By default it is on the left now where previously it was on the right in Moodle 1.9 and in Moodle 2.0 until last year I think when I shifted it to the left to give more room to Administrators.

I think you can still disable the Navigation block in Admin settings, it used to be an option when it was first introduced. I am sure the option is still there.

Cheers

Mary

 
Average of ratings: -
Picture of Joseph Conradt
Re: Adding more block regions to custom theme
 

Hi Mary,

Do you know of any themes that use custom block regions off hand?  

 
Average of ratings: -
Picture of Joseph Conradt
Re: Adding more block regions to custom theme
 

OK I managed to figure out how to add additional block regions.  

1. In the config.php add the region name to the region array for the layout(s) you want the region to show up in. e.g. 'regions' => array('side-pre', 'side-post', 'side-content'),

2. Add the code to check if it exists at the top of the layout file: $hassidecontent = $PAGE->blocks->region_has_content('side-content', $OUTPUT);

3. Display the block region somewhere in the layout file (e.g. general.php).

<?php
if($hassidecontent){
echo $OUTPUT->blocks_for_region('side-content');
}
?>

4. Create string for new region.  In themename/lang/en/theme_themename.php add the line: $string['region-side-content'] = 'Content region';

5. Add a block to an existing region and click the little gear icon to edit the block.  Then under "where this block appears" change the default region to "Content region"; the same string you put in the lang file.

And then the block should appear where the custom block region is placed in the layout file.  Working in 2.5.

Hope this helps someone.

 
Average of ratings:Useful (3)
Picture of Ashutosh Bhardwaj
Re: Adding more block regions to custom theme
 

Hi Joseph, 

I was searching for something similar, except i wanted to create a custom frontpage design that can include additional regions with my custom width to accomodate existing blocks within a new width. (As moodl does not allow to change width of the block ) 

I'm using essential theme, and gonna give it a try, however there is no general layout file as such named. 

Since for now i want this on frontpage, so will be adding the code to frontpage.php and share the result. 


If you have any pointers or tips that can guide me to decide which layout file to add snippets to for different purspose, ( non logged i front page, admin, teacher, student dahsboard, course page etc ) please help me out


Thanks smile


 
Average of ratings: -
Picture of Richard Oelmann
Re: Adding more block regions to custom theme
Group Particularly helpful MoodlersGroup Testers

Ashutosh - please note: this code is in a post from 2013 and much has changed in this area since then

You would be much better looking at the code already in the Essential theme you have as that has a number of additional regions already in it which are built within the bootstrap grid framwework that the theme is using.

 
Average of ratings: -
It's only an avatar...
Re: Adding more block regions to custom theme
Group DevelopersGroup Documentation writersGroup Particularly helpful MoodlersGroup Testers

Further to what Richard has advised, you  might be better learning how to clone a theme and then customise it as you wish.

My reasons for saying this is that if you start off with the Esseniial theme, which to be truthful is quite a complex theme, especially when you are not familiar with how to create new block regions, or which layout file to use, seems pretty much a bad idea to me

You need to familiarise yourself with something simple, then, and only then, can you start to improve the layout of a theme when you are confident you know what you are doing.

Cheers

Mary

 
Average of ratings: -
Picture of Joseph Conradt
Re: Adding more block regions to custom theme
 

Hi Ashutosh,

If you're asking how to have custom layouts, you can find all that information in the theme's config.php file. There's a property $THEME->layouts which takes a nested array. You can take a look at the base theme's config.php file, because that one isn't too customized like Essential's.

For example:

$THEME->layouts = array( 
    'general' => array(
        'file' => 'general.php',
        'regions' => array('side-pre', 'side-post'),
        'defaultregion' => 'side-pre',
    ),
    'frontpage' => array(
        'file' => 'frontpage.php',
        'regions' => array('side-pre', 'side-post'),
        'defaultregion' => 'side-pre',
    ),
// [..] );
 
Average of ratings: -
Picture of Jordi Pujol-Ahulló
Re: Adding more block regions to custom theme
 

Hi all!

I am working in a migration of a Moodle site from 2.4 to 2.8.

In the new M2.8 we will be using the Theme More. And we need to get again 3 regions where to add blocks, mainly in the frontend. I have got it working with these changes:


diff --git a/theme/bootstrapbase/config.php b/theme/bootstrapbase/config.php
index d85dce9..6661ad7 100644
--- a/theme/bootstrapbase/config.php
+++ b/theme/bootstrapbase/config.php
@@ -50,13 +50,13 @@ $THEME->layouts = array(
     // Standard layout with blocks, this is recommended for most pages with general information.
     'standard' => array(
         'file' => 'columns3.php',
-        'regions' => array('side-pre', 'side-post'),
+        'regions' => array('side-pre', 'side-post', 'side-content'),
         'defaultregion' => 'side-pre',
     ),
     // Main course page.
     'course' => array(
         'file' => 'columns3.php',
-        'regions' => array('side-pre', 'side-post'),
+        'regions' => array('side-pre', 'side-post', 'side-content'),
         'defaultregion' => 'side-pre',
         'options' => array('langmenu' => true),
     ),
diff --git a/theme/clean/lang/en/theme_clean.php b/theme/clean/lang/en/theme_clean.php
index 618a3bf..4857b14 100644
--- a/theme/clean/lang/en/theme_clean.php
+++ b/theme/clean/lang/en/theme_clean.php
@@ -65,4 +65,5 @@ $string['pluginname'] = 'Clean';
 
 $string['region-side-post'] = 'Right';
 $string['region-side-pre'] = 'Left';
+$string['region-side-content'] = 'Center';
 
diff --git a/theme/clean/layout/columns3.php b/theme/clean/layout/columns3.php
index e6934da..3d046d0 100644
--- a/theme/clean/layout/columns3.php
+++ b/theme/clean/layout/columns3.php
@@ -88,6 +88,10 @@ echo $OUTPUT->doctype() ?>
     <div id="page-content" class="row-fluid">
         <div id="<?php echo $regionbsid ?>" class="span9">
             <div class="row-fluid">
+                <?php echo $OUTPUT->blocks('side-pre', 'span4 desktop-first-column'); ?>
+                <section id="region-content" class="span8 pull-right">
+                    <?php echo $OUTPUT->blocks('side-content');?>
+                </section>
                 <section id="region-main" class="span8 pull-right">
                     <?php
                     echo $OUTPUT->course_content_header();
@@ -95,7 +99,6 @@ echo $OUTPUT->doctype() ?>
                     echo $OUTPUT->course_content_footer();
                     ?>
                 </section>
-                <?php echo $OUTPUT->blocks('side-pre', 'span4 desktop-first-column'); ?>
             </div>
         </div>
         <?php echo $OUTPUT->blocks('side-post', 'span3'); ?>
diff --git a/theme/more/config.php b/theme/more/config.php
index ee98b11..fb609f1 100644
--- a/theme/more/config.php
+++ b/theme/more/config.php
@@ -41,5 +41,6 @@ $THEME->csspostprocess = 'theme_more_process_css';
 
 $THEME->blockrtlmanipulations = array(
     'side-pre' => 'side-post',
-    'side-post' => 'side-pre'
+    'side-post' => 'side-pre',
+    'side-content' => 'side-content'
 );
diff --git a/theme/more/lang/en/theme_more.php b/theme/more/lang/en/theme_more.php
index 53883f7..3c29d66 100644
--- a/theme/more/lang/en/theme_more.php
+++ b/theme/more/lang/en/theme_more.php
@@ -77,3 +77,4 @@ $string['secondarybackground_desc'] = 'The background colour of any secondary co
 $string['textcolor'] = 'Text colour';
 $string['textcolor_desc'] = 'The colour of the text.';
 
+$string['region-side-content'] = 'Center';
\ No newline at end of file
diff --git a/theme/more/lib.php b/theme/more/lib.php
index 0e8017f..c320c4f 100644
--- a/theme/more/lib.php
+++ b/theme/more/lib.php
@@ -177,3 +177,12 @@ function theme_more_set_customcss($css, $customcss) {
 
     return $css;
 }
+
+/**
+ * Called when initializing the web page. We need to add a system-wide central region
+ * for blocks.
+ * @param moodle_page $page current page.
+ */
+function theme_more_page_init(moodle_page $page) {
+    $page->blocks->add_region('side-content');
+}


Applying this patch, you have the more theme working with three regions: side-pre, side-content and side-post. To force that the current theme has these 3 regions, you need to initialize the region 'side-content' as an available region for blocks (in theme_more_page_init function).

To do so, I followed your comments and also an inspiration from https://moodle.org/mod/forum/discuss.php?d=278770.

We will use the More theme for its simplicity and because it provides all what we need, except the above changes to get blocks in the central part of the 3-column page.

Hoping this helps!

Jordi

 
Average of ratings: -
Picture of Richard Oelmann
Re: Adding more block regions to custom theme
Group Particularly helpful MoodlersGroup Testers

The one thing I would point out here to other readers (hoping you are already aware of it and have made your decisions accordingly) is that you have made your changes to 2 core themes which then are likely to be overwritten any time you upgrade your site and at least one of which is a potential parent them for many others.

Ideally clone more and make your changes to the cloned/child theme - including copying the layouts from clean rather than changing them there. All changes should be made in the one theme as you might find now that changing to another theme which uses clean as a parent (even for testing) will fail as it will not recognise the side-content you have added in the clean layout file. You may not want to use those other themes presently, but you may find your changes to both core themes reduce your options in the future.

 
Average of ratings: -
Picture of Jordi Pujol-Ahulló
Re: Adding more block regions to custom theme
 

Good point!

Thank you very much!

I'll consider about this.

Jordi

 
Average of ratings: -