Jump to content

create repeaters via api, sort them on creation


pppws
 Share

Recommended Posts

hey there,

i have a form to create repeater fields on the frontend via the api, which works fine.

if(count($newMoments)) {
            $page->moments->removeAll();
            $page->moments->add($newMoments);
            $page->save("moments");
            //$page->moments->sort('yearofmoment, month');
            //$page->save("moments");
            //echo "saved professions";
}

but now i'm trying to autosort them when/before (?) saving the page. on top you see one of the attempty i've tried to get this done.

by now i know that i have to put the sort function outside the if() because sometimes there will be no now moments, just editing of the existing ones which could cause a new sorting as well. so i put it here right under the if(). which causes the 'moments' to be sorted after submitting the form. but after a reload the the moments get back to the order in which they were entered. and in the backend nothing changes at all. 

what am i doing wrong?

do i have to use a usort() function to sort the $newMoments php array?

thanks for any help!

 

 

Link to comment
Share on other sites

  • 2 weeks later...

Saving a sort order to a Repeater field is trickier than you might expect. This is because the sort order of items in a Repeater field is determined by their sort position under their parent page in Admin > Repeaters > [repeater field] > [container page]. So the "sort" value of each Repeater page in other words.

When you do...

$page->repeater_field->sort('some_property')

...you are only changing the order of items in the PageArray. This order is never saved to the "sort" value of the individual repeater items.

Here is one way you can save the sort order:

// Here you order the PageArray how you want it
$r_items = $page->repeater_field->sort('some_property');

// BUT, the original keys (indexes) are preserved
// So you need to get just the values with new keys
// This step could be merged into the line above
// Alternatively you could just use a counter variable in the foreach loop
$r_items = $r_items->getValues();

foreach($r_items as $index => $r) {
    // Update the sort value of the repeater page
    // See: https://processwire.com/api/ref/pages/sort/
    $pages->sort($r, $index);
}

 

Also, you might be interested in the Page Move and Resort module by @kixe.

It has a method...

$pages->resortChildren($page, $selectorValue)  bool/ null resort children under specified parent based on selector value (sort=$selectorValue)

...which you could use on the parent page of the Repeater items.

  • Like 3
  • Thanks 1
Link to comment
Share on other sites

  • 2 weeks later...

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...