Jump to content

create pages via API, how add to the top of tree?


horst
 Share

Recommended Posts

Hi,

I'm sure the answer is here to find in forum, but after searching for a while now, I gave up and want to ask for it.

How can I add / sort new (via API) created childpages to the top of the parent-tree?

I want to have a descending sort order in the backend.

Link to comment
Share on other sites

thanks Adrian!

this solution from soma works partially: http://processwire.com/talk/topic/1428-create-new-child-as-top-sibling-rather-than-bottom/#entry12845

1)

It does the right sorting if parent sorting is set to none in template. But than I cannot do manually sort by dragging. I get an JS-Message:"Your sort was not saved because these pages are automatically sorted by ..." BUT that's wrong! It was set to this before I have changed the sort to none. Don't know where the error comes from. I have searched a mysql dump of whole DB, but there isn't a sort-setting in the DB for my parent-folder-template!

Could this be a bug? From where is the JS-Message generated?

-----

2)

When setting the parent template child-sorting to 'Manual drag-n-drop' I can do a manually sorting, but Somas script doesn't work right anymore!  :(

Link to comment
Share on other sites

I haven't tested Soma's solution, so not sure about those bugs, but do you need manual sorting in the back-end after creation? If not, wouldn't Ryan's suggestion of setting the sort order to "date created" in reverse work for you?

I am guessing you do want the manual sorting, but thought I'd mention it, just in case :)

Link to comment
Share on other sites

the base-sorting could be something like -dateCreated but I also want to be able to break that rule and drag some pages manually around.

therefor somas solution to emulate -dateCreated like it works with childrens-sortorder set to none would be perfect!

Link to comment
Share on other sites

Hey horst,

I just tested Soma's code with the sorting set to none and it seems to be working fine. The new page was added to the top of the children, but I could still manually sort without any error. I am running the latest dev - maybe a couple of days old.

Let me know if there is any specific scenario I can help you test.

Link to comment
Share on other sites

@adrian: before it does not work, e.g if the sort-settings is none or manual :(

There was a entry in my DB table pages_sortfields for the treeparent-page that get not altered when changing the template setting ??

After I have deleted this row (the only one in that table) this page reflects the selected template sortsetting again.

I want to add multiple pages with an importer script. Somas script works with one single page for me, but not with multiple pages.

When I try to add 7 pages and after I'm finished I output $p->sort and $p>customId in a foreach loop, I get this:

0 :: jh2140
1 :: jh2134
1 :: jh2135
1 :: jh2136
1 :: jh2137
1 :: jh2138
1 :: jh2139

I have sort setting 'Manual drag-n-drop'.

I have dev branch from last friday.

Edited by horst
Link to comment
Share on other sites

Ok, I have good working solution :) I think:

set the tree-parent-page sort-setting to "Manual drag-n-drop" when it is empty. (empty == has no children now!) Then with the importer script I use this code:

$parent = wire('pages')->get('template=TreeParent');  // get your tree-parent-page
$first = $parent->child;    // get the first child

$p = new Page();
...
$p->sort = (0!=$first->id) ? $first->sort -1 : 99999;
$p->save();

that way I get this output with my foreach loop:

99991 :: jh2142
99992 :: jh2141
99993 :: jh2140
99994 :: jh2139
99995 :: jh2138
99996 :: jh2137
99997 :: jh2136
99998 :: jh2135
99999 :: jh2134

and after some manual changes (2134 to top, and 2142 to bottom, and 2135 above 2140) and adding one new page to it with the importer script, I get this:

99990 :: jh2143
99991 :: jh2134
99992 :: jh2141
99993 :: jh2135
99994 :: jh2140
99995 :: jh2139
99996 :: jh2138
99997 :: jh2137
99998 :: jh2136
99999 :: jh2142

You only need to set the startvalue high enough, I think. :)  [ max can be 2.147.483.647 ]

Edited by horst
  • 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...