Adding a CMS block via setup script
For this, I suggest using the data
folder of one of your custom modules.
Let's say that the module is currently at version 1.0.4
.
Create the file data/[module]_setup/data-upgrade-1.0.4-1.0.5.php
with the following content:
Edit: changed file name
$content = 'BLOCK CONTENT HERE';
//if you want one block for each store view, get the store collection
$stores = Mage::getModel('core/store')->getCollection()->addFieldToFilter('store_id', array('gt'=>0))->getAllIds();
//if you want one general block for all the store viwes, uncomment the line below
//$stores = array(0);
foreach ($stores as $store){
$block = Mage::getModel('cms/block');
$block->setTitle('Block title here');
$block->setIdentifier('block_identifier_here');
$block->setStores(array($store));
$block->setIsActive(1);
$block->setContent($content);
$block->save();
}
After this just change the version in config.xml
to 1.0.5
clear the cache and refresh any page.
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$staticBlock = array(
'title' => 'Test Block',
'identifier' => 'test-block',
'content' => 'Sample Test Block',
'is_active' => 1,
'stores' => array(0)
);
Mage::getModel('cms/block')->setData($staticBlock)->save();
From: http://blog.chapagain.com.np/magento-create-cms-page-static-block-programmatically/#sthash.1FYKYYhI.dpuf
Instead of using the sql
folder, you should put any setup scripts modifying CMS data in the data
folder. See app/code/core/Mage/Cms/data/cms_setup
for some good examples. These install scripts add static blocks and CMS pages.
For changing config values, use this code:
$installer->setConfigData(
Mage_Page_Model_Config::XML_PATH_CMS_LAYOUTS,
'your_value_here'
);
Also, Here's a useful article