Added menu item to the main menu through INavigationProvider but it won't show?
Asked Answered
A

1

5

Using Orchard cms 1.5.1 I have created a module which contains controller that fetches list from a web service. I want to add a menu item in main menu when this module is enabled. For that i have created MainMenu as follows:

public class MainMenu:INavigationProvider
{
    public Localizer T { get; set; }
    public String MenuName
    {
        get { return "main"; }
    }
    public void GetNavigation(NavigationBuilder builder)
    {
        builder.Add(menu => menu.Add(T("Fetched List"), "4", item => item.Action("Index", "FetchedList")));
    }
}

When my module is enabled, navigation won't show that menu item. Am i doing something wrong?

Antwanantwerp answered 6/8, 2012 at 11:54 Comment(4)
When you put a breakpoint in GetNavigation, does it get hit?Handal
GetNavigation in MainMenu doesn't get hit. Also, IEnumerable<MenuItem> BuildMenu(string menuName) in Orchard.UI.Navigation.NavigationManager gets hit only for 'admin' as menuName.Antwanantwerp
As there is no more Orchard.Core.Navigation.Services.MainMenuNavigationProvider which looks for "main" as explained by Piotr Szmyd in his article at szmyd.com.pl/blog/… i think it is obsolete to implement INavigationProvider to attach menu items to Main Menu. Should I use IMenuProvider instead?Antwanantwerp
Yes, for the main menu, things changed in 1.5 and you should base your code on the new menu item providers that come with the system.Handal
S
7

From Orchard 1.5.0 onwards, INavigationProvider isn't used to build menus on the front end (it is still used to build the admin menu for the Dashboard though). You need to implement either IMenuProvider or INavigationFilter. See this post on David Hayden's blog for some pointers. You can also find good examples in both Orchard.Projections, and Orchard.CulturePicker.

Stellular answered 6/8, 2012 at 15:32 Comment(5)
Implementing INavigationFilter to add menu items does not seem to be the proper way of doing it, as it is or seems like a filter. Orchard.Core.Navigation.Services.DefaultMenuProvider implements IMenuProvider as should be, but if i implement it and there are more than one menu roots, my module will attach items to all of them. Any suggestions?Antwanantwerp
Correct, IMenuProvider is used to add completely new items to the menu, whereas INavigationFilter is used to expand content items like NavigationQueryMenuItem into it's component parts (e.g. if you using a projection to populate a portion of the menu), so really it depends on how you want to inject your menu items. IMenuProvider.GetMenu() takes a menu parameter, can you not use that to differentiate between root menus?Stellular
Will try it. Thanks Matt and Bertrand for your valuable inputs.Antwanantwerp
@Antwanantwerp @Stellular Exactly - since 1.5 the code-based navigation providers had been split into INavigationProvider (for admin) and IMenuProvider (for all other menus). Usage of either one is the proper way of adding new items to the menu. INavigationFilter is used to alter the existing, already built menu (you can add items this way, of course, but it's just not the best practice).Ayakoayala
link to blog entry is brokenDemagogue

© 2022 - 2024 — McMap. All rights reserved.