How Do I Edit The Breadcrumb Trail?

How Do I Edit The Breadcrumb Trail?

by Dylan Cleghorn -
Number of replies: 15

I have searched the forums and have yet to find a post explaining how to edit the breadcrumb trail.

I would like to take out some parts to make it simpler. For example, only showing the clickable strings would be better for our users.

Is there a certain file I need to edit? I've tried many different files but haven't found where I can edit this. 

Thanks.

Average of ratings: -
In reply to Dylan Cleghorn

Re: How Do I Edit The Breadcrumb Trail?

by Neelu Singh -

Hello Dylan,

Please go in mod/forum/view.php.

In reply to Neelu Singh

Re: How Do I Edit The Breadcrumb Trail?

by Dylan Cleghorn -

Neelu,

I greaty appreciate your response.

However, I don't see where I can edit the contents of the breadcrumb trail in this file. I can normally work my way around code pretty easily... but I feel completely ignorant with this customization.

Perhaps a more specific question could point me in a better direction. How would I remove a specific part, like MY COURSES or TOPIC #, from the breadcrumb trail?

Thanks again.

In reply to Dylan Cleghorn

Re: How Do I Edit The Breadcrumb Trail?

by Davo Smith -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers

You haven't stated whether you are using Moodle 1.9 or 2.0, so I'll give both answers:

Moodle 1.9 - function 'build_navigation' in lib/weblib.php

Moodle 2.0 - lib/outputrenderers.php 'navbar' function, along with the 'navbar' class in lib/navigationlib.php

I hope that points you in the right direction...

Average of ratings: Useful (3)
In reply to Davo Smith

Re: How Do I Edit The Breadcrumb Trail?

by Dylan Cleghorn -

Thank you for the reply, Davo.

I am using 2.0 - And I believe you have pointed me in the right direction.

I'm a novice with programming, and this customization is proving to be more confusing than I anticipated.

Looking at navigationlib.php I see many things. At line 125 I see 

array(0=>'system',10=>'category',20=>'course',30=>'structure',40=>'activity',50=>'resource',60=>'custom',70=>'setting', 80=>'user');

This looks to be a logical breadcrumb trail. Editing this, however, yields no changes to how the breadcrumb appears on the site.

Is there another line (or file) where I could edit the trail- for example, if I wanted to remove CATEGORY from the breadcrumb trail... so it would appear home>course instead of home>category>course?

Thanks

In reply to Dylan Cleghorn

Re: How Do I Edit The Breadcrumb Trail?

by Davo Smith -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers

The navbar function in outputrenderes.php takes the array of items generated by navbar->get_items() and draws it onto the page.

After that, it gets really, really complicated.

As I've found with several features of Moodle 2.x, they've been coded so cleverly as to automatically pick up every possible situation, that it is really difficult to trace through the intricacies of the code and understand what it is actually doing!

I think it will have something to do with setting the variable '$mainnavonly', there may be some milage in finding the first 'add' function in navigationlib.php, and near the end of the function, you could try:

if ($action === null) {

  $node->mainnavonly = true;

}

But I have no idea if that will work (my guess is that it should prevent any items without a link from appearing in the breadcrumb trail).

Average of ratings: Useful (1)
In reply to Davo Smith

Re: How Do I Edit The Breadcrumb Trail?

by Dylan Cleghorn -

Thanks Davo.

I placed

if ($action === null) { 
$node->mainnavonly = true;
}

in line 290 of navigationlib.php, just before return $node.  It did just what you guessed, hid the non link items in the breadcrumb. This is pretty close to what I was hoping to accomplish and I'll use this fix for now.

I was hoping it would be easier to edit the breadcrumb trail, add or remove specific items, so I could play with different arrangements (as there are still some linked items that aren't necessary for me). But I guess this just isn't a common enough customization to be a moodle feature. Oh well... I'll keep hackin around.

Thanks again. I certainly wouldn't have got this on my own!

In reply to Dylan Cleghorn

Vastus: Re: How Do I Edit The Breadcrumb Trail?

by Tõnis Tartes -

Changing the core libraries aint usually the best idea, to achieve the same result without changing the navigationlib.php, in your theme folder create a new file called renderers.php.

Inside there, add the following line:

class theme_[yourthemefoldername]_core_renderer extends core_renderer {

}

Then copy paste the navbar function from lib/outputrenderers.php and place it into renderers.php.

Inside the navbar function there are lines like:

for ($i=0;$i < $itemcount;$i++) {
     $item = $items[$i];
     $item->hideicon = true;

After the $item->hideicon = true; add -

 if ($item->action === null) {
     continue;
}

Thats it.

Average of ratings: Useful (2)
In reply to Tõnis Tartes

Re: Vastus: Re: How Do I Edit The Breadcrumb Trail?

by edu utima -

Hi Tõnis!

In my renderer.php I put thisway, but it did not work! Any tips?

<?php
class theme_aardvark_core_renderer extends core_renderer {
}

/**
* Return the navbar content so that it can be echoed out by the layout
* @return string XHTML navbar
*/
public function navbar() {
$items = $this->page->navbar->get_items();
$htmlblocks = array();
// Iterate the navarray and display each node
$itemcount = count($items);
$separator = get_separator();
for ($i=0;$i < $itemcount;$i++) {
$item = $items[$i];
$item->hideicon = true;
if ($item->action === null) {
continue;
}
if ($i===0) {
$content = html_writer::tag('li', $this->render($item));
} else {
$content = html_writer::tag('li', $separator.$this->render($item));
}
$htmlblocks[] = $content;
}

Regards,
Edu

In reply to Tõnis Tartes

Re: Vastus: Re: How Do I Edit The Breadcrumb Trail?

by Matt Rice -

We didn't like how long the breadcrumb trail was getting, so I was tasked to making it shorter: it was decided that for course pages we should include a link back to the course and the link to the last clickable crumb, if any. We have not touched breadcrumbs on other pages as we were mostly concerned with course pages.

Here's my code: (excerpted from /theme/essential/renderers.php)

     /*
     * This renders the navbar.
     * Uses bootstrap compatible html.
     */
    public function navbar() {
        $is_course = false;
        $items = $this->page->navbar->get_items();

        //If this is a course, only output the course node and the last link node (if any)
        //       (e.g. $item->action is not null)
        foreach ($items as $item) {
                if (!$is_course) {
                        $is_course = (navigation_node::TYPE_COURSE == $item->type);
                }
        }

        if ($is_course) {
                //Only output some nodes
                $last_node_found = false;
                $items = array_reverse($items);

                foreach ($items as $item) {
                        //Only output crumbs with links
                        if (navigation_node::TYPE_COURSE == $item->type ||
                                (!$last_node_found && $item->action)) {
                                $last_node_found = true;
                                $item->hideicon = true;
                                $breadcrumbs[] = $this->render($item);
                        }

                        if (navigation_node::TYPE_COURSE == $item->type) {
                                //We're done here
                                break;
                        }
                }

                $breadcrumbs = array_reverse($breadcrumbs);
        }
        else {
                //Output all navigation nodes
                foreach ($items as $item) {
                        $item->hideicon = true;
                        $breadcrumbs[] = $this->render($item);
                }
        }
        $divider = '<span class="divider">/</span>';
        $list_items = '<li>'.join(" $divider</li><li>", $breadcrumbs).'</li>';
        $title = '<span class="accesshide">'.get_string('pagepath').'</span>';
        return $title . "<ul class=\"breadcrumb\">$list_items</ul>";
    }

We are running Moodle 2.5 with the Julian Ridden's Essential theme.

In reply to Matt Rice

Re: Vastus: Re: How Do I Edit The Breadcrumb Trail?

by James Ballard -

We used a similar method to over-ride core renderer based on the $item->type to remove the sections item and truncate some of the user input text fields that have variable length:

public function navbar() {
$items = $this->page->navbar->get_items();
foreach ($items as $item) {
$item->hideicon = true;
// Do not include the section item.
if (in_array($item->type, array(global_navigation::TYPE_SECTION))) {
continue;
} else if (in_array($item->type, array(global_navigation::TYPE_CATEGORY, global_navigation::TYPE_COURSE, global_navigation::TYPE_ACTIVITY, global_navigation::TYPE_RESOURCE)) and strlen($item->text) > 40) {
// If the text is longer than 40 characters then truncate.
$item->text = substr($item->text,0,40)."...";
}
$breadcrumbs[] = $this->render($item);
}
$divider = '<span class="divider">/</span>';
$list_items = '<li>'.join(" $divider</li><li>", $breadcrumbs).'</li>';
$title = '<span class="accesshide">'.get_string('pagepath').'</span>';
return $title . "<ul class=\"breadcrumb\"><li>$list_items</li></ul>";
}
In reply to James Ballard

Re: Vastus: Re: How Do I Edit The Breadcrumb Trail?

by Dan Olson -

James,

That seems like a nice solution. We've built a custom theme based on Clean for Moodle 2.9 and I have experience creating a renderers.php file to extend the core renderer. I'd like to build on your example to simply remove the "Dashboard" link from the breadcrumb trail since we don't use the Dashboard. Any idea how to pull that one off? Must be a simple "if dashboard" then "do not include" -- but I'm unsure on the details. 

Thanks in advance,
Dan


In reply to Matt Rice

Re: Vastus: Re: How Do I Edit The Breadcrumb Trail?

by Richard Clay -

Thanks for this solution Matt. It works well in our 2.6 system and reduces the breadcrumb trail nicely.

One question for you or anyone else who might know - would it  be possible include a link to the parent category in the trail before the course link? I think this would be a useful addition.

Many thanks

Richard 

In reply to Dylan Cleghorn

Re: How Do I Edit The Breadcrumb Trail?

by Stuart Lamour -
Picture of Plugin developers

Hi Dylan, 

Paolo at Sussex did some nice code to make our breadcrumb more like those on other websites, which you can see here -

http://blogs.sussex.ac.uk/elearningteam/2011/07/12/moodle-breadcrumb/

not sure if its exactly what your looking for, but hopefully a nod in the right direction.

Cheers

Stuart

Average of ratings: Useful (1)
In reply to Stuart Lamour

Re: How Do I Edit The Breadcrumb Trail?

by Max Monclair -

Paolo's solution is what I'm trying to do for my breadcrumb as well. However, this is for version 1.9, and I'm using 2.1. I did some quick poking around to see if I could find something similar in my 2.1 core files, but could not. I'm also a novice at this, so I could very well have missed a similar function.

In reply to Max Monclair

Re: How Do I Edit The Breadcrumb Trail?

by Judy Hsu -

I'm glad that I found this old thread.

Just a quick related question. It seems that in Moodle 1.9.x, the "short name for site" setting in the Front page settings will be used as the breadcrumb trail of your Moodle site. However, this is no longer the case for Moodle 2.x. If you changed the wording of your "Short name for site", the beginning of your breadcrumb will still show as "Home" instead of the "Short name for site." Can anyone please confirm this? Is this a bug or is there a revised "feature?" Many thanks!

Average of ratings: Useful (2)