Magento – Add setting to Static Block page

cmsstatic-block

I am working on a new extensions that stores some HTML data in Static blocks. To make it work as needed, I need to be able to add a new setting to the Static Block page that allows me to set a static block as enabled/disabled for my extension to use it.

My extension is named Bounce Reducer so that is the setting I would like to add.

Is this possible to add a setting to this screen like that? And then be able to access it in other sections/pages of my extensions code?

Screenshot

Best Answer

The enabled flag on static blocks correspond directly to the db table is_active column, which is of type smallint.

To enable this functionality in your own module, you'd have to extend that table and add a column onto the table cms_block (I use tinyint here, as a boolean):

$installer->getConnection()->addColumn($installer->getTable('cms/block'),
    'br_enabled', 'tinyint(1) UNSIGNED DEFAULT 0 AFTER is_active');

You'll also have to rewrite Mage_Cms_Model_Resource_Block::_getLoadSelect to consider your flag as well:

protected function _getLoadSelect($field, $value, $object)
{
    $select = parent::_getLoadSelect($field, $value, $object);

    if ($object->getStoreId()) {
        $storeIds = array(Mage_Core_Model_App::ADMIN_STORE_ID, (int)$object->getStoreId());
        $select->join(
            array('cms_page_store' => $this->getTable('cms/page_store')),
            $this->getMainTable() . '.page_id = cms_page_store.page_id',
            array())
            ->where('is_active = ?', 1)
            ->where('br_enabled = ?', 1)
            ->where('cms_page_store.store_id IN (?)', $storeIds)
            ->order('cms_page_store.store_id DESC')
            ->limit(1);
    }

    return $select;
}

Finally, add in the appropriate form dropdown by extending Mage_Adminhtml_Block_Cms_Block_Edit_Form::_prepareForm and adding the following:

    $fieldset->addField('br_enabled', 'select', array(
        'label'     => Mage::helper('cms')->__('Bounce Reducer Enabled'),
        'title'     => Mage::helper('cms')->__('Bounce Reducer Enabled'),
        'name'      => 'br_enabled',
        'required'  => true,
        'options'   => array(
            '1' => Mage::helper('cms')->__('Enabled'),
            '0' => Mage::helper('cms')->__('Disabled'),
        ),
    ));
Related Topic