Moodle theme render override last resort
I occasionally struggle to remember how to override different types of renderer.
My first port of call is always:
However, sometimes (and maybe it’s because I’m not reading the instructions right) I just can’t seem to get the renderer override to work.
My last resort is to see how moodle is trying to resolve the renderer. I do this via some temporary hacks to lib/outputfactories.php in the theme_overridden_renderer_factory class.
public function get_renderer(moodle_page $page, $component, $subtype = null, $target = null) {
$classnames = $this->standard_renderer_classnames($component, $subtype);
list($target, $suffix) = $this->get_target_suffix($target);
// Theme lib.php and renderers.php files are loaded automatically
// when loading the theme configs.
// First try the renderers with correct suffix.
foreach ($this->prefixes as $prefix) {
foreach ($classnames as $classnamedetails) {
if ($classnamedetails[‘validwithprefix’]) {
if ($classnamedetails[‘autoloaded’]) {
$newclassname = $prefix . $classnamedetails[‘classname’] . $suffix;
} else {
$newclassname = $prefix . ‘_’ . $classnamedetails[‘classname’] . $suffix;
} var_dump($newclassname);
if (class_exists($newclassname)) {
return new $newclassname($page, $target);
}
}
}
}
You could also set breakpoints up instead of the var_dump if you have xdebug remote debugging enabled.
With the hacked code I get something like this:
string(30) “theme_cfzoutputcore_renderer” string(34) “theme_cfzoutputmod_quiz_renderer” string(27) “theme_cfz_mod_quiz_renderer” string(36) “theme_boostoutputmod_quiz_renderer” string(29)
I’m trying to override the quiz renderer for my theme_cfz theme. From the output above I can see that it is looking for mod_quiz_renderer with a namespaced class of theme_cfzoutputmod_quiz_renderer. Simple! Now all I need to do is create mod_quiz_renderer.php inside moodle/theme_cfz/classes/output AND give it the appropriate namespace and classname.
Here’s an example of how my custom mod_quiz_renderer alters the quiz page to simply say ‘Hey’. (obviously not a really great example but you get the idea I hope).
<?php
namespace theme_cfzoutput;
defined(‘MOODLE_INTERNAL’) || die();
class mod_quiz_renderer extends mod_quiz_renderer {
public function attempt_page($attemptobj, $page, $accessmanager, $messages, $slots, $id,
$nextpage) {
return ‘HEY’;
}
}
I hope this helps other people struggling to resolve a renderer override.
TIP: Your theme must be configured to use the overridden renderer factory if you want renderer overrides to work. You can set this in your theme’s config.php file as follows:
$THEME->rendererfactory = ‘theme_overridden_renderer_factory’;
TIP: It’s important if you are using the classes/output folder in your theme to do the overrides, that you ensure any classes you create are registered with the autoloader. If you have debugging switched on to developer level it does this automatically. If not, I believe purge caches or a version bump will register your class.
Source: https://brudinie.medium.com/feed
- Moodle Educator Certificate course starts 1st October – 8th September 2024
- PHP 8 constructor promotion with inheritance (subclasses) – 23rd August 2024
- Moodle jobs: PHP Developer / DevOps (6 month contract) at Moodle HQ – Full-timeContract – Anywhere in Europe or Asia-Pacific – 21st August 2024