Orchard cms将当前窗口小部件类型添加到自定义模块迁移

时间:2012-08-05 12:04:38

标签: orchardcms

我确信之前一定要问过这个问题。但我一直找不到任何有用的东西。

我想创建一些小部件(现有小部件类型,菜单,投影),作为自定义模块安装的一部分。 aslo一些查询和Projection页面。

以及新菜单(果园1.5.1),我在设置模块中找到了代码中的菜单部分

...

    // create a project menu
    var projectMenu = _menuService.Create("new Menu");

    // assign the project items to all current menu items
    foreach (var menuItem in _menuService.Get().Where(x => x.ContentItem.ContentType == "Project"))
    {
        // if they don't have a position or a text, then they are not displayed
        if (string.IsNullOrWhiteSpace(menuItem.MenuPosition) || string.IsNullOrEmpty(menuItem.MenuText))
    {
       continue;
    }
    menuItem.Menu = projectMenu.ContentItem;

我可以在迁移中添加一些内容来添加小部件和投影吗?

我一直在查看Orchard Recipe模块,看看那里有代码来执行设置小部件的命令。但我不确定如何最好地利用这个......

创建配方实例管理和执行整个配方看起来有点复杂。

1 个答案:

答案 0 :(得分:0)

在您可以使用的地方有一些非常有用的看似无证的服务。

使用以下构造函数

public class MigrationProjectType : DataMigrationImpl 
    {
        private readonly IMenuService _menuService;
        private readonly IContentManager _contentManager;
        private readonly IQueryService _queryService;
        private readonly IWidgetsService _widgetsService;

        public MigrationProjectType(IMenuService menuService, IContentManager contentManager,  IQueryService queryService, IWidgetsService widgetsService)
        {
            _menuService = menuService;
            _contentManager = contentManager;
            _queryService = queryService;
            _widgetsService = widgetsService;
        }
...
}

Auto fac将负责实例化服务

然后

public int UpdateFrom2()
        {
            // create a project menu
            var projectMenu = _menuService.Create("Projects Menu");

            // assign the project items to all current menu items
            foreach (var menuItem in _menuService.Get().Where(x => x.ContentItem.ContentType == "Project"))
            {
                // if they don't have a position or a text, then they are not displayed
                if (string.IsNullOrWhiteSpace(menuItem.MenuPosition) || string.IsNullOrEmpty(menuItem.MenuText))
                {
                    continue;
                }
                menuItem.Menu = projectMenu.ContentItem;
            }

            // create layer part 
            _widgetsService.CreateLayer("SideBar", "desc", "url(\"~\")");

            var defaultLayer = _widgetsService.GetLayers().First(x => x.Name == "Default");

            var widget =  _widgetsService.CreateWidget(defaultLayer.Id, "MenuWidget", "Projects Menu", "2", "Navigation");

            var menuWidget = widget.As<MenuWidgetPart>();
            menuWidget.Record.Menu = projectMenu.ContentItem.Record;

            _contentManager.Publish(menuWidget.ContentItem);

            return 3;
        }

查询有类似的服务,但我不知道如何为查询添加过滤器,以进行投影......

更新

继承人如何添加查询和过滤器

public int UpdateFrom7()
        {


            // create a project menu
            var projectMenu = _queryService.CreateQuery("My projects 3");

            var form = new Form { ContentTypes = "Project" };
            var xmlSerializer = new XmlSerializer(form.GetType());
            StringWriter sww = new StringWriter();
            XmlWriter writer = XmlWriter.Create(sww);
            xmlSerializer.Serialize(writer, form);
            var state = sww.ToString();


            projectMenu.FilterGroups[0].Filters.Add(new FilterRecord
            {
                Category = "Content",
                Description = "My filter",
                Position = 0,
                State = state,
                Type = "ContentTypes"
            });

            _contentManager.Publish(projectMenu.ContentItem);


            return 8;
        }

[Serializable]
    public class Form
    {
        public string Description { get; set; }
        public string ContentTypes { get; set; }

    }