Moodle 2 - how to set up breadcrumbs for a module page

Moodle 2 - how to set up breadcrumbs for a module page

sam marshall -
Vastausten määrä: 10
Kuva: Core developers Kuva: Peer reviewers Kuva: Plugin developers
Hi,

I can't find this in the Moodle 2 developer documentation so thought it might be most useful to ask here. (I could ask in chat but the relevant people - mainly Sam Hemelryk I think - aren't usually active at the same time as me.)

Can anybody point me in the right direction re setting up breadcrumbs for a module page in Moodle 2?

The module I'm currently converting to Moodle 2 has a system where you can 'preview' things outside the course structure before choosing to save them into a course. So basically, I have these 'preview' things which are not at course level.

Finally, there are pages related to things (which might be preview things). Such as, say, the 'Do something' page.

I don't want to use the 'backward compatibility' version where it does everything the old way, so as a result, I'm using the new layout system ($OUTPUT, $PAGE, etc). Comparing my breadcrumbs with the 1.9 version I'm trying to match, they come out like this:

1. If looking at a preview thing:

1.9: Preview > Name of thing
2.0: (no breadcrumbs)

Question: How do I get breadcrumbs in this situation? This situation doesn't really need any links (as shown) but I'm going to want them so I can add onto them on point 3.

2. If looking at a thing that's saved to the course:

1.9: Home > Course > Name of thing
2.0: Home > Courses > Category > Course > General > Name of thing

Question: This is OK but it's a bit crap that Name of thing is a link when I'm already on that exact URL. What's up with that?!

3. If looking at an action page for a preview thing:

1.9: Preview > Name of thing > Do something
2.0: (no breadcrumbs)

Note: same question as #1, but this is the important one - I need the link back to the thing, but I'm not getting any breadcrumbs.

4. If looking at an action page for a thing that's saved to the course:

1.9: Home > Course > Name of thing > Do something
2.0: Home > Courses > Category > Course > General > Name of thing

Note: Not really a serious problem as at least I have the link back, but it might be nice to add the > Do something to the end of the link.

To note, I did look at $PAGE->navbar but it seems like there aren't any useful methods you could use to add things to it.

Maybe this is related to the tree view of navigation - it would be fine to add these pages to the tree view too, preferably if they only show in it when I'm actually on that page anyway. Is that the approach to take?

Also, when it works, the breadcrumb trail is crazy long - this will definitely wrap to two lines on many OU course activities. Is it possible to use a site option to hide some of these things from the breadcrumb trail? (We'd like to hide home, courses, category, and 'general' - but hiding any of those is better than nothing.) I guess this is the sort of thing you can customise with a renderer in the theme?

--sam
Arviointien keskiarvo: -
Vastaus sam marshall

Re: Moodle 2 - how to set up breadcrumbs for a module page

Sam Hemelryk -
Hi Sam

Sorry I completely missed this, Tim ended up pointing me here.
I've just started work on the Development:Moodle_2.0_navigation_how_to to document how the navigation works and can be utilised.

However as you have already been waiting hopefully this is helpful, its also probably more in depth that you need but may help other who find this posting in need of information. I do warn you I haven't yet mastered the art of concise writing so this is very verbose.

Before we start a few notes:
  1. The navbar is based of the navigation, it is essentially the active item on the navigation preceded by all of its parents.
  2. An item is automatically marked active if it's link matches the link in that was set for the current page ($PAGE->set_url)
  3. If a page isn't on the navigation it can't be active and it won't ever be automatically included on the navbar.
  4. There are two ways to get something onto the navbar, first add it to the navigation and make sure it is active when being viewed, and second telling the navbar to add it manually.
  5. Module callbacks can only add to the navigation for the module, however you can add to the navigation generally from any page the downside being however that the navigation will only be extended as you want on the pages that execute your navigation code (although I think this is what you are after Sam).
This basically means you can either
  • Extend the modules navigation through the module callbacks (home > category > course > section > module > *) in which case it will always be available through AJAX expansion and will always be shown when viewing the module or any part of it.
  • Extend the navigation through your scripts in which case you can extend the navigation any where you want however it will only be seen when your extension code gets executed (within your pages).
So that all being said lets look at your questions:

If the first situation where you want a completely unique navbar you can either extend the navbar and not worry about the general navigation or add it to the navigation.

$PAGE->navbar->ignore_active();
$PAGE->navbar->add(get_string('preview'), new moodle_url('/a/link/if/you/want/one.php'));
$PAGE->navbar->add(get_string('name of thing'), new moodle_url('/a/link/if/you/want/one.php'));


The above code tells the navbar to ignore what ever the active page was and just use what you add, at which point we add two items as shown.

$previewnode = $PAGE->navigation->add(get_string('preview'), new moodle_url('/a/link/if/you/want/one.php'), navigation_node::TYPE_CONTAINER);
$thingnode = $previewnode->add(get_string('name of thing'), new moodle_url('/a/link/if/you/want/one.php'));
$thingnode->make_active();


The above lines of code adds a preview node to the bottom of the navigation and then adds a thingnode to the previewnode (adding a leaf to our tree).
The final line of code makes the thingnode active so that the navbar finds it however if the URL you give it is the same as the url you set for the page it will automatically be marked active and you won't need this call.

Next extending the navigation for the course.
For this you will need to know the course id and have called require_login($courseorid); so that the navigation is loaded for the course.

$coursenode = $PAGE->navigation->find($courseid, navigation_node::TYPE_COURSE);
$thingnode = $coursenode->add('Name of thing', new moodle_url('/a/link/if/you/want/one.php'));
$thingnode->make_active();


The new bit of code here really is the first line which simply finds the course node, to do this we give it the course id and the node type in this case TYPE_COURSE.
What we are doing here is relying on the navigation to generate the navigation up to the course and then just adding to the course.
In regards to the final element on the navbar being a link... hehe it's just how it is presently. It can be changed easily if desired.

The third and forth you probably have by now hymy

In regards to extending the navigation using the module callbacks plus a bit more information you can check out the navbar proposal however it is out of date and will be replaced by the doc I am about to write.

Finally to answer your question about hiding bits of the navbar there are no settings to do it at the moment, you could indeed override the core renderer from a theme and add some logic to only add what you want however I imagine it would be something that others may want as well (in fact I'm 100% positive it is) so perhaps we should come up with a setting to do it.

My apologies for the length of this, I have pretty much just dumped out information. Hopefully enough to answer your questions.

Cheers
Sam
Arviointien keskiarvo:Useful (1)
Vastaus Sam Hemelryk

Re: Moodle 2 - how to set up breadcrumbs for a module page

sam marshall -
Kuva: Core developers Kuva: Peer reviewers Kuva: Plugin developers
Thanks Sam. As well as having a great name, you're really helpful too. hymy Using this I've been able to add support for all four cases and it was pretty easy! Nice system.

About the 'link to current page' - I've previously had this reported by our testers as a minor accessibility problem. Presumably it potentially confuses screenreader users who may more heavily rely on features that list available links on the page, etc. I don't think it's a big problem, just saying.

--sam
Vastaus Sam Hemelryk

Re: Moodle 2 - how to set up breadcrumbs for a module page

tim st.clair -
Kuva: Plugin developers

Hi. You mention that there is new docmentation that you were working on. Is that http://docs.moodle.org/dev/Moodle_2.0_navigation_how_to? I tried to cut n paste the example from here to my settings.php in my plugin and the link turned up on the breadcrumb bar, not the navigation (e.g. $previewnode = $PAGE->navigation->add(...) should add to navigation, but it adds to $PAGE->navbar.)

I'd really like to find out how to use the "find" method. I tried $container = $PAGE->navigation->find(get_string('myprofile'), navigation_node::NODETYPE_BRANCH); but I can't find out where NODETYPE_BRANCH and such are defined or documented as to what they actually represent. Currently I have to do a code disk search and then try to figure it out myself. Is this the best way or have you finished the documentation you mentioned in this post (2 years ago)? (p.s. this is about how long it takes me to document code too!)

Vastaus tim st.clair

Re: Moodle 2 - how to set up breadcrumbs for a module page

Mark Ward -

One note which might help someone, I spent a bit of time trying to set this up on a custom search page yesterday with absolutely no luck... it took me about 30 minutes of confusion before I realised I was messing around with the breadcrumbs after calling printing $OUTPUT->header(). Doh! :P

Vastaus Sam Hemelryk

Re: Moodle 2 - how to set up breadcrumbs for a module page

Andrew Normore -

Whoa, there is definatley information that was useful on this post, that did NOT make it to your documentation page.

I am building a custom plugin in the /local directory.

I was looking for exactly this:

$PAGE->navbar->ignore_active();
$PAGE->navbar->add(get_string('preview')new moodle_url('/a/link/if/you/want/one.php'));
$PAGE->navbar->add(get_string('name of thing')new moodle_url('/a/link/if/you/want/one.php'));

 

How do I add a node to Site administration -> Courses -> My Plugin and set it active?


Many thanks !

Vastaus Andrew Normore

Re: Moodle 2 - how to set up breadcrumbs for a module page

Brian Merritt -
Kuva: Particularly helpful Moodlers

Hey Andrew & All

Did you get an answer? If yes, can you repost here or insert the URL re

How do I add a node to Site administration -> Courses -> My Plugin and set it active?

 

Many thanks

Vastaus Brian Merritt

Re: Moodle 2 - how to set up breadcrumbs for a module page

Andrew Normore -

Hi Brian, please see the post directly above yours for the answer. It was 3 lines of code. 

If you're still having trouble, make sure you are setting up the page properly:
echo $OUTPUT->header() 

Are you having trouble with it?

Vastaus Andrew Normore

Re: Moodle 2 - how to set up breadcrumbs for a module page

Brian Merritt -
Kuva: Particularly helpful Moodlers

Thanks for the reply Andrew - I just realised that what I thought was a new question was a summary hymy

The problem I am having is not to change the navbar from any module page, but how to insert changes either to breadcrumbs or to add a navigation section in the central column for all modules (I want to add a "<---Previous Activity        Next Activity--->" capability to modules so students can hop from their current activity to the next activity without having to go back to the section display.

Open University courses for example appear to allow the student to move from content to activity to activity to more content without all those sections and long lists of URLs.  Trouble is, I'm trying to stick as much as possible to core Moodle.

My course "simple2" format modifies the URLs and skips empty sections (sections which are primarily just a holder for URLs to the activities and resources) - but I can't find a way to invoke activities like book and give them a "don't go back to section 3 url, go forward to quiz id 245)

Otherwise I can just write a short navigation above each activity/resource, but that doesn't seem like a good long term plan silmänisku

Vastaus Sam Hemelryk

Re: Moodle 2 - how to set up breadcrumbs for a module page

yatin patel -
Thanks

In Moodel 2.9 Set Breadcrumb in change the code in lib/navigationlib .php

You can change the code in this line number 1031

view the code

$properties = array(
'key' => 'home',
'type' => navigation_node::TYPE_SYSTEM,
'text' => get_string('home'),
'action' => new moodle_url('https://google.com')
);

Vastaus yatin patel

Re: Moodle 2 - how to set up breadcrumbs for a module page

Richard Oelmann -
Kuva: Core developers Kuva: Plugin developers Kuva: Testers

Not sure what altering the link for the 'home' part of the breadcrumb has to do with this thread about applying the breadcrumbs for an activity/resource module? Probably not a good idea and certainly not a good idea to be editing core files without a very good reason!

The 'home' link of the breadcrumb already points to either the site home page or the user dashboard depending on the settings for the site, so why would you want to change that and confuse your users?