How can update an existing CMS static Page and how to create a new CMS static Page - programmatically
3 Answers
Here is an example how to insert cms data in m2
Here is an example how to update cms data in m2
Now you can try following way for insert cms data:
public function __construct( \Magento\Cms\Model\PageFactory $pageFactory ) { $this->pageFactory = $pageFactory; } And now:
$cmsPage = [ 'title' => 'TEST TITLE', 'identifier' => 'test-content-1', 'page_layout' => '1column', 'content' => 'test content 1', 'is_active' => 1, 'store_id' => [0], 'sort_order' => 22 ]; $this->pageFactory->create()->setData($cmsPage)->save(); $setup->endSetup(); For Update:
$newPageContent = 'NEW PAGE CONTENT'; $newPage = $this->pageFactory->create()->load( 'test-content-1', 'identifier' ); if ($newPage->getId()) { $newPage->setContent($newPageContent); $newPage->save(); } - Is 'identifier' the 'store_id' in above update section?harri– harri2021-04-15 08:09:35 +00:00Commented Apr 15, 2021 at 8:09
- @harri no, the identifier is another column. If you want to particular store data, then just add setStore($storeId) above.Sohel Rana– Sohel Rana2021-04-15 09:01:07 +00:00Commented Apr 15, 2021 at 9:01
- I got i think thanks, I changed the identifier to page_id and then used the id rather than identifier as had duplicate identifiers across stores.harri– harri2021-04-15 09:15:35 +00:00Commented Apr 15, 2021 at 9:15
In Magento 2.3.x, Magento has introduced the DataPatchInterface to add/modify the data. You can programmatically create/modify the cms page in your custom module using a data patch.
<?php namespace My\Module\Setup\Patch\Data; use Magento\Cms\Model\PageFactory; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\Patch\DataPatchInterface; class AddMyCmsPage implements DataPatchInterface { /** * @var ModuleDataSetupInterface */ private $moduleDataSetup; /** * @var PageFactory */ private $pageFactory; /** * @var \Magento\Cms\Model\ResourceModel\Page */ private $pageResource; /** * AddNewCmsPage constructor. * @param ModuleDataSetupInterface $moduleDataSetup * @param PageFactory $pageFactory * @param \Magento\Cms\Model\ResourceModel\Page $pageResource */ public function __construct( ModuleDataSetupInterface $moduleDataSetup, PageFactory $pageFactory, \Magento\Cms\Model\ResourceModel\Page $pageResource ) { $this->moduleDataSetup = $moduleDataSetup; $this->pageFactory = $pageFactory; $this->pageResource = $pageResource; } /** * {@inheritdoc} */ public function apply() { $pageIdentifier = 'test'; $pageData = [ 'title' => 'Test Page', 'page_layout' => '1column', 'meta_keywords' => '', 'meta_description' => '', 'identifier' => $pageIdentifier, 'content_heading' => '', 'content' => 'This is my test page.', 'layout_update_xml' => '', 'url_key' => $pageIdentifier, 'is_active' => 1, 'stores' => [0], 'sort_order' => 0 ]; $this->moduleDataSetup->startSetup(); $this->pageFactory->create()->setData($pageData)->save(); $this->moduleDataSetup->endSetup(); } /** * {@inheritdoc} */ public static function getDependencies() { return []; } /** * {@inheritdoc} */ public function getAliases() { return []; } } You can refer full post in this article.
<?php namespace namespace\module\Model\StoreCreation; use Magento\Cms\Model\PageFactory; class StoreDemoCreation { private $pageFactory; public function __construct( PageFactory $pageFactory ) { $this->pageFactory = $pageFactory; } public function duplicateDemo($store_id) { $testPage = [ 'title' => 'Test page title', 'identifier' => 'test-page', 'stores' => [$store_id], 'is_active' => 1, 'content_heading' => 'Test page heading', 'content' => 'Test page content', 'page_layout' => '1column' ]; $this->pageFactory->create()->setData($testPage)->save(); } }
- I think they are extra here:
use Magento\Framework\Setup\UpgradeDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface;St3phan– St3phan2018-03-23 16:47:38 +00:00Commented Mar 23, 2018 at 16:47 - you right i will edit itopen-ecommerce.org– open-ecommerce.org2018-03-23 16:50:12 +00:00Commented Mar 23, 2018 at 16:50