1

How to create a Static block for custom module creation in magento2

Thanks

4
  • You mean cms block? Commented Nov 29, 2017 at 8:39
  • Yes mohamed, i wants to create a static block Commented Nov 29, 2017 at 8:40
  • Ok you want create cms block in the same time when you install a custom module? Commented Nov 29, 2017 at 8:42
  • google.fr/amp/www.herveguetin.com/… Commented Nov 29, 2017 at 8:44

1 Answer 1

1

Try this in your install/upgrade script.

<?php 

...

private $blockFactory;

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

...
...

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

Reference

1
  • how to update if block is present already? Commented Nov 6, 2019 at 11:31

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.