/** * Duplicate a content item * * @param ContentInterface $content */ public function duplicate(ContentInterface $content) { //get valueset to clone $valueset = $content->getValueSet(); //clone valueset $duplicatedValueset = clone $valueset; $this->detachAndPersist($duplicatedValueset); //duplicate content $duplicatedContent = clone $content; $duplicatedContent->setSlug(null); $duplicatedContent->setValueSet($duplicatedValueset); $this->detachAndPersist($duplicatedContent); //iterate values, clone each and assign duplicate valueset to it foreach ($valueset->getValues() as $value) { //skip empty attributes if (is_null($value->getId())) { continue; } $duplicatedValue = clone $value; $duplicatedValue->setValueSet($duplicatedValueset); $this->detachAndPersist($duplicatedValue); } $this->em->flush(); return $duplicatedContent->getId(); }
/** * Duplicate a content item * * @param ContentInterface $content * * @return ContentInterface $duplicate */ public function duplicate(ContentInterface $content) { //duplicate content $duplicatedContent = clone $content; $duplicatedContent->setTitle(sprintf("%s copy %s", $duplicatedContent->getTitle(), date('r'))); $duplicatedContent->setSlug(null); $duplicatedContent->setActive(false); $duplicatedContent->setCreatedAt(new \DateTime()); //get valueset to clone if (null !== ($valueset = $content->getValueSet())) { //clone valueset $duplicatedValueset = clone $valueset; $this->detachAndPersist($duplicatedValueset); $duplicatedContent->setValueSet($duplicatedValueset); } $duplicatedContent->setBlocks(null); $this->detachAndPersist($duplicatedContent); if ($valueset) { //iterate values, clone each and assign duplicate valueset to it foreach ($valueset->getValues() as $value) { //skip empty attributes if (is_null($value->getId())) { continue; } $duplicatedValue = clone $value; $duplicatedValue->setValueSet($duplicatedValueset); $this->detachAndPersist($duplicatedValue); } } $this->em->flush(); return $duplicatedContent; }
/** * Duplicate a content item * * @param ContentInterface $content * @param NestedValue $nestedIn */ public function duplicate(ContentInterface $content, NestedValue $nestedIn = null) { //get valueset to clone $valueset = $content->getValueSet(); //clone valueset $duplicatedValueset = clone $valueset; $this->detachAndPersist($duplicatedValueset); //duplicate content $duplicatedContent = clone $content; $duplicatedContent->setSlug(null); $duplicatedContent->setValueSet($duplicatedValueset); if (!is_null($nestedIn)) { $duplicatedContent->setNestedIn($nestedIn); } $this->detachAndPersist($duplicatedContent); //iterate values, clone each and assign duplicate valueset to it foreach ($valueset->getValues() as $value) { //skip empty attributes if (is_null($value->getId())) { continue; } $duplicatedValue = clone $value; $duplicatedValue->setValueSet($duplicatedValueset); $this->detachAndPersist($duplicatedValue); //if type nested, find content that has nested_in value same as id of value if ($value instanceof \Opifer\EavBundle\Entity\NestedValue) { $nestedContents = $this->getRepository()->findby(['nestedIn' => $value->getId()]); foreach ($nestedContents as $nestedContent) { $this->duplicate($nestedContent, $duplicatedValue); } } } $this->em->flush(); return $duplicatedContent->getId(); }