src/Phlexible/Bundle/ElementtypeBundle/ElementtypeService.php
public function duplicateElementtype(Elementtype $sourceElementtype, $user)
    {
        $elementtype = clone $sourceElementtype;
        $uniqId = uniqid();

        foreach ($elementtype->getTitles() as $language => $title) {
            $elementtype->setTitle($language, $title.' - copy - '.$uniqId);
        }

        $elementtypeStructure = new ElementtypeStructure();

        $elementtype
            ->setId(null)
            ->setUniqueId($elementtype->getUniqueId().'-'.$uniqId)
            ->setRevision(1)
            ->setStructure($elementtypeStructure)
            ->setCreatedAt(new \DateTime())
            ->setCreateUser($user);

        $rii = new \RecursiveIteratorIterator($sourceElementtype->getStructure(), \RecursiveIteratorIterator::SELF_FIRST);

        $dsIdMap = [];
        foreach ($rii as $sourceNode) {
            /* @var $sourceNode ElementtypeStructureNode */
            if ($sourceNode->isReferenced()) {
                continue;
            }

            $node = clone $sourceNode;

            $dsIdMap[$sourceNode->getDsId()] = $dsId = UuidUtil::generate();

            $parentDsId = null;
            if (!$sourceNode->isRoot()) {
                $parentDsId = $dsIdMap[$sourceNode->getParentNode()->getDsId()];
            }

            $node
                ->setDsId($dsId)
                ->setParentDsId($parentDsId);

            $elementtypeStructure->addNode($node);
        }

        $mappings = $elementtype->getMappings();
        foreach ($mappings as $mappingIndex => $mapping) {
            foreach ($mapping['fields'] as $mappingFieldIndex => $mapingField) {
                if (isset($dsIdMap[$mapingField['dsId']])) {
                    $mappings[$mappingIndex]['fields'][$mappingFieldIndex]['dsId'] = $dsIdMap[$mapingField['dsId']];
                }
            }
        }
        $elementtype->setMappings($mappings);

        $this->elementtypeManager->updateElementtype($elementtype);

        return $elementtype;
    }
public function duplicateElementtype(Elementtype $sourceElementtype, $user)
    {
        $elementtype = clone $sourceElementtype;
        $uniqId = uniqid();

        foreach ($elementtype->getTitles() as $language => $title) {
            $elementtype->setTitle($language, $title.' - copy - '.$uniqId);
        }

        $elementtypeStructure = new ElementtypeStructure();

        $elementtype
            ->setId(null)
            ->setUniqueId($elementtype->getUniqueId().'-'.$uniqId)
            ->setRevision(1)
            ->setStructure($elementtypeStructure)
            ->setCreatedAt(new \DateTime())
            ->setCreateUser($user);

        $rii = new \RecursiveIteratorIterator($sourceElementtype->getStructure(), \RecursiveIteratorIterator::SELF_FIRST);

        $dsIdMap = [];
        foreach ($rii as $sourceNode) {
            /* @var $sourceNode ElementtypeStructureNode */
            if ($sourceNode->isReferenced()) {
                continue;
            }

            $node = clone $sourceNode;

            $dsIdMap[$sourceNode->getDsId()] = $dsId = UuidUtil::generate();

            $parentDsId = null;
            if (!$sourceNode->isRoot()) {
                $parentDsId = $dsIdMap[$sourceNode->getParentNode()->getDsId()];
            }

            $node
                ->setDsId($dsId)
                ->setParentDsId($parentDsId);

            $elementtypeStructure->addNode($node);
        }

        $mappings = $elementtype->getMappings();
        foreach ($mappings as $mappingIndex => $mapping) {
            foreach ($mapping['fields'] as $mappingFieldIndex => $mapingField) {
                if (isset($dsIdMap[$mapingField['dsId']])) {
                    $mappings[$mappingIndex]['fields'][$mappingFieldIndex]['dsId'] = $dsIdMap[$mapingField['dsId']];
                }
            }
        }
        $elementtype->setMappings($mappings);

        $this->elementtypeManager->updateElementtype($elementtype);

        return $elementtype;
    }
public function duplicateElementtype ( Elementtype $ sourceElementtype , $ user ) { $ elementtype = clone $ sourceElementtype ; $ uniqId = uniqid ( ) ; foreach ( $ elementtype -> getTitles ( ) as $ language => $ title ) { $ elementtype -> setTitle ( $ language , $ title . ' - copy - ' . $ uniqId ) ; } $ elementtypeStructure = new ElementtypeStructure ( ) ; $ elementtype -> setId ( null ) -> setUniqueId ( $ elementtype -> getUniqueId ( ) . '-' . $ uniqId ) -> setRevision ( 1 ) -> setStructure ( $ elementtypeStructure ) -> setCreatedAt ( new \ DateTime ( ) ) -> setCreateUser ( $ user ) ; $ rii = new \ RecursiveIteratorIterator ( $ sourceElementtype -> getStructure ( ) , \ RecursiveIteratorIterator :: SELF_FIRST ) ; $ dsIdMap = [ ] ; foreach ( $ rii as $ sourceNode ) { /* @var $sourceNode ElementtypeStructureNode */ if ( $ sourceNode -> isReferenced ( ) ) { continue ; } $ node = clone $ sourceNode ; $ dsIdMap [ $ sourceNode -> getDsId ( ) ] = $ dsId = UuidUtil :: generate ( ) ; $ parentDsId = null ; if ( ! $ sourceNode -> isRoot ( ) ) { $ parentDsId = $ dsIdMap [ $ sourceNode -> getParentNode ( ) -> getDsId ( ) ] ; } $ node -> setDsId ( $ dsId ) -> setParentDsId ( $ parentDsId ) ; $ elementtypeStructure -> addNode ( $ node ) ; } $ mappings = $ elementtype -> getMappings ( ) ; foreach ( $ mappings as $ mappingIndex => $ mapping ) { foreach ( $ mapping [ 'fields' ] as $ mappingFieldIndex => $ mapingField ) { if ( isset ( $ dsIdMap [ $ mapingField [ 'dsId' ] ] ) ) { $ mappings [ $ mappingIndex ] [ 'fields' ] [ $ mappingFieldIndex ] [ 'dsId' ] = $ dsIdMap [ $ mapingField [ 'dsId' ] ] ; } } } $ elementtype -> setMappings ( $ mappings ) ; $ this -> elementtypeManager -> updateElementtype ( $ elementtype ) ; return $ elementtype ; }
Duplicate an elementtype.

@param Elementtype $sourceElementtype
@param string      $user

@return Elementtype
Duplicate an elementtype .