Injecting menu items into front end menu (Orchard 1.5)

Topics: Writing modules
Aug 8, 2012 at 1:07 PM

Hello everyone,

I`m trying to figure out how to correctly inject a Menu Item inside the front-end menu of Orchard by implementing IMenuProvider inside my module.

The main reason behind this approach is we need to hide certain menu items depending on the User`s Permissions so by doing it like this I think we will be able to simply add a Permission condition and display only the appropriate menus (please let me know if I`m wrong about this)

Here`s what I have so far:

    public class MainMenu : IMenuProvider
    {
        public Localizer T { getset; }
        private readonly IContentManager _contentManager;
 
        public MainMenu(IContentManager contentManager)
        {
            _contentManager = contentManager;
        }
 
        public void GetMenu(IContent menu, NavigationBuilder builder)
        {
            builder.Add(T("Clauses"), "1", item => item.Url("~/Clauses"));
        }
    }

It`s working but for some reason my "Clauses" menu item shows up twice instead of only once.

  1. As far as I can tell from debugging, the GetMenu( ) method is only being called once
  2. I checked to be sure and I don`t have a manually defined "Clauses" menu item in my menu
  3. I only have a single menu defined in the Navigation Module (the default Main Menu)

I`ll be working on that this morning, I`ll post more details when I get any.

Thanks in advance!

Aug 8, 2012 at 1:16 PM

I just figured out why it`s displaying my menu item twice:

Inside MenuWidgetPartDriver.cs in the Display() Method, we found the following code at line 61:

                var localized = new List<MenuItem>();
                foreach(var menuItem in menuItems) {
                    // if there is no associated content, it as culture neutral
                    if(menuItem.Content == null) {
                        localized.Add(menuItem);
                    }
 
                    // if the menu item is culture neutral or of the current culture
                    if (String.IsNullOrEmpty(menuItem.Culture) || String.Equals(menuItem.Culture, currentCulture, StringComparison.OrdinalIgnoreCase)) {
                        localized.Add(menuItem);
                    }
                }

Now the two items I add manually have menuItem.Content == null AND menuItem.Culture == null which means they get added into localized once at the first if() and once at the second if(). 

I`ll see if I can define either of those so my MenuItems get added only once.