How to add CMS Static Block Programmatically using Setup Patchdata in Magento 2?

From Magento 2.3, Core modules use the declarative schema approach rather than setup upgrade scripts. This is the new recommended approach for Magento versions 2.3 and upper version.

All the InstallData and UpgrageData will be replaced by Data Patch Versioning.

Now you have to create Setup/Patch/Data folder and create the class file for create/update attribute or add sample data in Magento.

3 Replies to “How to add CMS Static Block Programmatically using Setup Patchdata in Magento 2?”

  1. Hi Rakesh,

    Very nice article on declarative data patch.

    Would you mind to share some thoughts on a question I have, once I created a class app/code/Rbj/CmsBlock/Setup/Patch/Data/AddNewCmsStaticBlock.php to add 1-2 static blocks initially, later I have a need to add 2-3 more static blocks than do I have to create a new class file app/code/Rbj/CmsBlock/Setup/Patch/Data/AddNewCmsStaticBlock_[timestamp].php with its apply() and getVersion() method or do I will have to update the changes in the same class file app/code/Rbj/CmsBlock/Setup/Patch/Data/AddNewCmsStaticBlock.php updating the content of the apply() method and updating the version in the getVersion() method?

  2. Great article! Thank you very much 🙂

    There is some notices about phpDoc:

    @var PageFactory

    It need to be changed to @var BlockFactory

    AddAccessViolationPageAndAssignB2CCustomers constructor.

    I think here is wrong description

    @author ,@category, @package, and @subpackage MUST NOT be used. Documentation is organized with the use of namespaces.

    https://devdocs.magento.com/guides/v2.3/coding-standards/docblock-standard-general.html#documentation-space

    The @inheritdoc tag SHOULD NOT be used. If a child class method requires a long description to explain its purpose, it may use @inheritdoc to indicate the new description is intended as an addition to the parent method description. In general such method overrides are a code smell and should be used as an incentive to make the code more self-documenting if possible.

    https://devdocs.magento.com/guides/v2.3/coding-standards/docblock-standard-general.html#inheritdoc

    Also, PatchVersionInterface is deprecared

    For Block save you need to use MagentoCmsModelBlockRepository

Leave a Reply