bernhard Posted November 15, 2024 Share Posted November 15, 2024 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 More sharing options...
bernhard Posted November 16, 2024 Author Share Posted November 16, 2024 I solved this by renaming the field instead of moving content. In this case this was the even better (as more performant) solution, but I'd still be interested if anyone knows another way of doing this! 1 Link to comment Share on other sites More sharing options...
Jonathan Lahijani Posted December 30, 2024 Share Posted December 30, 2024 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); } 1 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now