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
1

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 [];
    }
}

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.