Themes

 
 
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 (1)