1

I have site running Moodle 2.9.3+ and i was trying to customize the renderer of one existing plugin, so i found this:

How to override a renderer such that the functionality of the overridden renderer also remains available?

I added the following into the core_renderer.php of my theme

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

but when i tried to do

class topcoll_local_renderer extends format_topcoll_renderer {
  protected function section_header($section, $course, $onsectionpage, $sectionreturn = null) {
...
  }
}

it runs, but it's not loading the modified function. Do I need to do anything else? the modified function is running with no issues when I alter the code directly into the plugin, but not like this. I would greatly appreciate any possible hint.

Thanks

Community
  • 1
  • 1
Brandon
  • 135
  • 2
  • 12
  • Without being able to see all the code, it is very hard to give any advice on that - I suggest you are probably either extending the wrong class or accessing a member variable that is mistyped or something. – davosmith Feb 04 '16 at 15:02
  • in a related question... can i override a core function from Moodle? Again, i changed the core function and it is working there, but i would love to have that customized on my theme so i have better control depending on what selected theme i have. Thanks :) – Brandon Feb 04 '16 at 15:16
  • @davosmith thanks for your help! – Brandon Feb 04 '16 at 15:27
  • @theruss thanks for your help! – Brandon Feb 04 '16 at 15:28
  • You can override a core function if it is within a renderer. If it is a random function elsewhere in Moodle, then you will need to look instead for some sort of hook that has been implemented in that function (or just give up and edit the core file). – davosmith Feb 04 '16 at 16:20
  • well it's the function setnew_password_and_mail() within moodlelib.php, although i would settle for having a local variation of it. The function is not even in a class so i can't do the same trick. Thanks – Brandon Feb 04 '16 at 18:23

2 Answers2

4

You need to do two things to get a theme renderer to override a core renderer:

  1. You need to edit the theme's config.php to add the line: $THEME->rendererfactory = 'theme_overridden_renderer_factory';
  2. You need to name your renderer class 'theme_NAMEOFTHEME_format_topcoll_renderer' (and extend 'format_topcoll_renderer', as you have done).

The theme_overridden_renderer_factory works by extending the process of instantiating a renderer to look for a class that matches the name 'theme_NAMEOFTHEME_NAMEOFRENDERER' - as long as that class exists, then it should be used (otherwise the original renderer is used).

See https://docs.moodle.org/dev/Overriding_a_renderer for more details.

davosmith
  • 6,037
  • 2
  • 14
  • 23
1

Hmmm are you sure there isn't an error in the script inclusion? If you have error_reporting off in your ini settings you won't see the E_WARNING PHP might be raising. Set this to on, or use require_once() instead. See the accepted response to this SO post for the differences: Difference between require, include and require_once?

Community
  • 1
  • 1
theruss
  • 1,690
  • 1
  • 12
  • 18