Jump to content

Possible to move repeater items from one field to another while maintaining the ID?


bernhard
 Share

Recommended Posts

Hi everyone!

Today I had to move some data stored in a repeater field to another field. I searched the forum and found an admin action by @adrian and came up with a new method in rockmigrations that can copy or move repeater items to new fields and/or new pages: https://github.com/baumrock/RockMigrations/blob/ad59425831c74e32034035afa9d2276f398cc2be/RockMigrations.module.php#L3536

Now I realised that there is a problem: The way this works is it creates new items and then removes the old items. But that also means that IDs change 😮 And I have referenced those repeater items from somewhere else.

Does anybody have an idea of how to do that?

Link to comment
Share on other sites

  • 1 month later...

I was thinking about this as well recently.  Here's what I came up with:

function convertRepeaterPageToPage($repeaterPage, $newParent, $newTemplate, $newStatus) {
  // store for cleanup
  $forPage = $repeaterPage->getForPage();
  $forField = $repeaterPage->getForField();

  // convert
  $repeaterPage->set('parent_id', $newParent->id);
  $repeaterPage->set('templates_id', $newTemplate->id);
  $repeaterPage->set('status', $newStatus);
  $repeaterPage->set('published', time()); // make this adjustable as well?
  $repeaterPage->save(['noHooks'=>true]);

  // cleanup
  $forPage->save($forField, ['noHooks'=>true]);

  return $repeaterPage;
}

Note: It should be improved to make sure what's provided in the arguments is valid.  Also maybe have the ability to set the 'name' field of the page as well instead of preserving the auto-generated one that a repeater item gets assigned.  Also maybe use types for the arguments.

---

Example:  Let's say you have a repeater field called 'books'.  Then you decide one day that it would be better that they existed as regular pages instead of repeater pages.  Therefore, you would create a new template called 'book' making sure it has the same fields as the repeater fields, then do this:

foreach($pages->get('/path/to/page/')->books as $book) {
  convertRepeaterPageToPage($book, wire('pages')->get('/foo/'), 'book', 1);
}
  • Like 1
  • Thanks 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...