Magento 2 – How to Add a CMS Block Programmatically

cmscms-blockmagento2programmaticallyupgrade-script

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/magento/module-cms/Setup/InstallData.php.

Best Answer

add this to the your class:

private $blockFactory;

and make your constructor look like this:

public function __construct(
    PageFactory $pageFactory,
    \Magento\Cms\Model\BlockFactory $blockFactory
  )
{
    $this->pageFactory = $pageFactory;
    $this->blockFactory = $blockFactory;
}

you can even remove the $pageFactory dependency if you don't need it anymore.

Then replace:

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

with

$testBlock = [
    'title' => 'Test block title',
    'identifier' => 'test-block',
    'stores' => [0],
    'is_active' => 1,
];
$this->blockFactory->create()->setData($testBlock)->save();
Related Topic