Add Page to Bottom of Parent's List of Children

50 views
Skip to first unread message

Jackson Fisher

unread,
Aug 24, 2021, 4:06:53 PM8/24/21
to KalikoCMS Developer Forum
Hi Fredrik, first of all thanks for creating this, it's incredible and I've been able to integrate it into our LMS pretty flawlessly and even got the file and folder stuff integrated with Amazon S3 so that the media content is stored in the cloud with version control and etc.

Unrelated to any changes I've made: I'm having a simple issue with being able to add a child page to a parent and have it populate at the end of the list rather than the beginning. I see in the jstree that there's an option for first or last or etc. and I've looked through EditablePage.cs but I can't find where I'd control this from the Kaliko side so as to make sure the sort index is properly set (and don't want to edit source code any more than I have to).

I've added: 

DefaultChildSortDirection = SortDirection.Ascending, DefaultChildSortOrder = SortOrder.SortIndex

To the parent page type as well as this page type itself (and tried different orders  like alphabetical and directions in case it just doesn't start with the proper index compared to the rest) and no matter what when you initially add a page it starts at the top above the other children like so:

chrome_UlnEKhg6Vr.png

So I am assuming the sorting is referring to the pages that are rendered for site visitors and not the content manager. 

I feel like I'm missing some simple setting or making some rookie mistake so before I go more crazy I was going to ask if you could point me in the right direction. 

Fredrik Schultz

unread,
Aug 25, 2021, 2:11:01 PM8/25/21
to KalikoCMS Developer Forum
Hi,
I'm really glad to hear that you've found the CMS useful! Always interesting to hear how it's being used, thank you.

Regarding your question there's a couple of possible answers (a little depending on how you want it to work).

When a new page is created in the editor the current node of the page tree is refreshed and repopulated with the child pages, including the newly created one. So what you should see is always a reflection of the sort order.

When the parent have a sort order set to SortIndex newly created pages will get the index 0 until they are manually reordered within the page tree, thus ending up first in the list.

Option 1) is to change the sort order to alphabetical or based on creation date. The default settings will only be applied to new pages created after it has been set, for pages already existing it has to be done by expanding "+ Show advanced options" when editing the parent page and changing the value:
Capture.PNG

Option 2) If you want to keep the ability to manually reorder children (which you loose if you chose to sort by anything other than "Sort index") you could add a bit of code to the save event to change the index value.

This is done by attaching an event handler to the page factory: 
PageFactory.PageSaved += PageSavedHandler;

In the event handler you can then change the sort index like this:
    private void PageSavedHandler(object sender, KalikoCMS.Events.PageEventArgs e) {
        var page = e.Page;

        // Only run for the first version of the page
        if(page.CurrentVersion == 1) {
            // Get the number of siblings and calculate the last possible position
            var siblings = PageFactory.GetChildrenForPage(page.ParentId, KalikoCMS.Core.PublishState.All);
            var lastPosition = siblings.Count - 1;

            // If there are more siblings...
            if(lastPosition > 0) {
                // ...move our saved page to the last position
                PageFactory.ReorderChildren(page.PageId, page.ParentId, lastPosition);
            }
        }
    }

Notice that the sort index must be in the range of 0 to number of children - 1 otherwise it will fail.

It might also need a bit more logic (like checking if it's a particular content type etc.), but I hope it can serve as an example of what can be done.

Jackson Fisher

unread,
Aug 30, 2021, 11:41:52 AM8/30/21
to KalikoCMS Developer Forum
Thanks for your quick reply, and the multiple options.

I've overridden EditPage.aspx.cs with our own to add the PageSavedHandler you posted (adding it just before editablePage.Save() in SavePropertiesForPage) and it is definitely working, it is added in the database with the proper sort index.

I am still having a problem with it: even on a page reload (or full cache clear and hard reload) the page will sit at the top of the list of children. If I stop Visual Studio and run it again (or add another page) it will refresh and the page will properly be at the bottom. 

Maybe I've added the handler in the wrong place? (In my ignorance, as I said, I had to override the code behind for EditPage) Or there's a page refresh I could call? 

I know you don't have a lot of time to dedicate to this project at the moment, the problem is mostly resolved (and if not for our content manager's likely complaints I might just leave it at this) and I can't thank you enough for your help thus far.



Fredrik Schultz

unread,
Sep 4, 2021, 5:22:51 AM9/4/21
to KalikoCMS Developer Forum
I'm a little bit curious to the behavior that it's stored correctly but not shown as such immediately, but my guess is that it's related to the life cycle of EditPage.aspx.

I would recommend moving the event handler to some startup routine, like the web projects Startup-class:
public void Configuration(IAppBuilder app) {
   // ...
   PageFactory.PageSaved += PageSavedHandler;
}

private void PageSavedHandler(object sender, KalikoCMS.Events.PageEventArgs e) {
   // ...
}

That would hopefully that would work better!

Jackson Fisher

unread,
Sep 15, 2021, 10:10:17 AM9/15/21
to KalikoCMS Developer Forum
Thanks for your reply, and sorry for my late reply, had to jump to another project for a client of ours.

Unfortunately that didn't work but it's a much better place to add the handler so thank you for that.

I will circle back around to this occasionally and keep hammering at it, it's not the end of the world if I can't fix it. If I find a fix I'll post it here for posterity.

Reply all
Reply to author
Forward
0 new messages