Back to Top

Get the Static Block Content in Controller

Updated 24 July 2019

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 :).

Start your headless eCommerce
now.
Find out More
. . .

Leave a Comment

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


2 comments

  • highriskgateways
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home