1

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

1
  • By module or by theme please? Sep 23, 2017 at 1:27

3 Answers 3

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();
}
3
  • Is 'identifier' the 'store_id' in above update section?
    – harri
    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
    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
    Apr 15, 2021 at 9:15
3

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.

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();
}

}

2
  • I think they are extra here: use Magento\Framework\Setup\UpgradeDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface;
    – St3phan
    Mar 23, 2018 at 16:47
  • you right i will edit it Mar 23, 2018 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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