I have not much expertise in magento so please pardon me if i am doing pretty much wrong. I am using Magento ver.

I want to add a new image field in magento static block. I want to use this new image field as background image of each static block. Background image name will be saved to database table "CMS_BLOCK" in a column named 'background'. For this , i am writing a extension.

Below is detail of extension. This is the config file(config.xml) of extension

<?xml version="1.0"?>

                        <Staticblock_Image before="Mage_Adminhtml">saveAction</Staticblock_Image>

Then this is the file(Form.php) responsible for all static block fields and below file is located at app\code\local\Staticblock\Image\Block\Adminhtml\Cms\Block\Edit\Form.php


class Staticblock_Image_Block_Adminhtml_Cms_Block_Edit_Form extends 

  protected function _prepareForm()
$model = Mage::registry('cms_block');
$form = new Varien_Data_Form(array(
                                'id' => 'edit_form', 
                                'action' => $this->getData('action'), 
                                'method' => 'post', 
                                'enctype' => 'multipart/form-data')

    $fieldset = $form->addFieldset('base_fieldset', array('legend'=>Mage::helper('cms')->__('General Information'), 'class' => 'fieldset-wide'));

    if ($model->getBlockId()) {
        $fieldset->addField('block_id', 'hidden', array(
            'name' => 'block_id',

    $fieldset->addField('title', 'text', array(
        'name'      => 'title',
        'label'     => Mage::helper('cms')->__('Block Title'),
        'title'     => Mage::helper('cms')->__('Block Title'),
        'required'  => true,

    $fieldset->addField('identifier', 'text', array(
        'name'      => 'identifier',
        'label'     => Mage::helper('cms')->__('Identifier'),
        'title'     => Mage::helper('cms')->__('Identifier'),
        'required'  => true,
        'class'     => 'validate-xml-identifier',

     * Check is single store mode
    if (!Mage::app()->isSingleStoreMode()) {
        $field =$fieldset->addField('store_id', 'multiselect', array(
            'name'      => 'stores[]',
            'label'     => Mage::helper('cms')->__('Store View'),
            'title'     => Mage::helper('cms')->__('Store View'),
            'required'  => true,
            'values'    => Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm(false, true),
        $renderer = $this->getLayout()->createBlock('adminhtml/store_switcher_form_renderer_fieldset_element');
    else {
        $fieldset->addField('store_id', 'hidden', array(
            'name'      => 'stores[]',
            'value'     => Mage::app()->getStore(true)->getId()

    $fieldset->addField('is_active', 'select', array(
        'label'     => Mage::helper('cms')->__('Status'),
        'title'     => Mage::helper('cms')->__('Status'),
        'name'      => 'is_active',
        'required'  => true,
        'options'   => array(
            '1' => Mage::helper('cms')->__('Enabled'),
            '0' => Mage::helper('cms')->__('Disabled'),
    if (!$model->getId()) {
        $model->setData('is_active', '1');
    $fieldset->addField('background', 'image', array(
        'name' => 'background',
        'label' => 'Background image',
        'title' => 'Background image'

    $fieldset->addField('content', 'editor', array(
        'name'      => 'content',
        'label'     => Mage::helper('cms')->__('Content'),
        'title'     => Mage::helper('cms')->__('Content'),
        'style'     => 'height:36em',
        'required'  => true,
        'config'    => Mage::getSingleton('cms/wysiwyg_config')->getConfig()
return parent::_prepareForm();



Next file is the controller file(IndexController.php) of the extension and it is located at app\code\local\Staticblock\Image\controllers\Adminhtml\IndexController.php as below.


class Staticblock_Image_Adminhtml_IndexController extends 

public function saveAction() {

    $model = Mage::getModel('staticblock_image/staticblock_image');
    $data = $this->getRequest()->getPost();
    if ($data) {
        if (isset($_FILES['background']['name']) && $_FILES['background']['name'] != '') {
        $uploader = new Varien_File_Uploader('background');


        // Set media as the upload dir
        $media_path  = Mage::getBaseDir('media') . DS . 'background' . DS;

        // Set thumbnail name
        $file_name = 'cms_';

        // Upload the image
        $uploader->save($media_path, $file_name . $_FILES['background']['name']);

        $data['background'] = 'background' . DS . $file_name . $_FILES['background']['name'];

        // Set thumbnail name
        $data['background'] = $data['background'];
        } else {
            $data = $request->getPost();
            if($data['background']['delete'] == 1) {
                $data['background'] = '';
            } else {


Last file(install-0.1.0.php) to create a background column in the CMS table of the database. It is located at app\code\local\Staticblock\Image\sql\staticblock_image_setup and it is as below.

 * Flexishore Cms module
 * @category    Flexishore
 * @package     Flexishore_Cms
 * @author      Kos Rafał <rafal.k@flexishore.com>
 * @copyright  Copyright (c) 2011 Flexishore http://flexishore.com

$installer = $this;
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

  ALTER TABLE  `cms_block` ADD  `background` VARCHAR( 255 ) NULL;


This is all i have done so far using help from google and guys like you. Please help me and guide me to achieve this task. I know images can be uploaded from the WYSIWYG editor but i want to upload a separate image for each static block.

  • What issue you are facing with this code? Please provide more details. – John Jul 24 '17 at 12:22
  • Thanks for your response John.Actually image field appears in the backed to upload image. But when i click "Save and Continue" button , image is neither moved to said directory nor image name stored to the database table. – Naqash Butt Jul 24 '17 at 12:27
  • May be there is an issue in config.xml file or in IndexController.php. – Naqash Butt Jul 24 '17 at 12:31
  • John do you have any solution to my said problem? Can you please help me out? No one is responding to my question and i am literally dying. – Naqash Butt Jul 26 '17 at 5:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.