Read More
Menu Close

    Get the Static Block Content in Controller

    Today we will learn how we can get the cms/static block content in Controller’s action file.
    Here we go.

    To get the job done, we need to use the following classes :

    \Magento\Cms\Model\Block
    \Magento\Cms\Model\Template\FilterProvider

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $block = $objectManager->create(\Magento\Cms\Model\Block::class);
    $storeId = 1;//it can be any active store id.
    $blockId = 12;// it can be any available block id.
    $block->setStoreId($storeId)->load($blockId);
    $html = $objectManager->create(\Magento\Cms\Model\Template\FilterProvider::class)->getBlockFilter()->setStoreId($storeId)->filter($block->getContent());
    

    This is a simple code that helps us to get the html content of the static/cms block to use it further.
    happy coding :).

    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    2 comments

  • highriskgateways
    Thanks for sharing an Awesome Blog.
    • Nishad Bhan (Moderator)
      Thanks, Dear. For any doubts you can drop a mail at [email protected]
  • Back to Top