Jump to content

Inject page at special position


Nico Knoll
 Share

Recommended Posts

I'm not sure and haven't tested, but should be something like this:

$p[$key] = $page;

Where $p is the PageArray and $key is the position. If you meant to set it as second page from root, then you would go with this:

$p = $pages->get(1)->children();
$p[1] = $page;

EDIT: It might also require to save parent page, moved page or both.

Link to comment
Share on other sites

If it's something you want to commit to the DB, you'll want to make sure that the parent doesn't already have a sortfield specified. If it does, then it's going to sort by that field in the tree automatically, and you'd have to account for that. But you would have had to specifically set a sortfield for that to be the case. By default, pages have a sortfield of 'sort', which just means it will sort by the 'sort' property of the child pages (an integer). That's what you want. In your case, you want to grab the first page and change its 'sort' property to be one less than it currently is, and then take the page you want to insert and give it the 'sort' value that the first page had:

<?php
// get the parent page you'll be working with
$parent = $pages->get('/some/parent/page/with/children/'); 

// get the first child of $parent
$first = $parent->child();

// create a new page that you want to insert (or grab some existing page)
$new = new Page();
$new->template = $templates->get('some-template');
$new->parent = $parent; 

// set its 'sort' property and save it 
$new->sort = $first->sort; 
$new->save(); 

// now save $first with it's sort being 1 less
$first->sort--;
$first->save(); 

That should insert the new page as being the second child of the $parent.

I also like using date/time fields for sorting, even if you don't need the date/time for anything else.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...