public function duplicate($blocks, $owner = null)
    {
        if ($blocks instanceof BlockInterface) {
            $blocks = array($blocks);
        }

        $iterator = new \RecursiveIteratorIterator(
            new RecursiveBlockIterator($blocks),
            \RecursiveIteratorIterator::SELF_FIRST
        );

        $originalIdMap = array();
        $originalParentMap = array();

        $clones = array();

        // iterate over all owned blocks and disconnect parents keeping ids
        /** @var Block $block */
        foreach ($iterator as $block) {
            $blockId = $block->getId();
            $parentId = false;

            if (in_array($block->getId(), $originalIdMap)) {
                continue;
            }

            $clone = clone $block;
            $clone->setId(null);
            $clone->setParent(null);
            $clone->setOwner($owner);

            // if it has a parent we need to keep the id as reference for later
            if ($block->getParent()) {
                $parentId = $block->getParent()->getId();
            }

            if ($clone instanceof BlockContainerInterface) {
                $clone->setChildren(null);
            }

            $this->em->persist($clone);
            $this->em->flush($clone); // the block gets a new id

            $originalIdMap[$clone->getId()] = $blockId;
            if ($parentId) {
                $originalParentMap[$clone->getId()] = $parentId;
            }

            $clones[] = $clone;
        }

        // iterate over all new blocks and reset their parents
        foreach ($clones as $clone) {
            if (isset($originalParentMap[$clone->getId()])) {
                foreach ($clones as $parent) {
                    if (isset($originalParentMap[$clone->getId()]) &&
                        $originalParentMap[$clone->getId()] === $originalIdMap[$parent->getId()]) {
                        $clone->setParent($parent);
                        $parent->addChild($clone);

                        $this->em->flush($clone);
                        $this->em->flush($parent);
                    }
                }
            }
        }

        return $clones;
    }
public function duplicate($blocks, $owner = null)
    {
        if ($blocks instanceof BlockInterface) {
            $blocks = array($blocks);
        }

        $iterator = new \RecursiveIteratorIterator(
            new RecursiveBlockIterator($blocks),
            \RecursiveIteratorIterator::SELF_FIRST
        );

        $originalIdMap = array();
        $originalParentMap = array();

        $clones = array();

        // iterate over all owned blocks and disconnect parents keeping ids
        /** @var Block $block */
        foreach ($iterator as $block) {
            $blockId = $block->getId();
            $parentId = false;

            if (in_array($block->getId(), $originalIdMap)) {
                continue;
            }

            $clone = clone $block;
            $clone->setId(null);
            $clone->setParent(null);
            $clone->setOwner($owner);

            // if it has a parent we need to keep the id as reference for later
            if ($block->getParent()) {
                $parentId = $block->getParent()->getId();
            }

            if ($clone instanceof BlockContainerInterface) {
                $clone->setChildren(null);
            }

            $this->em->persist($clone);
            $this->em->flush($clone); // the block gets a new id

            $originalIdMap[$clone->getId()] = $blockId;
            if ($parentId) {
                $originalParentMap[$clone->getId()] = $parentId;
            }

            $clones[] = $clone;
        }

        // iterate over all new blocks and reset their parents
        foreach ($clones as $clone) {
            if (isset($originalParentMap[$clone->getId()])) {
                foreach ($clones as $parent) {
                    if (isset($originalParentMap[$clone->getId()]) &&
                        $originalParentMap[$clone->getId()] === $originalIdMap[$parent->getId()]) {
                        $clone->setParent($parent);
                        $parent->addChild($clone);

                        $this->em->flush($clone);
                        $this->em->flush($parent);
                    }
                }
            }
        }

        return $clones;
    }
public function duplicate ( $ blocks , $ owner = null ) { if ( $ blocks instanceof BlockInterface ) { $ blocks = array ( $ blocks ) ; } $ iterator = new \ RecursiveIteratorIterator ( new RecursiveBlockIterator ( $ blocks ) , \ RecursiveIteratorIterator :: SELF_FIRST ) ; $ originalIdMap = array ( ) ; $ originalParentMap = array ( ) ; $ clones = array ( ) ; // iterate over all owned blocks and disconnect parents keeping ids /** @var Block $block */ foreach ( $ iterator as $ block ) { $ blockId = $ block -> getId ( ) ; $ parentId = false ; if ( in_array ( $ block -> getId ( ) , $ originalIdMap ) ) { continue ; } $ clone = clone $ block ; $ clone -> setId ( null ) ; $ clone -> setParent ( null ) ; $ clone -> setOwner ( $ owner ) ; // if it has a parent we need to keep the id as reference for later if ( $ block -> getParent ( ) ) { $ parentId = $ block -> getParent ( ) -> getId ( ) ; } if ( $ clone instanceof BlockContainerInterface ) { $ clone -> setChildren ( null ) ; } $ this -> em -> persist ( $ clone ) ; $ this -> em -> flush ( $ clone ) ; // the block gets a new id $ originalIdMap [ $ clone -> getId ( ) ] = $ blockId ; if ( $ parentId ) { $ originalParentMap [ $ clone -> getId ( ) ] = $ parentId ; } $ clones [ ] = $ clone ; } // iterate over all new blocks and reset their parents foreach ( $ clones as $ clone ) { if ( isset ( $ originalParentMap [ $ clone -> getId ( ) ] ) ) { foreach ( $ clones as $ parent ) { if ( isset ( $ originalParentMap [ $ clone -> getId ( ) ] ) && $ originalParentMap [ $ clone -> getId ( ) ] === $ originalIdMap [ $ parent -> getId ( ) ] ) { $ clone -> setParent ( $ parent ) ; $ parent -> addChild ( $ clone ) ; $ this -> em -> flush ( $ clone ) ; $ this -> em -> flush ( $ parent ) ; } } } } return $ clones ; }
Clones blocks and persists to database

@param BlockInterface      $block
@param BlockOwnerInterface $owner

@return BlockInterface
Clones blocks and persists to database