How to check the CMS block is active?

I wonder how to verify that a specific CMS block is active or not.

So far I have found that the CMS block is the Mage_Cms_Block_Block class, which inherits from the Mage_Cms_Block_Abstract class

Mage :: Log (get_class (Mage :: application () → getLayout () → createBlock ('CMB / block') → setBlockId ('promo_space')

None of the two classes have methods that would check whether a block is included or not. How to do it?

+7
magento
source share
5 answers

Got it myself

I created the isActive (Identifiere, Value) method in the auxiliary block "Block" in the local Mage / Cms module.

This is how the method looks

 public function isActive($attribute, $value){ $col = Mage::getModel('cms/block')->getCollection(); $col->addFieldToFilter($attribute, $value); $item = $col->getFirstItem(); $id = $item->getData('is_active'); if($id == 1){ return true; }else{ return false; } } 

the $ attribute parameter is a table field (cms-block), such as "identifier" or "title", and the value can be the name of the static block itself or the identifier itself. Both are used to filter the specific static block that interests you.

This is how I call the assistant

 if(Mage::helper('cms/block')->isActive('identifier','promo_space')){ //do that } 

I also updated the config.xml file for the Cms block to read my new helper and method.

I hope this will be helpful.

+5
source share

Mage::getModel('cms/block')->load('static_block_identifier')->getIsActive()

Replace static_block_identifier with the identifier that you assigned to the static CMS block.

+17
source share

This code works for me:

 if ( $this->getLayout()->createBlock('cms/block')->setBlockId('YOUR-BLOCK-ID')->toHtml() !== '' ) {} 
+3
source share

This may be an old one, but I'm using a different method that works not only for cms blocks, but also for any other block loaded into the layout. If you need to check if a block has been loaded:

 if($this->getLayout()->getBlock('your_block_name')) //Do whatever you need here 

This is pretty easy!

+1
source share

The best way to do this is to add an observer to this event: controller_action_layout_generate_blocks_after, which occurs immediately after Magento initializes and generates Block objects. You have access to layout classes and actions and to all generated blocks before displaying HTML

 //You can check if the block exists in the layout $layout = $observer->getEvent()->getObserver(); $cmsBlock = $layout->getBlock($identifier);//Returns false if doesn't exist. //You can check it in the database too: $cmsModel = Mage::getModel('cms/page')->load($identifier); if($cmsModel->getId() AND $cmsModel->getIsActive() == 1) { //CMS block is active } 
+1
source share

All Articles