Remove items from navigation block?

Re: Remove items from navigation block?

by Csaba Gloner -
Number of replies: 9

I got help so here the answer in case it is important for someone else:

With 

.block_navigation .type_custom.depth_3:nth-child(5) {

    display: none;

}

any particular menu item can be removed.


In reply to Csaba Gloner

Re: Remove items from navigation block?

by vahid mohammadi -
In reply to vahid mohammadi

Re: Remove items from navigation block?

by Jon Bolton -
Picture of Particularly helpful Moodlers Picture of Testers

This appears to work. Use with caution though as it removes the whole participants menu - including sub-items including blogs and notes - for ALL users, including Admin:


.block_navigation .type_unknown.depth_4:nth-child(1) {
    display: none;
}
In reply to Jon Bolton

Re: Remove items from navigation block?

by Doyle Lewis -

CSS (especially with nth-child) is a dangerous way to handle this, since the ordering can change pretty easily. If you have a local module this is pretty easy to do with PHP, and then the item is never even rendered.

function local_module_extend_navigation(global_navigation $nav)
{
    if ($participants = $nav->find('participants', navigation_node::TYPE_CONTAINER)) {
    	$participants->remove();
    }
}
In reply to Doyle Lewis

Re: Remove items from navigation block?

by Grant Colloty -

Please bear with me I'm a Moodle noob and trying to find my way around. Would that function get placed in the lib.php file in the theme? I'm using the Klass theme and customising it to my client's needs.


I was also wondering if I there is a way enforce the string values within the data-keys attributes so that I don't get duplicate values. What I'd like to do is prevent all the course sections from rendering in the nav drawer but keep the course links and set their data-key value to the course short name so I can target them in my css and apply icons to them for a compact version of the nav drawer for mobile devices (see screenshot).


Ideally I'd like to handle this through the theme and not modify core files. Any help would be greatly appreciated.

Attachment moodle.PNG
In reply to Grant Colloty

Re: Remove items from navigation block?

by Doyle Lewis -

I don't believe it can go in a theme, but create a new local module and put it in the lib.php in there. You can try putting it in the lib.php in the theme. Just change local_module to theme_klass and see what happens.

I don't think you're going to be able to change the data that gets sent out. For getting a customized nav like that you're probably going to need to write something completely custom.

One thing I have noticed is there are a number of menu items that Moodle doesn't tag correctly, so they can't be pulled out using this method, you need to use JavaScript to match the text of the link to be able to remove them. If I ever have free time I'll have to submit a PR to fix them.

In reply to Doyle Lewis

Re: Remove items from navigation block?

by Guillermo Biot -

Hello Doyle,

Would this remove the item from the navigation drawer of boost theme? I need to add a link to the "My courses" item and I was wondering if I could do it following your approach

function local_module_extend_navigation(global_navigation $nav)
{
    if ($mycourses = $nav->find('mycourses', navigation_node::TYPE_CONTAINER)) {
    	$mycourses->addlink(...); //<-- how could I add a link to an existing item???
    }
}
Thank you
In reply to Guillermo Biot

Re: Remove items from navigation block?

by Doyle Lewis -

Yep, that works, but the method to call is just add(). Here is some similar code I have to add a link to the main course menu.

$coursenode = $PAGE->navigation->find($COURSE->id, navigation_node::TYPE_COURSE);
if ($coursenode) {
$coursenode->add('Build LTI Cartridge', new moodle_url('/course/builder.php', ['id'=> $COURSE->id]), navigation_node::TYPE_CUSTOM, null , null, new pix_icon('a/download_all' , ''));
}
In reply to Jon Bolton

Re: Remove items from navigation block?

by Guillermo Biot -

Maybe I'm missing something but since moodle 3.2 you can use the data-key attribute in the css selector to avoid using nth-child(). For instance:

#nav-drawer [data-key="calendar"] {

  display: none;

}

will hide the calendar from the drawer.

You can also match an item by the content of its href attribute, for instance:

#nav-drawer a[href*="course/view.php"]

will match all courses in the drawer because the link points to a url containing "course/view.php".

Average of ratings: Useful (3)