Jump to content
pppws

create repeaters via api, sort them on creation

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!

 

 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...