Jump to content

Move RepeaterMatrix data to another RepeaterMatrix within a Repeater


a-ok
 Share

Recommended Posts

I'm working with a client who had uploaded 70+ articles into the CMS and used a RepeaterMatrix (training_detail_flexible_content) to add a number of 'modules' per article. Since then we wanted to do something a little different so I have created a Repeater (training_detail_flexible_content_block) and within that repeater is the RepeaterMatrix (training_detail_flexible_content) which has text, video, and image modules.

I'm trying to work out a way, for each article, to essentially 'copy/paste' the old text module fields into the new one. I've done this before but only when there was one text field into repeater whereas there's multiple text modules per article.

This is what I had before below. Any advice?

$articles = $pages->find("parent=/articles/");
foreach ($articles as $article) {
	$article->of(false);
	$repeater = $article->article_content->getNew();
	$repeater->repeater_matrix_type = 1;
	$repeater->article_content_text = $article->article_text;
	$repeater->save();
	$article->article_content->add($repeater);
	$article->save();
}

You can see below that the first lot of modules is what we previously had and I want to move them into the first repeater row of the Repeater with the exactly the same order etc.

Screen Shot 2017-02-02 at 17.04.00.png

Link to comment
Share on other sites

Link to comment
Share on other sites

I'm thinking something like this but wasn't sure on if the ->add() is missing?

$exercises = $pages->find("parent=/undervisning/");
foreach ($exercises as $exercise) {

	$exercise->of(false);
	
	$old_repeater = $exercise->training_detail_flexible_content; // Old repeater matrix
	$new_repeater = $exercise->training_detail_flexible_content_block->getNew(); // Add repeater row to new repeater
	
	foreach ($old_repeater as $old_row) { // For each row in old repeater matrix
		
		$new_row = $new_repeater->training_detail_flexible_content->getNew(); // Add repeater matrix row within repeater
		$new_row->repeater_matrix_type = 1; // Set the type of the repeater matrix row (text)
		
		// By here we should have a new repeater matrix row (type=1) added within the repeater to match the old repeater matrix rows
		
		// Now to transfer the text across		
		$new_row->training_detail_flexible_content_text = $old_row->training_detail_flexible_content_text;
		
		// Save each repeater matrix row added
		$new_row->save();
		
	}
	
	// Save the whole new repeater
	$new_repeater->save();
	
	// Save page
	$exercise->save();

}

 

 

 

Link to comment
Share on other sites

1 hour ago, oma said:

Anybody have any further thoughts on this? Struggling to work this out...

I might be easier to change the parent of the RepeaterMatrix rather than trying to move each item and their contents. This is what I did with the PageTable to Repeater converter: https://github.com/adrianbj/ProcessAdminActions/blob/bcb8479e90224f958f0747476657c459cd230307/actions/PageTableToRepeaterMatrix.action.php#L294

Not saying it is the only/best solution, but it seemed to remove the hassles of handling different fieldtypes in my situation at least.

  • Like 2
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...