Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Adrian 38 posts 117 karma points
    Sep 05, 2014 @ 17:00
    Adrian
    0

    Default sort position of new items.

    We want to use Umbraco's sort ordering to tweak ordering.

    But on our site, when we create an article, it is added to the end of the folder(node).

    Is there anyway to make it insert at the beginning of the folder?

    We want articles' default order to be newest first.

    It's made slightly worse on our site, because it's is using List view so new article appear on the last page...

  • John C Scott 473 posts 1183 karma points
    Sep 05, 2014 @ 17:55
    John C Scott
    0

    I'm familiar with this problem, I've had this before with news sections. There is no setting that I'm aware of that can invert the sort order. The best idea I can think of is this feels like a good case for a publishing event. Perhaps limited to the document type of your news article. This then would update the sort order on publish, and it may need to republish the parent too. A bit of a Friday afternoon idea.

  • Sören Deger 733 posts 2844 karma points c-trib
    Sep 10, 2014 @ 17:00
    Sören Deger
    1

    Hi Adrian,

    there is a package for this :

    http://our.umbraco.org/projects/website-utilities/unewsmanager

    The package contains an eventhandler for auto sort of a special document type. You can use this or you can customize this too.

     

    Maybe another solution is to use the document sorter package (http://our.umbraco.org/projects/developer-tools/document-sorter) but I don't know if it also supports V7.

     

    I hope this help you.

     

    Sören

  • Adrian 38 posts 117 karma points
    Sep 10, 2014 @ 17:39
    Adrian
    0

    Thanks for that - I've written my own event handler for now.

  • Ridhika 5 posts 25 karma points
    May 18, 2015 @ 10:58
    Ridhika
    0

    @Adrian: Could you please share your code snippet with us?

  • Adrian 38 posts 117 karma points
    May 18, 2015 @ 11:13
    Adrian
    0

    Unfortunately its not 'quite' working, and I've not had time to revisit it yet...

    Notice the early return in ContentService_Saved

    public class KeepNewContentAtTop : ApplicationEventHandler
        {
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                Umbraco.Core.Services.ContentService.Saved += ContentService_Saved;
                Umbraco.Core.Services.ContentService.Saving += ContentService_Saving;
            }
    
            void ContentService_Saving(Umbraco.Core.Services.IContentService sender, Umbraco.Core.Events.SaveEventArgs<Umbraco.Core.Models.IContent> e)
            {
                foreach (var a in e.SavedEntities)
                {
                    var dirty = (IRememberBeingDirty)a;
                    var isNew = dirty.WasPropertyDirty("Id");
    
                    if (isNew)
                    {
                        a.SortOrder = 0;
                    }
    
                }
            }
    
            private void ContentService_Saved(Umbraco.Core.Services.IContentService sender, Umbraco.Core.Events.SaveEventArgs<Umbraco.Core.Models.IContent> e)
            {
                return;
                int idToMoveToTop = -1;
    
    
                foreach (var a in e.SavedEntities)
                {
                    var dirty = (IRememberBeingDirty)a;
                    var isNew = dirty.WasPropertyDirty("Id");
    
                    if (isNew)
                    {
                        LogHelper.Debug(this.GetType(), "New article:" + a.Name);
    
                        var parent = sender.GetById(a.ParentId);
    
                        if (parent.HasProperty("newestFirst"))
                        {
                            var newestFirst = parent.Properties["newestFirst"].Value as int?;
                            if (newestFirst == 1)
                            {
                                idToMoveToTop = a.Id;
                            } 
                        }
                    }
                } // END foreach (var a in e.SavedEntities)
    
                if (idToMoveToTop != -1)
                {
                    var childItems = sender.GetChildren(idToMoveToTop);               
    
                    foreach( var a in childItems){
                        LogHelper.Debug(this.GetType(), a.Name + ":" + a.SortOrder);
                        if (a.Id == idToMoveToTop)
                        {
                            a.SortOrder = 0;
                            LogHelper.Debug(this.GetType(),"Found " + a.SortOrder.ToString());
                        }
                    }
    
                    childItems = childItems.OrderBy(i => i.SortOrder);
                    sender.Sort(childItems,raiseEvents:false);
                }
            }
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft