Skip to content

Instantly share code, notes, and snippets.

@0-Sony
Last active August 12, 2016 13:09
Show Gist options
  • Save 0-Sony/47f95f63b0162fe61111 to your computer and use it in GitHub Desktop.
Save 0-Sony/47f95f63b0162fe61111 to your computer and use it in GitHub Desktop.
Cms Block Installer
<!-- local/Namespace/Module/data/namespace_module_setup/data-install.0.0.1.php -->
<?php
/**
* CMS Blocks Installer
*/
try {
$block1Content = <<<HTML
Lorem ipsum dolor sit amet, consectetur adipisicing elit.
<span>Accusamus amet asperiores aut, deserunt, eius, eligendi enim explicabo fugit minima mollitia nam necessitatibus obcaecati officia quasi qui quod tempore ut vero!</span>
HTML;
$block2Content = <<<HTML
<h2>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</h2>
Accusamus amet asperiores aut, deserunt, eius, eligendi enim explicabo fugit minima mollitia nam necessitatibus obcaecati officia quasi qui quod tempore ut vero!
HTML;
$cmsBlocksData = array(
array(
'title' => 'Title1 Cms Block',
'identifier' => 'title1_cms_block', // Must be unique
'content' => $block1Content,
'is_active' => true,
'stores' => array(Mage_Core_Model_App::ADMIN_STORE_ID)
),
array(
'title' => 'Title2 Cms Block',
'identifier' => 'title2_cms_block', // Must be unique
'content' => $block2Content,
'is_active' => true,
'stores' => array(Mage_Core_Model_App::ADMIN_STORE_ID)
)
);
/* @var $cmsBlockModel Mage_Cms_Model_Block */
$cmsBlockModel = Mage::getModel('cms/block');
foreach ($cmsBlocksData as $data) {
$cmsBlock = clone $cmsBlockModel;
// Load block, for appropriate store
$cmsBlock->setStoreId($data['stores'][0])
->load($data['identifier'], 'identifier');
// Create CMS block if it doesn't exist
if (!$cmsBlock->getId()) {
$cmsBlock->setData($data);
// Otherwise, we update it
} else {
// Specific data update
// or add $data in param addData() to update all
$cmsBlock->addData(array(
'content' => $data['content']
));
}
$cmsBlock->save();
}
} catch (Exception $e) {
Mage::logException($e);
}
<!-- local/Namespace/Module/etc/config.xml -->
<!-- Here we need to declare resources -->
<config>
<global>
<resources>
<namespace_module_setup>
<setup>
<module>Namespace_Module</module>
<class>Mage_Core_Model_Resource_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</namespace_module_setup>
</resources>
</global>
</config>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment