Skip to main content
1 of 4
user avatar
user avatar

How to add a CMS block programmatically in Magento 2?

I need to add a CMS block via an install/upgrade script. I've already figured out how to add "normal" CMS pages as seen in the script below. But since I can't find any way to add CMS blocks in Magento 2's code, on Google or here, I'm quite jammed.

namespace [Vendor]\[Module]\Setup;

use Magento\Cms\Model\Page;
use Magento\Cms\Model\PageFactory;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;

class UpgradeData implements UpgradeDataInterface
{
    /**
     * Page factory.
     *
     * @var PageFactory
     */
    private $pageFactory;

    /**
     * Init.
     *
     * @param PageFactory $pageFactory
     */
    public function __construct(PageFactory $pageFactory)
    {
        $this->pageFactory = $pageFactory;
    }

    /**
     * Upgrade.
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     */
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        if (version_compare($context->getVersion(), '0.0.1') < 0) {
            $testPage = [
                'title' => 'Test page title',
                'identifier' => 'test-page',
                'stores' => [0],
                'is_active' => 1,
                'content_heading' => 'Test page heading',
                'content' => 'Test page content',
                'page_layout' => '1column'
            ];

            $this->pageFactory->create()->setData($testPage)->save();
        }

        $setup->endSetup();
    }
}

I understand I don't need all values defined in the $testPage array, so I stripped it down to the following:

$testPage = [
    'title' => 'Test block title',
    'identifier' => 'test-block',
    'stores' => [0],
    'is_active' => 1
    'content' => 'Test block content'
];

Does anyone know what I need to change to make this test page into a test block?

Note: I based my script on the install data script in the Magento 2 CMS module located in vendor/module-cms/Setup/InstallData.php.

user42093