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