在 Orchard 中创建导航菜单项 [英] Creating a navigation menu item in Orchard

查看:50
本文介绍了在 Orchard 中创建导航菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个 Orchard 模块,并希望在启用该模块时在导航列表中显示一个项目.理想情况下,我希望能够在模块被禁用时删除该项目.

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. 当模块启用和禁用时,我应该连接到哪里?
  2. 如何以编程方式将菜单项添加到现有导航?

推荐答案

您可以为此实现 IMenuProvider 接口.示例实现可能如下所示:

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.

另一种方法是使用 IFeatureEventHandler 挂钩模块启用事件,并使用内容管理器创建带有 url 的菜单项并将它们添加到指定的菜单.我真的不推荐这种方法;您失去了对菜单项的控制(例如更新 url),它们可能会意外从菜单中删除,您必须知道要将它们添加到的菜单的名称,您受到更多限制(无法进行权限检查等).).

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

我假设您说的是出现在前端.如果您谈论管理菜单,请查看通常称为 AdminMenu.cs 的文件的几乎所有模块,大量示例:)

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

这篇关于在 Orchard 中创建导航菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆