Issue extending core_renderer to override outputrenderers methods

Issue extending core_renderer to override outputrenderers methods

by Nathan Hunt -
Number of replies: 4

There are a few Moodle renders I would like to override so that I can change the output/html for the custom theme I'm making.

There file is in the moodle root lib/outputrenderers.php

I did some searching around and came across https://docs.moodle.org/dev/Overriding_a_renderer#Our_first_renderer so I tried to follow this. I created a file in the root of my theme called renderers.php with the class:

class theme_mythemename_core_renderer extends core_renderer

And, for example on one of the methods I wanted to override, I copied the user_menu method and placed it within the class.

However, when I go to my moodle site I now get the error "Exception - Call to undefined method theme_mythemename_core_renderer::region_main_settings_menu()". The only method I have defined is the user_menu one, which is extending the core_renderer so I shouldn't imagine I have to copy every method over into my new class?

I must note I am using Moodle 3.3, and the docs in that link are a bit confusing as it states 2.9+ should use templates, but the methods I'm wanting to override are still using html_writer. So don't know if I'm going about it the right way or not?

I also have the following setting in my theme's config.php file:

$THEME->rendererfactory = 'theme_overridden_renderer_factory';

Average of ratings: Useful (1)
In reply to Nathan Hunt

Re: Issue extending core_renderer to override outputrenderers methods

by Tobias Marx -

No one?

I've got a similar problem (though in my case, the overridden functions are just ignored... sad ).

Can anyone explain how overriding an output render in Moodle 3.3 works, please?

Regards

Tobias

In reply to Nathan Hunt

Re: Issue extending core_renderer to override outputrenderers methods

by Dale Davies -

Not sure if you'd just pasted it as an example, but you would need to update the mythemename part of that line with your own theme name..

class theme_mythemename_core_renderer extends core_renderer

In reply to Dale Davies

Re: Issue extending core_renderer to override outputrenderers methods

by Tobias Marx -

I can't speak for the OP, but in my case I've used the theme's name:


<?php

defined('MOODLE_INTERNAL') || die();

class theme_boost_core_renderer extends core_renderer {

}

include_once($CFG->dirroot . "/course/renderer.php");

class theme_boost_core_course_renderer extends core_course_renderer {

   /**
    * Renders HTML to display particular course category - list of it's subcategories and courses
    *
    * Invoked from /course/index.php
    *
    * @param int|stdClass|coursecat $category
    */
   public function course_category($category) {
       global $CFG;
       require_once($CFG->libdir. '/coursecatlib.php');
       $coursecat = coursecat::get(is_object($category) ? $category->id : $category);
       $site = get_site();
       $output = '';

</snip>

In reply to Nathan Hunt

Re: Issue extending core_renderer to override outputrenderers methods

by Rafiq Muhammad -

Hi,

I have encountered the same problem and I found the following solution

Before going further, check this out
https://docs.moodle.org/dev/Boost_Navigation

Excerpt:
"These navigation elements are custom elements that only exist in the boost theme. The boost theme has it's own core renderer which is where they are defined. See theme/boost/classes/output/core_renderer.php. The functions are region_main_settings_menu() and context_header_settings_menu()."

Based on my understanding, the method region_main_settings_menu() can only be found in the boost core renderer. Hence, when trying to override the core renderer, instead of using

class theme_mytheme_core_renderer extends core_renderer 
you have to use

class theme_mytheme_core_renderer extends theme_boost\output\core_renderer 
to point exactly to the boost core renderer.

Having done that, the error is gone.

Hope it helps.

Thanks

Rafiq



Average of ratings: Useful (1)