Creating a navigation menu item in Orchard

旧街凉风 提交于 2020-01-16 04:22:06

问题


I have written an Orchard Module and would like an item to appear in a Navigation list when the module is Enabled. Ideally, I would like to be able to remove the item when the Module is disabled.

  1. Where should I hook into to for when the module is enabled and disabled?
  2. How do I programmatically add a menu item to an already existing Navigation?

回答1:


You can implement the IMenuProvider interface for this. An example implementation might look something like this:

namespace Orchard.Bar {
    public class SuperMenuProvider : IMenuProvider {
        private readonly IOrchardServices _orchardServices;

        public SuperMenuProvider(IOrchardServices orchardServices) {
            _orchardServices = orchardServices;

            T = NullLocalizer.Instance;
        }

        public Localizer T { get; set; }

        public void GetMenu(IContent menu, NavigationBuilder builder) {
            string position = "10";
            builder.Add(T("Foo"), position, item => item.Url("http://foo.com").AddClass("someClass"));
            builder.Add(T("Bar"), position + ".1", item => item.Action("Index", "Foo", new { area = "Orchard.Bar" }));
            if (_orchardServices.Authorizer.Authorize(Orchard.Security.StandardPermissions.AccessAdminPanel)) {
                builder.Add(T("Secure FooBar"), position + ".2", item => item.Action("Index", "Secure", new { area = "Orchard.Bar" }));
            }
        }
    }
}

This will appear on all menus on the front end. You may want to put in the name of the menu you are targeting if you know for sure that is what it is called (default in Orchard is "Main Menu", people don't generally change it to be honest). This could be a little brittle, so you may want it customizable, either with a site setting or you could create a part that you attach to the menu content type that lets the admin specify whether to show your menu items on the said menu.

An alternative approach would be to hook into the modules enable event using IFeatureEventHandler and using the content manager to create menu items with urls and adding them to a specified Menu. I don't really recommend this approach; you lose control of the menu items (e.g. to update a url), they can be removed from the menu accidentally, you have to know the name of the Menu you are adding them to, you are more limited (cant do permissions checks etc.).

I assume you are talking about showing up on the front end. If you talking about the admin menu then check out pretty much any module for a file generally called AdminMenu.cs, plenty of examples :)




回答2:


The question doesn't specify what the module does so I guess we're to assume that it creates a content type. In that case you have (at least) two options:

  • In the Content Type's Content Definition go to Add Parts and add the Menu part. This will allow you to add a content item to a menu from the item's content editor.
  • From the Navigation menu choose the appropriate Menu and select add a Content Menu Item. Note that the content type must be set as "listable" in Content Definition in order for the items to be listed as a choice.

Disabling the module should remove the item from the navigation in either case.



来源:https://stackoverflow.com/questions/29235508/creating-a-navigation-menu-item-in-orchard

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!