![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/app/code/Soon/Core/Setup/ |
<?php /** * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * @author Hervé Guétin <[email protected]> <@herveguetin> * @copyright Copyright (c) 2016 Agence Soon (http://www.agence-soon.fr) */ declare(strict_types=1); namespace Soon\Core\Setup; use Magento\Cms\Api\BlockRepositoryInterface; use Magento\Cms\Api\Data\PageInterface; use Magento\Cms\Api\PageRepositoryInterface; use Magento\Cms\Model\Page; use Magento\Cms\Model\ResourceModel\Page\Collection; use Magento\Cms\Model\ResourceModel\Page\CollectionFactory; use Magento\CmsUrlRewrite\Model\CmsPageUrlRewriteGenerator; use Magento\Framework\Api\SearchCriteriaInterface; use Magento\UrlRewrite\Model\UrlPersistInterface; use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; class Cms { /** * @var SearchCriteriaInterface */ private $searchCriteria; /** * @var BlockRepositoryInterface */ private $cmsBlockRepository; /** * @var PageRepositoryInterface */ private $cmsPageRepository; /** * The definition of our custom pages * * @var array */ private $customPages = [ [ 'title' => 'Accueil', 'identifier' => 'home', 'content' => 'Accueil' ], [ 'title' => '404 - Page non trouvée', 'identifier' => 'no-route', 'content' => 'Page non trouvée' ], [ 'title' => 'CGV', 'identifier' => 'cgv', 'content' => 'Conditions générales de vente' ], [ 'title' => 'Mentions légales', 'identifier' => 'sales-terms', 'content' => 'Mentions légales' ], [ 'title' => 'Exemples CSS', 'identifier' => 'css-samples', 'content' => 'Exemples CSS - A compléter par intégrateurs' ], ]; /** * @var PageInterface */ private $page; /** * @var Collection */ private $pageCollection; /** * @var UrlPersistInterface */ private $urlPersist; /** * Cms constructor. * @param SearchCriteriaInterface $searchCriteria * @param BlockRepositoryInterface $cmsBlockRepository * @param PageRepositoryInterface $cmsPageRepository * @param PageInterface $page * @param CollectionFactory $pageCollectionFactory * @param UrlPersistInterface $urlPersist */ public function __construct( SearchCriteriaInterface $searchCriteria, BlockRepositoryInterface $cmsBlockRepository, PageRepositoryInterface $cmsPageRepository, PageInterface $page, CollectionFactory $pageCollectionFactory, UrlPersistInterface $urlPersist ) { $this->searchCriteria = $searchCriteria; $this->cmsBlockRepository = $cmsBlockRepository; $this->cmsPageRepository = $cmsPageRepository; $this->page = $page; $this->pageCollection = $pageCollectionFactory->create(); $this->urlPersist = $urlPersist; } /** * Install CMS stuff */ public function install(): void { $this->cleanCmsPages(); $this->createCmsPages(); } /** * Delete all existing CMS pages */ private function cleanCmsPages(): void { // We cannot use API because it doesn't do what it is supposed to do... // @see http://goo.gl/Lo2ZLO array_map(function ($page) { /** @var $page Page */ //@codingStandardsIgnoreLine // We must do this because \Magento\CmsUrlRewrite\Plugin\Cms\Model\ResourceModel\Page::aroundDelete is not called when calling delete on page resource model. $this->urlPersist->deleteByData( [ UrlRewrite::ENTITY_ID => $page->getId(), UrlRewrite::ENTITY_TYPE => CmsPageUrlRewriteGenerator::ENTITY_TYPE, ] ); $page->getResource()->delete($page); }, $this->pageCollection->getItems()); } /** * Add our custom CMS pages */ private function createCmsPages(): void { array_map(function ($customPage) { $this->page ->setId(null)// in order to create a new page ->setTitle($customPage['title']) ->setIdentifier($customPage['identifier']) ->setIsActive(true) ->setContentHeading('') ->setContent($customPage['content']) ->setPageLayout('1column'); $this->cmsPageRepository->save($this->page); }, $this->customPages); } }