General developer forum

How to build a main menu using the navigation block

 
Picture of Eric Millin
Re: How to build a main menu using the navigation block
 

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:

 
Average of ratings: Useful (9)
Picture of Javier Fernandez
Re: How to build a main menu using the navigation block
 

Hi Eric,

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

 
Average of ratings: -
Picture of Mark Johnson
Re: How to build a main menu using the navigation block
Core developersParticularly helpful MoodlersPlugin 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.

 
Average of ratings: Useful (1)
Picture of Javier Fernandez
Re: How to build a main menu using the navigation block
 

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

 
Average of ratings: -
Picture of Jamie Brett
Re: How to build a main menu using the navigation block
 

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.

 
Average of ratings: -
Picture of Javier Fernandez
Re: How to build a main menu using the navigation block
 

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

 
Average of ratings: Useful (1)
Picture of Eric Millin
Re: How to build a main menu using the navigation block
 

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.
 
Average of ratings: Useful (2)
Picture of Tuan Tran
Re: How to build a main menu using the navigation block
 

Thank you so much. It works smile

 
Average of ratings: -
Picture of Jamie Brett
Re: How to build a main menu using the navigation block
 

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

 
Average of ratings: -
Picture of Eric Millin
Re: How to build a main menu using the navigation block
 

You should be able to extend the nav block, then recursively loop through all of the nodes starting from "Home" and pluck out the ones you want to remove.

 
Average of ratings: -
Picture of Lavanya Manne
Re: How to build a main menu using the navigation block
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

 
Average of ratings: -
Rosario playing soccer
Re: How to build a main menu using the navigation block
Plugin developers

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

 
Average of ratings: -
Picture of Stephen D
Re: How to build a main menu using the navigation block
 

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?

 
Average of ratings: -
Rosario playing soccer
Re: How to build a main menu using the navigation block
Plugin developers

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

 
Average of ratings: -
Picture of Sabin Pradhan
Re: How to build a main menu using the navigation block
 

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


 
Average of ratings: -
Picture of Richard Oelmann
Re: How to build a main menu using the navigation block
Core developersParticularly helpful MoodlersPlugin developersTesters

You dont need any php or code changes to rename this - just edit the language pack so that it says what you want smile

Site Administration > Language > Language customisation

Richard

 
Average of ratings: -
Picture of Sabin Pradhan
Re: How to build a main menu using the navigation block
 

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

 
Average of ratings: -
Picture of nitin sharma
Re: How to build a main menu using the navigation block
 

go to index page of your moodle root i.e index.php

add code over there

if(is_loggedin())

{

$url = $CFG->wwwroot."/mod/lesson/view.php?id=1";

redirect($url);

}

 
Average of ratings: -
Picture of duy vu
Trả lời: Re: How to build a main menu using the navigation block
 

you do it follow image

place folder lib

 
Average of ratings: -