How to build a main menu using the navigation block

Re: How to build a main menu using the navigation block

Eric Millin -
Колькасьць адказаў: 18

I have just discovered how to do this and I figured I'd post it incase someone else is looking for help.

In initialise(), lines 1114-1123 of /lib/navigationlib.php, Moodle searches /local for plugins that extend the navigation menu.  If it finds one, it passes a $global_navigation object.  So here's what you do:

1) Create the directory /wwwroot/local/myplugin

2) Create a new file called "lib.php" in the directory.

3) Moodle looks for a function name that matches "myplugin" + "_extends_navigation".  In our case, add the following function to your file:

function myplugin_extends_navigation(global_navigation $navigation) {
$nodeFoo = $navigation->add('Foo');
$nodeBar = $nodeFoo->add('Bar');
}

Reload the main page and you should now see this:

У адказ на Eric Millin

Re: How to build a main menu using the navigation block

Javier Fernandez -

Hi Eric,

Thanks for sharing it with us. Unfortunately, it doesn´t work work us. This is what I get. Any advice please?

У адказ на Javier Fernandez

Re: How to build a main menu using the navigation block

Mark Johnson -
Выява Core developers Выява Particularly helpful Moodlers Выява Peer reviewers Выява Plugin developers

Hi Javier,

This may seem like an odd question, but is that code in your lib.php file enclosed in <?php and ?> tags? It looks to me like they've been omitted, so it's just printing the contents of the file.

У адказ на Mark Johnson

Re: How to build a main menu using the navigation block

Javier Fernandez -

Thanks Mark,

It was that. As you can see I dont know anything about php. Is there any manual for moodle developpers I could have a look?

I just installed it and need to tweak it a bit.

Thanks,

Javier

У адказ на Eric Millin

Re: How to build a main menu using the navigation block

Jamie Brett -

Eric,

Your 'navigationlib.php' solution is a good one, and looks close to a solution for something we'd like to do. We'd like to hide from students the Navigation block 'My profile' node and sub nodes. I don't know php enough to intelligently edit navigationlib.php to hide that node. Would you make a suggestion? Thank you in advance.

У адказ на Jamie Brett

Re: How to build a main menu using the navigation block

Javier Fernandez -

Hi Eric,

What about if you want to link "Bar" to an external website?

<?php

function myplugin_extends_navigation(global_navigation $navigation) {

$nodeHelp = $navigation->add('Help');

$nodeHelp->add('Bar');}

?>

Thanks in advance,

Javier

У адказ на Javier Fernandez

Re: How to build a main menu using the navigation block

Eric Millin -

Here are some snippets of what I've done so far.  This is experimentation and I'm new to PHP, so take it for what it's worth.

function custom_nav_extends_navigation(global_navigation $navigation) {

//get the "Home" node

$nodeHome = $navigation->children->get('1')->parent;

//Rename it

$nodeHome->title= 'Foo';
$nodeHome->key='Foo';
$nodeHome->text = 'Foo';

//Create a child node
$nodeAwesome = $navigation->add('10 Reasons We're Awesome');

//Add children to nodeAwesome. Pretend we have a list "$myList" of links to add.
for ($i = 0; $i <= count($myList); $i += 1)
$nodeAwesome->add($myList[$i], new moodle_url('/path/to/file/'.$myList[$i]), null, null, $myList[$i]);

//force the node open
$nodeAwesome->forceopen = true;
}

You can put whatever url you like when you add a node.
У адказ на Jamie Brett

Re: How to build a main menu using the navigation block

Jamie Brett -

Just noting that my previous post in this thread is a little off topic, and addressed more specifically in this thread:

http://moodle.org/mod/forum/discuss.php?d=170934#p755095

У адказ на Eric Millin

Re: How to build a main menu using the navigation block

Lavanya Manne -
Выява Plugin developers

Hi Eric Millin,

Could I know how could we call only the courses section from navigation block and display in my custom block. I need a particular function for courses.

 

Thanks

Lavanya

У адказ на Eric Millin

Re: How to build a main menu using the navigation block

Rosario Carcò -

Thanks a lot for this. I developed a sitenavigation for Moodle 1.9 you will find in the new plugins section and in these forums. I implemented a lot of admin options to tailor what should be displayed and you can use the latest version with popUp-windows or inline display. I might think about porting some features to 2.x one day.

Rosario

У адказ на Eric Millin

Re: How to build a main menu using the navigation block

Stephen D -

Thanks, I have followed this solution and managed to generate custom navigation items. Do you know how I would be able to only show these for teachers? For example to add shortcuts to commonly used pages that I only want teachers to have access to?

У адказ на Stephen D

Re: How to build a main menu using the navigation block

Rosario Carcò -

YES, this is sort of things I do in my own myCourses Block. You have to check the roles/capabilities and show only if the logged in user meets those conditions. You will find examples here in the forums or when diving into the code of myCourses Block.

Rosario

У адказ на Eric Millin

Re: How to build a main menu using the navigation block

Sabin Pradhan -

Hi Eric,

I just look at your code of adding the node to navigation.But I like to rename the Courses node to something else.I am also new babies to php.

Can you suggest me how to do that.I had attached the screen shot of actually what I want to achieve.

 

Thanks for your help.

Sabin

Укладаньне Capture3.PNG
У адказ на Sabin Pradhan

Re: How to build a main menu using the navigation block

Richard Oelmann -
Выява Core developers Выява Particularly helpful Moodlers Выява Plugin developers Выява Testers

You dont need any php or code changes to rename this - just edit the language pack so that it says what you want усьмешка

Site Administration > Language > Language customisation

Richard

У адказ на Richard Oelmann

Re: How to build a main menu using the navigation block

Sabin Pradhan -

Hi Richard,

You are right.I already change the name of node and others by using Language customisation.

Well it will be great if you can also help me in another issue.

I would like to redirect eh user to particular page after they successfully log in for e.g to this url :http://localhost/moodle/mod/lesson/view.php?id=1

Do you know how to do it.

Thanks

Sabin