Adding a compact logo to the frontpage (Moodle)

Adding a compact logo to the frontpage (Moodle)

Today I resolved a very small problem which had been bugging me for awhile. On our Moodle theme the small compact logo appeared in the top navbar everywhere except on the frontpage. On the frontpage it was displaying correctly.

To solve I first checked the mustache template file in the boost theme / my theme to get some clues as to why it wasn’t working.

<a href="{{{ config.wwwroot }}}" class="navbar-brand aabtn {{# output.should_display_navbar_logo }}has-logo{{/ output.should_display_navbar_logo }}
        {{^ output.should_display_navbar_logo }}
                d-none d-sm-inline
        {{/ output.should_display_navbar_logo }}
">
			
        {{# output.should_display_navbar_logo }}
                <span class="logo d-none d-sm-inline">
                        {{# output.get_compact_logo_url }}
			        <img src="{{output.get_compact_logo_url}}" alt="{{sitename}}">
			{{/ output.get_compact_logo_url }}
                        {{^ output.get_compact_logo_url }}
			        <span class="site-name d-none d-md-inline">{{{ sitename }}}</span>
			{{/ output.get_compact_logo_url }}
                </span>
        {{/ output.should_display_navbar_logo }}
           
        {{^ output.should_display_navbar_logo }}
                <span class="site-name d-none d-md-inline test">{{{ sitename }}}</span>
        {{/ output.should_display_navbar_logo }}
            
</a>

The code I found referenced the following function “should_display_navbar_logo”.

To find out where this was coming from I used grep and searched the moodle installation for a reference to this function and discovered it was coming from lib/outputrenderers.php

public function should_display_navbar_logo() {
        $logo = $this->get_compact_logo_url();
        return !empty($logo) && !$this->should_display_main_logo();
}

This function in turn referenced another function called “should_display_main_logo” guessing it may be in the same file I searched again and found the function.

public function should_display_main_logo($headinglevel = 1) {

        // Only render the logo if we're on the front page or login page and the we have a logo.
        $logo = $this->get_logo_url();
        if ($headinglevel == 1 && !empty($logo)) {
                if ($this->page->pagelayout == 'frontpage' || $this->page->pagelayout == 'login') {
                        return true;
                }
         }

         return false;
 }

It was this function that was causing the compact logo to disappear on the frontpage. In order to override it I needed to write a renderer override within my theme. I already have a core renderer override setup in my theme to handle some of the footer elements so I didn’t need to reset this up.

defined('MOODLE_INTERNAL') || die();

class theme_name_core_renderer extends core_renderer {

     /* ... */

}

I created a copy of the “Should_display_main_logo” function and added it into my renderer override class. I think modified the following line:

//if ($this->page->pagelayout == 'frontpage' || $this->page->pagelayout == 'login') {
if ($this->page->pagelayout == 'login') {

This change means that the compact logo will now show on the frontpage.

Comments are closed.