Overriding renderer in granch-child theme (or course theme

Overriding renderer in granch-child theme (or course theme

by Nicolas Dalpe -
Number of replies: 8

Hi Everyone,
I am using Moodle 3.7.2
I try to override a renderer from my base theme to a grand-child (course theme). The renderer contains around 40 methods so I would like to override only the 1 method I need in it. What happen is, for the methods that are not overridden, Moodle gets them in the core renderer (/lib/outputrenderers.php) instead of my parent theme. How do I make Moodle understand that he has to use the renderer from my parent theme instead of Moodle’s core? Also, why an overridden renderer in the parent theme does not affect the course theme (grandchild)?

Ideal situation:
I would override the renderer in my parent theme so the changes would be reflected in all the grandchild themes (8 courses in total, all have their own course theme).

Good situation:
I override the renderer in each course theme but only the method I want to override. The rest of the method in the renderer are taken from the parent or base theme (not Moodle's core)



Moodle’s core :
/lib/outputrenderers.php

Base theme: Name: RemUI
\theme\remui\classes\output\core_renderer.php
namespace theme_remui\output;
class core_renderer extends remui_renderer {
protected function render_context_header(\context_header $contextheader) {
// This is the only method I want to override
}
}


Parent theme

config.php
$THEME->name = 'MyParentTheme';
$THEME->parents = ['remui'];
$THEME->rendererfactory = 'theme_overridden_renderer_factory';

\theme\MyParentTheme\classes\output\core_renderer.php namespace theme_remui\output; class core_renderer extends remui_renderer { protected function render_context_header(\context_header $contextheader) { // This is the method I want to override } }


Grand-child theme

config.php
$THEME->name = 'MyGCTheme';
$THEME->parents = ['remui', 'MyParentTheme'];
$THEME->rendererfactory = 'theme_overridden_renderer_factory';

\theme\MyGCTheme\classes\output\core_renderer.php namespace theme_remui\output; class core_renderer extends remui_renderer { protected function render_context_header(\context_header $contextheader) { // This is the method I want to override } }

I hope all this make sense. Please ask any question you may have.

Tank you in advance for your help

Average of ratings: -
In reply to Nicolas Dalpe

Re: Overriding renderer in granch-child theme (or course theme

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Why do all the themes have the same namespace? (rhetorical question).

Average of ratings: Useful (1)
In reply to Gareth J Barnard

Re: Overriding renderer in granch-child theme (or course theme

by Nicolas Dalpe -
This is the name space that came with the theme. Not sure how it influences the themes and the theme inheritance...
In reply to Nicolas Dalpe

Re: Overriding renderer in granch-child theme (or course theme

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Do you understand PHP namespaces in relation to OO and inheritance?

In reply to Gareth J Barnard

Re: Overriding renderer in granch-child theme (or course theme

by Nicolas Dalpe -
As I understand it it, it is used to "boxed" your class name in a name space so your class naming cannot conflict with other classes. You can use the namespace path to instantiate the class or use the "use" keyword to add the name.

So what does it mean for the theme inheritance?

If my namespace is theme_remui\output; in my parent theme does that mean that PHP will use the renderer from the base theme? I tried changing the name space from the base theme to the parent theme but Moodle still ignores the renderer from my parent theme when the course theme (grand-child theme) is activated (for some reason, the base theme is rendered, not the parent).

I am using the grandchild theme as course theme. I would really like my grand-child theme to inherit the renderer modification from the parent theme otherwise I will have to duplicate the modified renderer for each course theme.
In reply to Nicolas Dalpe

Re: Overriding renderer in granch-child theme (or course theme

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
Ok, your classes don't extend the parent.
In reply to Gareth J Barnard

Re: Overriding renderer in granch-child theme (or course theme

by Nicolas Dalpe -
Yeah I know, that is what I can't figure out. The CSS of my parent theme is used but not the renderer??
In reply to Nicolas Dalpe

Re: Overriding renderer in granch-child theme (or course theme

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Without knowing the exact RemUI code and off the top of my head as don't have the theme to test:

namespace theme_remui\output;
class core_renderer extends remui_renderer {

namespace theme_MyParentTheme\output;
class core_renderer extends \theme_remui\output\core_renderer {

namespace theme_MyGCTheme\output;
class core_renderer extends \theme_MyParentTheme\output\core_renderer {

Hence 'Ok, your classes don't extend the parent.', basic OO PHP.

In reply to Gareth J Barnard

Re: Overriding renderer in granch-child theme (or course theme

by Nicolas Dalpe -
Now I understand your comment "your classes don't extend the parent". I got it working, not the I thought it would work but close enough.

My base theme didn't change

My parent theme:
namespace theme_MyParentTheme\output;
class core_renderer extends \theme_remui\output\core_renderer {
protected function render_context_header(\context_header $contextheader) {
// changes go here
}
}

My grand-child theme:
namespace theme_MyGCTheme\output;
class core_renderer extends \theme_MyParentTheme\output\core_renderer {}

I had to add an empty renderer in my grand-child theme for this to work. Otherwise the renderer from my parent theme would be ignored.

Thank you for your help Gareth, really appreciated.