/
CmsDuplicate

CmsDuplicate

GitHub - magesuite/cms-duplicate

This module provides an easy way of duplicating CMS pages in the admin panel.

 

Installation

This module is part of MageSuite metapackage

 

Installation if metapackage is not used:

composer require "creativestyle/magesuite-cms-duplicate" ^2.0.0

Admin settings

In order to duplicate CMS page select Duplicate from the Action dropdown:

Fill fields in the modal and Save the page.

Settings:

Name

Note

Name

Note

New page identifier

Please provide new identifier (url key) for duplicated page. New identifier must be unique and cannot be used already by any other page.

New page title

Please provide new title for duplicated page. Title does not have to be unique.

Backend

Backend documentation need to be added

 

Clicking Duplicate from dropdown

Controller for displaying form to duplicate existing page

vendor/creativestyle/magesuite-cms-duplicate/Controller/Adminhtml/Duplicate/Form.php:38
class Form extends \Magento\Backend\App\Action { public function execute() { ..... $block = $layout->createBlock(\MageSuite\CmsDuplicate\Block\Adminhtml\Duplicate\Form::class) ->toHtml(); ..... $resultRaw->setContents($block); return $resultRaw; } }

 

Template for form for new (duplicated) page

vendor/creativestyle/magesuite-cms-duplicate/view/adminhtml/templates/form.phtml
<?php /** @var \MageSuite\CmsDuplicate\Block\Adminhtml\Duplicate\Form $block */ $page = $block->getPage(); ?> <div id="duplicate-page-modal"> <form method="post" id="duplicate-cms-page-form" action="<?php echo $block->getUrl('cmspageduplicate/duplicate/duplicate') ?>"> ........... <?php $blocks = $block->getBlocksToDuplicate(); ?>

 

Getting page which we want to duplicate by page_id

vendor/creativestyle/magesuite-cms-duplicate/Block/Adminhtml/Duplicate/Form.php:47
class Form extends \Magento\Backend\Block\Template { protected $_template = 'MageSuite_CmsDuplicate::form.phtml'; public function getPage() { if (!$this->cmsPage) { $pageId = $this->getRequest()->get('page_id'); $this->cmsPage = $this->pageRepository->getById($pageId); } return $this->cmsPage; } public function getBlocksToDuplicate() { $page = $this->getPage(); $layoutXml = $page->getLayoutUpdateXml(); $components = $this->xmlToComponentConfigurationMapper->map($layoutXml); $blocks = []; foreach ($components as $component) { ..... $blockData = $this->getBlockData($component); $blocks[] = $blockData; } return $blocks; } }

 

Clicking duplicate

vendor/creativestyle/magesuite-cms-duplicate/Controller/Adminhtml/Duplicate/Duplicate.php:43
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
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
class ComponentConfigurationToXmlMapper { /** * Maps components configuration to corresponding XML Layout format */ public function map($components, $existingXml = '') { ..... } }

 

 

Frontend

No frontend to document.

 

 

Related content