1

How can update an existing CMS static Page and how to create a new CMS static Page - programmatically

  • By module or by theme please? – CompactCode Sep 23 '17 at 1:27
6

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();
}
2
<?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 Mar 23 '18 at 16:47
  • you right i will edit it – open-ecommerce.org Mar 23 '18 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.