Autoload & override blocks settings renderer in theme

Autoload & override blocks settings renderer in theme

by Bharat Pareek -
Number of replies: 6

Hello everyone

I am in process of creating a theme that will override various renderers to modify look and feel of moodle.

Right now i am following cleanm theme which uses auto loading to override core renderer, my requirement was to override the following renderers:

1. Block settings renderer '/blocks/settings/renderer.php'

2. Course Renderer '/course/renderer.php'


/theme_themename/output was the namespace i used to override core_renderer and it worked fine as i can override any function from core_renderer class, but the same is not happening with the two renderers described above.


Steps i followed (to override course renderer):

  • Created a class in themename/classes/output directory, the file name is course_renderer.php.

  • Tried the following class names:
  1. class core_course_renderer extends \core_course_renderer

  2. class theme_themename_course_renderer extends \core_course_renderer

  • Namespace 
  1. namespace theme_mdltheme\output

  2. namespace theme_mdltheme\output\course\renderer;

  3. namespace theme_mdltheme\output\course_renderer;


Steps i followed (to override blocks settings renderer):

  • Created a class in themename/classes/output directory, the file name is block_settings_renderer.php.

  • Tried the following class names:
  1. class block_settings_renderer extends \core_course_renderer

  2. theme_themename_block_settings_renderer extends \block_settings_renderer

  • Namespace 
  1. namespace theme_mdltheme\output

  2. namespace theme_mdltheme\output\blocks\settings\renderer;

None of the above worked as i can not override any functions in these classes. I have tried many variations in namespaces but could not find any solution.


I noticed both these classes i am trying to override by auto loading  does not use namespaces, so could this be the reason? 

I do not want to require the class first using 'require_once($CFG->dirroot . "/blocks/settings/renderer.php");'


Any help would be appreciated. 

Average of ratings: -
In reply to Bharat Pareek

Re: Autoload & override blocks settings renderer in theme

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

When converting Essential and Shoehorn to use automatic class loading and namespaces I also looked at the 'cleanm' theme, but its tricky.  Therefore use those two themes as an example and read: https://docs.moodle.org/dev/Automatic_class_loading.  Good luck!

Average of ratings: Useful (1)
In reply to Gareth J Barnard

Re: Autoload & override blocks settings renderer in theme

by Bharat Pareek -

Thanks a lot, it really helped. Now i have another question smile


In Essential theme you have used only the following line of code to override course renderer class:

File name: core_course_renderer.php

class theme_essential_core_course_renderer extends core_course_renderer {
}


Where as in Shoehorn theme you have included the core_course_renderer before overriding it, as given below:
File name: course_renderer.php

require_once($CFG->dirroot . "/course/renderer.php");
class theme_shoehorn_core_course_renderer extends core_course_renderer { }


I read about auto loading  but these two different ways of doing the same thing confused me. 

Could you please specify the difference between two approaches you have taken in both themes?

In reply to Bharat Pareek

Re: Autoload & override blocks settings renderer in theme

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

I learn all the time by discovery / trial and error.  Thus the line in Shoehorn is almost certainly from before the change and is now not needed.

You've also quoted the old file name in Shoehorn - fixed on GitHub yesterday.

There is only me checking my code and no 'peer' / Kent Beck 'Extreme' programming going on.  Oddities will happen.  I'm human.

Average of ratings: Useful (2)
In reply to Gareth J Barnard

Re: Autoload & override blocks settings renderer in theme

by Bharat Pareek -

Sorry Gareth

I was not criticizing you, I just wanted to know the difference if there was any. smile

Thanks again for clarification. smile

In reply to Bharat Pareek

Re: Autoload & override blocks settings renderer in theme

by Gareth J Barnard -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hi Bharat,

Oh, don't worry, I did not take it as such!  In fact, helped me to improve the code where I'd missed something, so thank you smile.

Cheers,

Gareth