Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check CMS block is active?

Tags:

magento

I wonder how to check that a particular CMS block is active or not.

So far I have found that CMS block are Mage_Cms_Block_Block class that inherits from Mage_Cms_Block_Abstract class

Mage::log(get_class(Mage::app()->getLayout()->createBlock('cms/block')->setBlockId('promo_space')

Neither of the two classes have methods that would check wether the block is active or not. How do I do it?

like image 300
latvian Avatar asked Feb 17 '10 16:02

latvian


2 Answers

Got this myself

I created a method isActive(Identifiere, Value) in helper "Block" in the Mage/Cms local 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;
    }

}

parameter $attribute is table(cms-block) field such as 'identifier' or 'title' and value can be the name of the static block or identifier itself. Both used to filter down the particular static block you are interested in

Here is how i call the helper

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

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

I hope its useful.

like image 191
latvian Avatar answered Nov 15 '22 14:11

latvian


This code works for me:

if ( $this->getLayout()->createBlock('cms/block')->setBlockId('YOUR-BLOCK-ID')->toHtml() !== '' ) {}
like image 35
ahgood Avatar answered Nov 15 '22 14:11

ahgood