Nico Knoll Posted November 12, 2011 Share Posted November 12, 2011 Hi, I want to inject a page via API at the second child position of a page in my page tree. Is there a way to set a special position via $page? Greets, Nico Link to comment Share on other sites More sharing options...
apeisa Posted November 12, 2011 Share Posted November 12, 2011 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 More sharing options...
ryan Posted November 12, 2011 Share Posted November 12, 2011 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. 1 Link to comment Share on other sites More sharing options...
Nico Knoll Posted November 13, 2011 Author Share Posted November 13, 2011 Thanks, Ryan, that's working! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now