Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
class Duplicate extends \Magento\Backend\App\Action
{
    public function execute()
    {
        $data = $this->getRequest()->getPostValue();

        /** @var \Magento\Framework\Controller\Result\Json $result */
        $result = $this->resultJsonFactory->create();

        if ($data) {
            try {
                $newPage = $this->pageDuplicator->duplicate($data['page_id'], $data['title'], $data['identifier'], $blockData);       
            }
            ......
        }

        return $result;
    }
}

Duplicate page - duplicate blocks and assign to components

vendor/creativestyle/magesuite-cms-duplicate/Service/PageDuplicator.php:39
Code Block
class PageDuplicator
{
    public function duplicate($oldPageId, $newTitle, $newIdentifier, $blocksData = [])
    {
        $oldPage = $this->pageRepository->getById($oldPageId);

        $duplicatedPage = clone $oldPage;
        .....
        if (!empty($blocksData)) {
            $duplicatedBlocks = $this->duplicateBlocks($blocksData);

            $this->assignDuplicatedBlocksToComponents($duplicatedBlocks, $duplicatedPage);
        }

        $duplicatedPage->save();

        return $duplicatedPage;
    }
    
    protected function duplicateBlocks(array $blocks)
    {
        $this->validateIfNewBlocksIdentifiersDoesNotExist($blocks);

        foreach ($blocks as &$block) {
            $blockEntity = $this->blockRepository->getById($block['blockId']);

            $blockEntity->setId(null);
            $blockEntity->setIdentifier($block['blockIdentifier']);
            $blockEntity->setTitle($block['blockTitle']);

            $blockEntity->save();

            $block['newBlockId'] = $blockEntity->getId();
        }

        return $blocks;
    }
    
    protected function assignDuplicatedBlocksToComponents($duplicatedBlocks, $oldPage)
    {
        $oldLayoutUpdateXml = $oldPage->getLayoutUpdateXml();

        $oldComponentsConfiguration = $this->xmlToComponentConfigurationMapper->map($oldLayoutUpdateXml);

        foreach ($duplicatedBlocks as $newBlock) {
            foreach ($oldComponentsConfiguration as &$component) {
                if ($component['id'] != $newBlock['componentIdentifier']) {
                    continue;
                }

                $component = $this->mapNewBlockDataToComponent($newBlock, $component);
            }
        }

        $newLayoutUpdateXml = $this->componentConfigurationToXmlMapper->map($oldComponentsConfiguration, $oldLayoutUpdateXml);

        $oldPage->setLayoutUpdateXml($newLayoutUpdateXml);
    }
}

vendor/creativestyle/magesuite-content-constructor-admin/Repository/Xml/ComponentConfigurationToXmlMapper.php:47
Code Block
class ComponentConfigurationToXmlMapper
{
    /**
     * Maps components configuration to corresponding XML Layout format
     */
    public function map($components, $existingXml = '')
    {
        .....
    }
}

Frontend

No frontend to document.

...