Steekvlam
Steekvlam

Reputation: 21

Magento how to add extra fields for static block

I've been trying to add extra fields to the static block so it will be easier to update.

How can I add a simple textfield or maybe a image upload field?

See example: https://i.sstatic.net/2HtDC.jpg

Thanks in advance!

Upvotes: 1

Views: 3438

Answers (3)

Apeiron
Apeiron

Reputation: 1971

In adition to @Rajeev You need get the parent form in this way...

<?php
    class Namespace_Module_Block_Adminhtml_Cms_Block_Edit_Form extends Mage_Adminhtml_Block_Cms_Block_Edit_Form {

         protected function _prepareForm()
        {
          $form = parent::_prepareForm()->getForm();
          $fieldset = $form->addFieldset('fieldset_example', array('legend'=>Mage::helper('core')->__('My example fieldset')));
          $fieldset->addField('sub_title', 'text', array(
                'name'      => 'sub_title',
                'label'     => Mage::helper('cms')->__('Sub Title'),
                'title'     => Mage::helper('cms')->__('Sub Title'),
                'required'  => true,
            ));
            return $this;    
        }
    }

Upvotes: 1

Rajeev K Tomy
Rajeev K Tomy

Reputation: 2214

For this, you need to overwrite this class Mage_Adminhtml_Block_Cms_Block_Edit_Form. This class is used to add fieldsets and fields for cms_block. Take a look on the _prepareForm() method inside it.

If you put this code, just after Title field,

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

you can see your sub-title text field in static blocks. However Do not edit a core file directly. You need to write a custom module that should overwrite this class. Your Module config file should contain this code

File : app/code/local/Namespace/Module/etc/config.xml

<config> 
    <global>
        <blocks>
            <adminhtml>
                <rewrite>
                     <cms_block_edit_form>Namespace_Module_Block_Adminhtml_Cms_Block_Edit_Form</cms_block_edit_form>
                </rewrite>        
            </adminhtml>
        </blocks>
    </global>
</config>

This will allow you to rewrite the class. What you need to do now is define the rewrite class now and in there you need to rewrite _prepareForm(). It should some what look like this.

Location : app/code/local/Namespace/Module/Block/Adminhtml/Cms/Block/Edit/Form.php

<?php
class Namespace_Module_Block_Adminhtml_Cms_Block_Edit_Form extends Mage_Adminhtml_Block_Cms_Block_Edit_Form {

     protected function _prepareForm()
    {
        //put all the code inside parent class here 
        //then place the below content in appropriate place

        $fieldset->addField('sub_title', 'text', array(
            'name'      => 'sub_title',
            'label'     => Mage::helper('cms')->__('Sub Title'),
            'title'     => Mage::helper('cms')->__('Sub Title'),
            'required'  => true,
        ));
        return parent::_prepareForm();

    }
}

Try based on this idea

EDIT

Please note, it will allow you to put new field in cms > block, howver to save this, you need to define model for your module. You have two options. Add a new field to save your new field in Cms > Block table or create your own table and store this value in that field along with the referene to cms >block table. This is out of box and you should implement it your own way.

Happy coding

Upvotes: 1

Shivananda Chary
Shivananda Chary

Reputation: 565

My suggestion is, rather add an extra field, you can add a <div>...</div> to in the content are by removing what you see is what you get editor(just click on the show / hide Editor).
And for image you can direct upload images by clicking on the inset/edit image option from the menu. It will be simpler than creating custom fields.

Upvotes: 0

Related Questions