Overriding a renderer with namespace

Re: Overriding a renderer with namespace

Acs Gabor -
Atsakymų skaičius: 5
Dear Damyon,

I have one more question, is there any difference with the course/renderer.php ?
This method worked like a charm with format_topics_renderer and with mod_quiz_renderer, but I were not able to make this work with this one.

I tried with core_course_renderer and with just course_renderer with the same format, but Moodle just does not load it either way. (no error message this time, the system does not notice the file...)

Thanks in advance!
Atsakymas į Acs Gabor

Re: Overriding a renderer with namespace

Michael Meneses -

Hi,

I have same problem with course/renderer.php.

I working this. Using theme child of theme_boost.

See you later.

Atsakymas į Michael Meneses

Re: Overriding a renderer with namespace

Michael Meneses -

Hi,

The renderer core_course_renderer is in theme_boost/classes/output/core/course_renderer.php, copied to theme child.


Thank you for all

Atsakymas į Michael Meneses

Re: Overriding a renderer with namespace

Acs Gabor -

Thanks Michael,  I also figured out  finally that course_renderer needs to be put into the output/core folder.

Overriding other renderers look even more complicated. I tried to override backup/util/ui/renderer.php with this method, but no combination works.

I found a complicated example in theme_boost (gradereport_history_renderer) :

class gradereport_history_renderer extends \gradereport_history\output\renderer  - but I were not able to understand the logic behind this. (The original folder is grade/report/history/classes/output/renderer.php)

So for backup/util/ui/renderer.php it should be backup_util\ui\renderer but I tried every possible combination I could think of with '_ 'and '\' marks at different places with no success.

If someone could tell me the logic behind this (or what is the correct way to override), I would be grateful, I am really frustrated by this.

Atsakymas į Acs Gabor

Re: Overriding a renderer with namespace

Nicholas Yang -
/course/renderer.php is like /backup/util/ui/renderer.php in that they both aren't in a classes/output folder. So I used the /theme/boost/classes/output/core/course_renderer.php as reference to create the file /theme/mytheme/classes/output/core/backup_renderer.php as follows:

<?php

namespace theme_mytheme\output\core;
defined('MOODLE_INTERNAL') || die;

require_once($CFG->dirroot . '/backup/util/ui/renderer.php');

class backup_renderer extends \core_backup_renderer {
  ...
}

I tested it by overriding render_backup_files_viewer() function. One slight annoyance was that I had to go through all the classes/functions that don't belong to the theme_mytheme\output\core namespace and add a preceding backslash "\". In this example:

  • \backup_files_viewer
  • \html_table
  • \html_writer
  • \moodle_url


  public function render_backup_files_viewer(\backup_files_viewer $viewer) {
global $CFG;
$files = $viewer->files;

$table = new \html_table();
$table->attributes['class'] = 'MY-RANDOM-TEST-CLASS backup-files-table generaltable';

...

display_size($file->get_filesize()),
\html_writer::link($fileurl, get_string('download')),
\html_writer::link($restoreurl, get_string('restore')),

...

$html = \html_writer::table($table);
$html .= $this->output->single_button(
new \moodle_url('/backup/backupfilesedit.php', array(

...

return $html;
}


I'm the same as you, used to overriding renderers in that old way. I've been using the Elegance theme before this (which unfortunately has no Moodle 3.3 version yet), and that uses a mytheme/renderers.php file to include other renderer PHP files in a renderers subdirectory.

Is there an advantage to doing the autoload way (with the classes/output) directory? It was frustrating for me having to clear the cache several times when I was still trying to figure out the right filename and path. But I guess once the name is right, I shouldn't have to keep clearing the cache? It just made it really annoying to find the right combination for overriding :p.

I definitely second your desire for a clearer explanation or tutorial behind the logic of how to figure out all the naming conventions for the different types of renderers (with/without namespaces, with/without being nested in subdirectories, etc.)

Atsakymas į Nicholas Yang

Re: Overriding a renderer with namespace

Acs Gabor -

Thank you Nicholas!

You helped a lot, the whole process is much clearer now and I managed to make the backup renderer work as well!