Copy Express Form

Permalink 1 user found helpful
Hi Everyone,

I'm just putting this out there if anyone needs to copy an express entity form. I couldn't find how to do this anywhere else and I figure if I needed it, someone else may as well.

$em = \Core::make('database/orm')->entityManager();
$r = $em->getRepository('\Concrete\Core\Entity\Express\Form');
$f = $r->findOneById('{form id goes here}');
$n = new Form();
$n->setName($f->getName().' - Copy');
$n->setEntity($f->getEntity());
$em->persist($n);
$em->flush();
foreach($f->getFieldSets() as $fs) {
   $nfs = new FieldSet();
   $nfs->setTitle($fs->getTitle());
   $nfs->setForm($n);
   $em->persist($nfs);
   $em->flush();
   foreach($fs->getControls() as $c) {

JeffPaetkau
 
JeffPaetkau replied on at Permalink Reply
JeffPaetkau
Also, this should probably simply be a button in the dashboard.
mnakalay replied on at Permalink Reply
mnakalay
Thank you for sharing, this could be useful.

One thing you might want to consider is that Doctrine operations can take some time and I could imagine this code timing out with (really) big forms.

Although Doctrine doesn't really allow for batch saving per se, there are still ways of doing it.

Here's a link in case you want to look into it:https://www.doctrine-project.org/projects/doctrine-orm/en/latest/ref...
JeffPaetkau replied on at Permalink Reply
JeffPaetkau
Ya, the code is not perfect for sure. It also doesn't copy association controls. However, it did what I needed. If anyone wants to spend the time to improve it ... have at it!
nickratering replied on at Permalink Reply
nickratering
Hello JeffPaetkau,

Copying express form is exactly what I need for a project. How do you run this piece of code, where to put it? And what's the ID of an expressform, the handle, database ID, or the code in the url after /view_entity/ ?

Best,
Nick