Magento 2 - Test if Static Block is Enabled in custom template
If you use
$block->getLayout()
->createBlock('Magento\Cms\Block\Block')->setBlockId(BLOCK_ID)->toHtml();
Then there are not need to check block is active or not because of Magento\Cms\Block\Block
's _toHtml()
function return content whenever the block is active[ by checking if ($block->isActive()) {
]
Check _toHtml() function of that class:
protected function _toHtml()
{
...
$html = '';
if ($blockId) {
....
/** @var \Magento\Cms\Model\Block $block */
$block = $this->_blockFactory->create();
......
if ($block->isActive()) {
$html = $this->_filterProvider->getBlockFilter()->setStoreId($storeId)->filter($block->getContent());
}
}
return $html;
}
For logical reference,you can use below
$html=$block->getLayout()->createBlock(
'Magento\Cms\Block\Block'
)->setBlockId(10)->toHtml();
// check it have content
if($html!=''):
echo $html
endif;
You can do it using below code for any custom template,
$obj = \Magento\Framework\App\ObjectManager::getInstance();
$blocks = $obj->get('Magento\Cms\Model\Block')->load(7);
if($blocks->getIsActive()){
//code
}