General developer forum

Help needed for overriding forum email renderer in theme

 
it's me
Help needed for overriding forum email renderer in theme
Group Core developersGroup Plugin developers

Hi,

Gareth Barnard and I tried to override the renderer for sending email via the forum. Unfortunately we were not very successful. You can have a look at our different approaches:

Link to first approach

1) Modify theme core renderer:

/theme/campus/classes/core_renderer.php and add that (setting 2nd param to true should send email as plaintext, which did not work)

class theme_campus_mod_forum_renderer extends mod_forum_renderer {
$data = $post->export_for_template($this, true);
return $this->render_from_template('mod_forum/' . $this->forum_post_template(), $data);
}
Also that did not work: https://github.com/dasistwas/moodle-theme_campus/commit/29e7e7dddaba2f3fa5f5c36116a9d456f4e10561#diff-6de519938595b6e8b92f56fc5aba0259

Link to second approach

Does not work either

These are the approaches taken from https://docs.moodle.org/dev/Overriding_a_renderer

The intention of the renderers is, that they can be overridden, but obviously that is not the case here. My assumption is, that cronjob just ignores themes totally and thus overriding via the theme won't work for cronjob tasks.

The goal would be to send a custom header and footer in every forum post. I think we can not achieve that by just modifying the template, which is quite easy to do.

Any help would be very appreciated.

Kind regards,
David
 
Average of ratings: -
Picture of Darko Miletić
Re: Help needed for overriding forum email renderer in theme
Group Core developersGroup Particularly helpful Moodlers

Could you elaborate more what exactly are you trying to do here and why?

 
Average of ratings: -
Gareth J Barnard
Re: Help needed for overriding forum email renderer in theme
Group Core developersGroup Particularly helpful MoodlersGroup Plugin developers

Solved!

We've managed to override the 'htmlemail' subtype renderer (really confusing as the file name is just 'renderer.php' in the '/mod/forum/classes/output/email' folder and not 'renderer_htmlemail' as with 'textemail' its 'renderer_textemail.php') within the theme.  This now works when the cron task runs and calls 'forum_cron()' in '/mod/forum/lib.php' and the theme is set for the site.

Now we can adapt in the theme the content of the eMail message that is sent when people post on a forum.

Does anybody understand subtype renderers?

Gareth

 
Average of ratings: -
it's me
Re: Help needed for overriding forum email renderer in theme
Group Core developersGroup Plugin developers

Hi,

so as Gareth already mentioned, we solved the problem. We did do some debugging sessions, which showed us the right renderer to override. Our first approach did not work due to overriding the wrong renderer. So debugging showed us, when Moodle looks for the email renderer, that renders the htmlemail the following classes can be used (campus is the name of the theme used, change to your theme name if you try to achieve similar things):

  1. theme_campus\output\mod_forum\email_renderer_htmlemail  This is the class Moodle looks for
    so folder structure and filename looks like that
    /theme/campus/output/mod_forum/email_renderer_htmlemail.php
    Namespace and classname within the file:
    namespace theme_campus\output\mod_forum\email;  
    class email_renderer_htmlemail extends \mod_forum\output\email\renderer {
    // overrides go here }
  2. theme_campus\output\mod_forum\email\renderer_htmlemail
  3. theme_campus_mod_forum_email_renderer_htmlemail

Another thing what you have to do is to put the mustache template used for htmlemail in the templates folder of your theme and then override the necessary methods:

    public function export_for_template(\renderer_base $renderer, $plaintext = false) {
if ($plaintext) {
return $this->export_for_template_text($renderer);
} else {
return $this->campus_export_for_template_html($renderer);
}
}

Note: campus_export_for_template_html has replaced the original method (protected so overriding not possible, so just use our own function instead).

In the method campus_export_for_template_html you can add additional variables, that you can also put in the template.


So hope that helps when someone tries to override html email renderer.


Kind regards

David




 
Average of ratings: Useful (1)
Picture of Mihir J
Re: Help needed for overriding forum email renderer in theme
 

Hi David

This is indeed very useful.

Good one!

Thanks!

Mihir

 
Average of ratings: -
it's me
Re: Help needed for overriding forum email renderer in theme
Group Core developersGroup Plugin developers

Hi,

I do have to make some corrections. The function export_for_template is not part of the renderer. So overriding this won't work. Instead use this function do override:


public function render_forum_post_email(\mod_forum\output\forum_post_email $post) {
// Was ($this, $this->target === RENDERER_TARGET_TEXTEMAIL) and as we are already 'htmlemail' it will always be false.
$data = $post->export_for_template($this, false);
// Add our new data.
$forumhtmlemailheader = \theme_campus\toolbox::get_setting('forumhtmlemailheader', 'format_html');
if ($forumhtmlemailheader) {
$data['messageheader'] = $forumhtmlemailheader;
}
$forumhtmlemailfooter = \theme_campus\toolbox::get_setting('forumhtmlemailfooter', 'format_html');
if ($forumhtmlemailfooter) {
$data['messagefooter'] = $forumhtmlemailfooter;
}

return $this->render_from_template('mod_forum/' . $this->forum_post_template(), $data);
}
 
Average of ratings: -