Override/add to navigationlib.php in renderer to style breadcrumbs?

Override/add to navigationlib.php in renderer to style breadcrumbs?

by A W -
Number of replies: 7

Hi,

Using Moodle 3.2.2. 

To colour code breadcrumbs so the course breadcrumb is a different colour I've found I CAN achieve this simply by just adding this function (3 lines below) to lib\navigationlib.php, I can then call that function from the navbar.mustache template and depending on what it returns the breadcrumb can be a different colour. BUT in PROD environment we can't change anything that isn't in the /theme folder and navigationlib.php is in the higher up/lib folder. 

So, is it possible to override/add to navigationlib.php from within the theme folder i.e. override it somehow with custom render or something?

For reference function I had to navigationlib.php is:

public function has_iscourse() {

       if (preg_match('#course/view#', $this->action)) return (1);

}



and can call this from navbar.mustache:

{{#get_items}}

            {{#has_action}}

                 {{#has_iscourse}} 

                        <li class="breadcrumb-item"><a style="font-weight: bold;" href="{{{action}}}" {{#get_title}}title="{{get_title}}"{{/get_title}}>{{{text}}}</a></li>

                 {{/has_iscourse}}

                 {{^has_iscourse}}

                         <li class="breadcrumb-item"><a href="{{{action}}}" {{#get_title}}title="{{get_title}}"{{/get_title}}>{{{text}}}</a></li>

                 {{/has_iscourse}}

            {{/has_action}}

            {{^has_action}}

                        <li class="breadcrumb-item">{{{text}}}</li>

            {{/has_action}}

        {{/get_items}}


Any help much appreciated,

Thanks,

Alex

Average of ratings: -
In reply to A W

Re: Override/add to navigationlib.php in renderer to style breadcrumbs?

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Hi,

When you say a different background-colour for courses, do you mean exactly that, as in the background changes to the colour "red" in a course page?

If so this can be done with CSS no need for a lib function.

Example:

.pagelayout-incourse .breadcrumb { background-color: #aabbcc; }

Hope this helps?

Mary

In reply to Mary Evans

Re: Override/add to navigationlib.php in renderer to style breadcrumbs?

by A W -

Hi Mary,

Thanks for the suggestion.  The code I added to navigationlib.php allows you to discern if an actual breadcrumb (within the trail) is for the course hence you can style it any way you want e.g. turning it blue as per below to make returning to the course easier (if there are lots of subsequent crumbs).


Blue breadcrumb


Thanks,

Alex

In reply to A W

Re: Override/add to navigationlib.php in renderer to style breadcrumbs?

by Mary Evans -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

In that case, can you please explain what "PROD" means as this is key to your original question?

Also...I need to say sorry because I misread your original question. In my head I saw a coloured breadcrumb nav bar...

Thanks

Mary

In reply to A W

Re: Override/add to navigationlib.php in renderer to style breadcrumbs?

by Andrew Davidson -

Hi Alex,

While the core implementation of the navbar output calls the get_items function from the mustache template,  you could easily do this from within your themes core renderer override and then loop through the items to add the information you desire.

I've done something similar to this recently, but it involves building your own object to send to the template renderer

Something like this:

public function navbar() {
    $items = $this->page->navbar->get_items();
    foreach ($items as $key => $item) {
        if (// logic for is course) {
            $items[$key]->iscourse = true;
        }
        $items[$key]->titletext = $item->get_title();
        $items[$key]->text = $item->text;
    }
    $context = new stdClass();
    $context->items = array_values($items);
    return $this->render_from_template('core/navbar', $context);
}

And then your mustache template would look something like this:

<nav role="navigation">
    <ol class="breadcrumb">
        {{#items}}
            {{#action}}
                {{#iscourse}} 
                        <li class="breadcrumb-item"><a style="font-weight: bold;" href="{{{action}}}" {{#title}}title="{{title}}"{{/title}}>{{{text}}}</a></li>
                 {{/iscourse}}
                 {{^iscourse}}
                         <li class="breadcrumb-item"><a href="{{{action}}}" {{#title}}title="{{title}}"{{/title}}>{{{text}}}</a></li>
                 {{/iscourse}}
            {{/action}}
            {{^action}}
                <li class="breadcrumb-item">{{{text}}}</li>
            {{/action}}
        {{/items}}
    </ol>
</nav>

Hope that helps

Average of ratings: Useful (1)
In reply to Andrew Davidson

Re: Override/add to navigationlib.php in renderer to style breadcrumbs?

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

Just to simplify it further, when I have done something like this in the past (not in mustache, but the principle holds) I have used a variable to hold the classes as a string and passed that to avoid the conditional in the layout code itself. Something along the lines of:

if (// logic for is course ) {
    $items[$key]->breadcrumbclass = 'breadcrumb-item breadcrumb-course';
} else {
    $items[$key]->breadcrumbclass = 'breadcrumb-item';
}

So you only then need one line for this in the template:

{{#iscourse}} 
                        <li class="breadcrumb-item"><a style="font-weight: bold;" href="{{{action}}}" {{#title}}title="{{title}}"{{/title}}>{{{text}}}</a></li>
                 {{/iscourse}}
                 {{^iscourse}}
<li class="{{breadcrumbclass}}"><a href="{{{action}}}" {{#title}}title="{{title}}"{{/title}}>{{{text}}}</a></li>
                 {{/iscourse}}
Not suggesting this is necessarily better, but a personal preference to keep the template as simple as possible and have as much of the logic in the function/layout file calling it as possible - seems to me to be in the spirit of the mustache templating to reduce the logic in each template, where possible smile

In reply to Richard Oelmann

Re: Override/add to navigationlib.php in renderer to style breadcrumbs?

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

Oh forgot to say - although hopefully obvious: you then need to set the css for how you want the breadcrumb-course class to show smile But its then in the css rather than an inline element, so easier to override in the future too.

In reply to Richard Oelmann

Re: Override/add to navigationlib.php in renderer to style breadcrumbs?

by A W -

All,

Many thanks for the suggestions, I'll give these a go, certainly looks doable.

Thanks,

Alex