Moodle 3.4: Overriding block output via the theme

Moodle 3.4: Overriding block output via the theme

by Raad Al-Rawi -
Number of replies: 8

Hi All


This is different to the usual "how do I override a renderer in my theme?" problem, hence my posting the question.

So what I am trying to do is override the output from the myoverview block (which produces the "Course overview" content on the Dashboard page) and add an "un-enrol me" link to each course record displayed.

I can override the mustache templates and add the extra elements to display the link, no problem. However, I need to generate the link and pass it into the renderer context and this is where I have a problem.

The block has the following renderer structure:

myoverview - classes - output + courses-view.php
                              + main.php
                              + renderer.php

and I believe I need to inject the link in courses-view.php, so to that effect I have created an override in my theme:

mytheme - classes - output - block_myoverview - courses-view.php
courses-view.php

<?php
// This file is part of Moodle - http://moodle.org/
//
...
namespace theme_mytheme\output\block_myoverview;
defined('MOODLE_INTERNAL') || die();
...
/**
 * Export this data so it can be used as the context for a mustache template.
 *
 * @param \renderer_base $output
 * @return array
 */
public function export_for_template(renderer_base $output) {
    global $CFG;
    ...
    foreach ($this->courses as $course) {
        ...
        $exportedcourse = $exporter->export($output);
        ...
        $exportedcourse->unenrollink = $this->generate_unenrol_link($course); // Uses a custom function to generate link
        ...
    }
    ...
}

However, this override is not being picked up =(

Does it have the wrong namespace, is it in the wrong location, or is it not possible to do this?

Any help greatly appreciated!


Cheers


Raad

~~

Raad Al-Rawi

Moodle Technical Developer/DBA

University of Cambridge

Average of ratings: -
In reply to Raad Al-Rawi

Re: Moodle 3.4: Overriding block output via the theme

by Raad Al-Rawi -

Does anyone have any ideas? I am starting to suspect the myoverview block does not implement a renderer that can be overridden in this way - am I correct?


Cheers


Raad

In reply to Raad Al-Rawi

Re: Moodle 3.4: Overriding block output via the theme

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Hi Raad,

The My Overview block does use renderers and templates that can be overridden by the theme.   The code you've posted above shows you adding a link to the context for the template - have you also overridden the template to output this link somewhere?

In reply to Mark Johnson

Re: Moodle 3.4: Overriding block output via the theme

by Raad Al-Rawi -

Hi Mark - Thanks for responding!


Yes, I created a template under my theme:

mytheme - templates - block_myoverview - courses-view-course-item.php

and in that courses-view-course-item.php I have a section that outputs the link, e.g.

...
<div>{{unenrollink}}</div>
...
I believe this should work, so I was thinking maybe I'm missing something else, like a layout file perhaps, although the class method export_for_template seems to do all the work.


However, I now have a suspicion that the class override is not being picked up by the system, because if I introduce a syntax error in the courses-view.php file it doesn't generate an error. However, I can't see where else I should put the class override other than:

mytheme/classes/output/block_myoverview/courses-view.php

Any pointers would be fantastic.


Cheers


Raad



In reply to Raad Al-Rawi

Re: Moodle 3.4: Overriding block output via the theme

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Your template should have the .mustache extension, not .php. 

In reply to Mark Johnson

Re: Moodle 3.4: Overriding block output via the theme

by Raad Al-Rawi -

Sorry - typo crept in there - it does actually have a .mustache extension (that would have been embarrassing!).

I've been fiddling with this a bit more and it is possible to override the block renderer as per Fabio's post in  the topic Regarding myoverview block overriding in theme so I've tried to extend this method to the courses_view but no joy so far.



In reply to Raad Al-Rawi

Re: Moodle 3.4: Overriding block output via the theme

by Γιώργος Μαριός -

I'm having the same problem, trying to modify the output in blocks/myoverview/classes/output/courses_view.php.

Overriding the renderer gives an error about the namespace of the $main variable that is passed to it.

It seems to me that the question is: Are we allowed to override a plugin class that is not a renderer? If not, what would be the right approach?

In reply to Γιώργος Μαριός

Re: Moodle 3.4: Overriding block output via the theme

by Darko Miletić -
Are we allowed to override a plugin class that is not a renderer?
No.

 

If not, what would be the right approach?
There is no right approach. Moodle was not designed for that kind of modification. There is something called Customscripts. But that is path leading to madness smile

The only tangible way to proceed would be to write a custom javascript plugin that will rewrite the html dumped by that plugin.

Average of ratings: Useful (2)