0

在 Umbraco 6 中,当您创建一个新节点时,它被放在底部。
如果您希望它位于顶部,则必须手动对其进行排序。

如何让新节点默认出现在顶部?

4

2 回答 2

1

您可以创建一个事件处理程序,在创建新节点时更改节点的排序顺序。有关实现您自己的处理程序的更多详细信息,请参阅应用程序启动事件和事件注册。

未经测试的粗略示例,我相信您可以做得更优雅,但应该为您指明正确的方向:

public class YourApplicationEventHandlerClassName : ApplicationEventHandler
{
    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
      ContentService.Created += ContentServiceCreated;
    }

    private void ContentServiceCreated(IContentService sender, NewEventArgs<IContent> e)
    {
      var cs = ApplicationContext.Current.Services.ContentService;
      var content = e.Entity;
      var parentNode = content.Parent();

      content.SortOrder = parentNode.Children().OrderBy(n => n.SortOrder).First().SortOrder - 1;
      cs.Save(content);
    }
}
于 2014-02-26T08:09:44.450 回答
1

ContentService.Created活动对我不起作用。进行了一些战斗,但在v7其中Umbraco,我使用了该ContentService.Saved事件,并对脏属性进行了一些双重检查,以确保您不会陷入无限循环:

    private void ContentSaved(IContentService sender, SaveEventArgs<IContent> e)
    {
        foreach (var content in e.SavedEntities)
        {
            var dirty = (IRememberBeingDirty)content;
            var isNew = dirty.WasPropertyDirty("Id");
            if (!isNew) return;

            var parentNode = content.Parent();
            if (parentNode == null) return;
            var last = parentNode.Children().OrderBy(n => n.SortOrder).FirstOrDefault();
            if (last != null)
            {
                content.SortOrder = last.SortOrder - 1;
                if (content.Published)
                    sender.SaveAndPublishWithStatus(content);
                else
                    sender.Save(content);
            }
        }
    }

public class AppStartupHandler : ApplicationEventHandler
{
    protected override void ApplicationInitialized(UmbracoApplicationBase umbracoApplication,
        ApplicationContext applicationContext)
    {
        ContentService.Saved += ContentSaved;
    }
}
于 2016-07-25T14:03:22.810 回答