Cannot override a renderer in Moodle

I have tried following the Moodle documentation on how to override a renderer, but it does not work for some reason. Here is what I have done:

  1. Created a folder called overridetest in /theme/ directory.

  2. Created a file called config.php in /theme/ directory:

<?php      $THEME->name = 'overridetest';     $THEME->parents = array('standard', 'base');          $THEME->rendererfactory = 'theme_overriden_renderer_factory';          $THEME->sheets = Array('overridetest'); 
  1. Created a renderers.php file in /theme/ directory:
<?php      class theme_overridetest_core_renderer extends core_renderer     {         public function heading($text, $level = 2, $classes = 'main', $id = null)         {             $content = html_writer::start_tag('div', array('class' => 'headingcontainer'));             $content .= html_writer::empty_tag('img', array('src' => $this->pix_url('headingpic', 'theme'), 'alt' => '', 'class' => 'headingimage'));             $content .= parent::heading($text, $level, $classes, $id);             $content .= html_writer::end_tag('div');                          return $content;         }     } 
  1. Created the directory /theme/overridetest/pix/ and included an image headingpic.png.

  2. Created a directory /theme/overridetest/style/ and included a file called overridetest.css:

.headingcontainer .headingimage {     float: left;     margin-right: 1em; }  .headingcontainer h2,  .headingcontainer h3,  .headingcontainer h4,  .headingcontainer h5,  .headingcontainer h6 {     text-align: left; } 

However, this does not seem to work. Am I doing something wrong?

Add Comment
1 Answer(s)

It probably has to do with the version of Moodle you are using. The method you showed here is working for older Moodle versions (lower than 2.8, according to the documentation).

For newer versions, you have to override renderers in your plugins. In your case, you could override it in your own theme, if you’re rolling a custom one.

Let’s say that you have created your own theme called mycustomtheme (the easiest is to create a Boost child theme https://docs.moodle.org/dev/Creating_a_theme_based_on_boost).

You could override the heading function by doing the following :

  • install your mycustomtheme plugin (by visiting the home page when logged in as an administrator)
  • register mycustomtheme as the main theme in Site Administration > Appearance > Themes > Theme selector
  • turn on the Theme Editor Mode (in Site Administration > Appearance > Themes > Theme settings)
  • create a file named core_renderer.php in /theme/mycustomtheme/classes/output/
<?php  namespace theme_mycustomtheme\output;  use html_writer;  defined('MOODLE_INTERNAL') || die;  class core_renderer extends \core_renderer {     public function heading($text, $level = 2, $classes = 'main', $id = null) {         $content = html_writer::start_tag('div', array('class' => 'headingcontainer'));         $content .= html_writer::empty_tag('img', array(                     'src' => $this->image_url('headingpic', 'theme'),                      'alt' => '', 'class' => 'headingimage'));         $content .= parent::heading($text, $level, $classes, $id);         $content .= html_writer::end_tag('div');         return $content;     } } 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.