2

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

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"?>
<config>
<modules>
    <Staticblock_Image>
        <version>0.1.0</version>
    </Staticblock_Image>
</modules>

<global>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <Staticblock_Image before="Mage_Adminhtml">saveAction</Staticblock_Image>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
    <models>
        <staticblock_image>
            <class>Staticblock_Image_Model</class>
        </staticblock_image>
    </models>
    <blocks>
        <staticblock_image>
            <class>Staticblock_Image_Block</class>
        </staticblock_image>
        <adminhtml>
            <rewrite>
                <cms_block_edit_form>Staticblock_Image_Block_Adminhtml_Cms_Block_Edit_Form</cms_block_edit_form>
            </rewrite>
        </adminhtml>
    </blocks>
    <resources>
        <staticblock_image_setup>
            <setup>
                <module>Staticblock_Image</module>
                <class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </staticblock_image_setup>
        <staticblock_image_write>
            <connection>
                <use>core_write</use>
            </connection>
        </staticblock_image_write>
        <staticblock_image_read>
            <connection>
                <use>core_read</use>
            </connection>
        </staticblock_image_read>
    </resources>
</global>

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

<?php

class Staticblock_Image_Block_Adminhtml_Cms_Block_Edit_Form extends 
Mage_Adminhtml_Block_Widget_Form
{

  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')
                            );
$form->setHtmlIdPrefix('block_');

    $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');
        $field->setRenderer($renderer);
    }
    else {
        $fieldset->addField('store_id', 'hidden', array(
            'name'      => 'stores[]',
            'value'     => Mage::app()->getStore(true)->getId()
        ));
        $model->setStoreId(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()
    ));
$form->setValues($model->getData());
$form->setUseContainer(true);
$this->setForm($form);
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.

<?php

class Staticblock_Image_Adminhtml_IndexController extends 
Mage_Adminhtml_Controller_action
{

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');

        $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
        $uploader->setAllowRenameFiles(false);
        $uploader->setFilesDispersion(false);

        // 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'];
        $model->setBackground($data['background']);
        } else {
            $data = $request->getPost();
            if($data['background']['delete'] == 1) {
                $data['background'] = '';
                $model->setBackground($data['background']);
            } else {
                unset($data['background']);
                $model->setBackground(implode($request->getPost('background')));
            }
        }
    }

}
}

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.

<?php
/**
 * 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');
$installer->startSetup();

$installer->run('
  ALTER TABLE  `cms_block` ADD  `background` VARCHAR( 255 ) NULL;
');

$installer->endSetup();

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.