General developer forum

Moodle 3.4: Overriding block output via the theme

 
Picture of Raad Al-Rawi
Moodle 3.4: Overriding block output via the theme
 

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: -
Picture of Raad Al-Rawi
Re: Moodle 3.4: Overriding block output via the theme
 

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

 
Average of ratings: -
Picture of Mark Johnson
Re: Moodle 3.4: Overriding block output via the theme
Core developersParticularly helpful MoodlersPlugin 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?

 
Average of ratings: -
Picture of Raad Al-Rawi
Re: Moodle 3.4: Overriding block output via the theme
 

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



 
Average of ratings: -
Picture of Mark Johnson
Re: Moodle 3.4: Overriding block output via the theme
Core developersParticularly helpful MoodlersPlugin developers

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

 
Average of ratings: -
Picture of Raad Al-Rawi
Re: Moodle 3.4: Overriding block output via the theme
 

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.



 
Average of ratings: -