I've created a widget, this widgets has a CMS block chooser on the block class I have the following function:
public function getCmsBlock()
{
$blockId = $this->getData("block_id");
$storeId = $this->_storeManager->getStore()->getId();
$block = $this->_blockFactory
->create()
->setStoreId($storeId)
->load($blockId);
return $block;
}
The $this->_storeManager
class variable is an instant of Magento\Cms\Model\BlockFactory
I realised that the load function is deprecated and the setStoreId
doesn't really work.
What is the alternative for loading a CMS block programatically where I would be able to set the store ID and check if the CMS is active?
Best Answer
Try to use
Magento\Cms\Model\BlockRepository
to get block by ID, like this:In addition you can use the method
getList()
to get block like this:The
getList()
result should look like this:and block looks as usual: