How to build a main menu using the navigation block

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

by Eric Millin -
Number of replies: 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:

Average of ratings: Useful (11)
In reply to Eric Millin

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

by 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?

In reply to Javier Fernandez

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

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of 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.

Average of ratings: Useful (2)
In reply to Mark Johnson

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

by 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

In reply to Eric Millin

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

by 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.

In reply to Jamie Brett

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

by 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

Average of ratings: Useful (1)
In reply to Javier Fernandez

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

by 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.
Average of ratings: Useful (2)
In reply to Eric Millin

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

by Tuan Tran -

Thank you so much. It works smile

In reply to Jamie Brett

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

by 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

In reply to Jamie Brett

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

by Eric Millin -

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.

In reply to Eric Millin

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

by Lavanya Manne -
Picture of 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

In reply to Eric Millin

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

by 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

In reply to Eric Millin

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

by 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?

In reply to Stephen D

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

by 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

In reply to Eric Millin

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

by 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

Attachment Capture3.PNG
In reply to Sabin Pradhan

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

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

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

In reply to Richard Oelmann

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

by 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

In reply to Sabin Pradhan

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

by nitin sharma -

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);

}

In reply to Sabin Pradhan

Trả lời: Re: How to build a main menu using the navigation block

by duy vu -

you do it follow image

place folder lib