Jump to content

Set order of pages in PageTable field via API


alexcapes
 Share

Recommended Posts

I have a PageTable field that is updated via a custom module.

Pages are updated by deleting them then re-adding but crucially need be re-added in the same order in the PageTable field.

If I re-add pages, they go to the bottom of the field (it's a manual drag n drop setting).

Is there a way to re-add where the page was previously? Or at the very least I need to maintain the first page in the field. It has a checkbox ticked if it's first so the re-added page could use that to test whether it needs to go first.

I just don't know how to set the order of pages in a PageTable field via the api.

Any help here much appreciated.

 

 

Link to comment
Share on other sites

21 minutes ago, adrian said:

You need to set the "sort" property of each item, so if you keep track if their initial sort value you can set them manually when you add them back in.

Would you mind expanding on this Adrian? How would I set the sort property of each item in the PageTable field?

 

 

Link to comment
Share on other sites

45 minutes ago, alexcapes said:

Would you mind expanding on this Adrian? How would I set the sort property of each item in the PageTable field?

Sure - perhaps you could show me the code you have so far so it will be easier to modify to add the sort value back in.

Link to comment
Share on other sites

I've actually created a working solution (which I'm sure can be improved upon but it works for now).

I've made a simple module that populates a field ('edition_sort') on each PageTable page with it's order number:

$i = 1;
$editions = $page->editions;
foreach($editions as $edition) {

$edition->of(false);
$edition->edition_sort = $i;
$edition->save();

$i++;
}

I can then get this field ('edition_sort') value from the page being deleted and add it to the newly created page, then sort the PageTable by the field.

It all works but I'm sure there's a much neater way of doing it! 

  • Like 1
Link to comment
Share on other sites

1 hour ago, adrian said:

You should be able to use the built-in "sort" field exactly the same way.

I think "sort" is the sort order of the pages as they appear in the tree, which may be different than the order they appear in the PageTable field.

  • Like 3
Link to comment
Share on other sites

7 minutes ago, Robin S said:

I think "sort" is the sort order of the pages as they appear in the tree, which may be different than the order they appear in the PageTable field.

Wow - you are completely right. It's so weird that the sort db field for the page table field just matches the sort value of the actual pages. When you re-order the entries in the page table field it seems to recreate all the rows in the new order, but without changing the "sort" field value. I wonder if this was intentional or an oversight?

  • Like 1
Link to comment
Share on other sites

8 minutes ago, adrian said:

It's so weird that the sort db field for the page table field just matches the sort value of the actual pages.

I think that's just a coincidence if you add several pages one after the other without reordering them in the field. But because a page that's in a PageTable field can reside anywhere in the page tree there could be other pages added (that have nothing to do with the PageTable field) which would affect the sort property of the page.

11 minutes ago, adrian said:

I wonder if this was intentional or an oversight?

Intentional, because there are really two sort values - the sort property that belongs to the page itself (the tree sort) and the sort that belongs to the PageTable field. This is in the PT database table but doesn't seem to be passed though as a property of the pages when you get them via the PT field.

 

@alexcapes

3 hours ago, alexcapes said:

I just don't know how to set the order of pages in a PageTable field via the api.

The order of the field is set according to the order of pages in the PageArray (or array of page IDs) when you save it to the field. For example:

$page->setAndSave('my_pagetable', array(1009, 1002, 1007)); // an array of page IDs
// or
$pa = $pages->find("template=my_template, sort=title"); // create and sort your PageArray however you like
$page->setAndSave('my_pagetable', $pa);

You could also do things like get the existing PageTable PageArray and then add pages using methods like insertAfter()

But are you sure you have to delete and recreate pages? You could keep the existing pages and change just about any property of them without actually deleting them. Maybe explain the whole process of what you're doing and someone may be able to suggest a different approach.

  • Like 5
Link to comment
Share on other sites

24 minutes ago, Robin S said:

 

The order of the field is set according to the order of pages in the PageArray (or array of page IDs) when you save it to the field. For example:


$page->setAndSave('my_pagetable', array(1009, 1002, 1007)); // an array of page IDs
// or
$pa = $pages->find("template=my_template, sort=title"); // create and sort your PageArray however you like
$page->setAndSave('my_pagetable', $pa);

You could also do things like get the existing PageTable PageArray and then add pages using methods like insertAfter()

But are you sure you have to delete and recreate pages? You could keep the existing pages and change just about any property of them without actually deleting them. Maybe explain the whole process of what you're doing and someone may be able to suggest a different approach.

Thanks for this - I'm going to experiment more with the PageArray and methods like insertAfter().

Unfortunately I do need to delete + recreate the pages. I'm dealing with large XML data files, which are updating existing pages in PW. Not only does the XML update fields but it will sometimes not include fields which is seen as the equivalent of deleting those fields entirely. It gets complicated and messy and was agreed that deleting and recreating the pages was all round cleaner way of doing things. Not ideal I know but it works for this project.

 

  • 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

×
×
  • Create New...