Magento CMS – How to Add Custom Fields to Page Information Tab

admincmsmagento-1.8

I need to add some extra field to "Page Information" tab in cms->pages. How can i add custom fields from getDefaultEntities() function of my Setup.php file.Can anyone help me ?

Best Answer

Adding Custom field into Magento CMS Information Tab at admin side.

Using event observer pattern you can easily add custom field to product information tab in Magento CMS pages.

For e.g. take a field "identifier".

To add this field to product info tab you have to create an extension with this three files and it will add the identifier field to product info tab in admin panel.

Create directory structure like this with these three files:

- local
 - Sandipvaghasiya
  -CMS
   -Model
    -Observer.php
   -etc
    -config.xml
   -sql
    -sandipvaghasiyacms_setup
     -mysql4-install-0.0.1.php

1) Now first create an observer that will add our custom field to product information tab for adding it:

\local\ Sandipvaghasiya\CMS\Model\Observer.php

<?php
class Sandipvaghasiya _CMS_Model_Observer
{
    public function cmsField($observer)
    {

        $model = Mage::registry('cms_page');

        $form = $observer->getForm();

        $fieldset = $form->addFieldset('sandipvaghasiya_identifier_fieldset', array('legend'=>Mage::helper('cms')->__('Custom'),'class'=>'fieldset-wide'));
        //add new field
        $fieldset->addField('identifier', 'text', array(
            'name'      => 'identifier',
            'label'     => Mage::helper('cms')->__('Identifier'),
            'title'     => Mage::helper('cms')->__('Identifier'),
            'disabled'  => false,
            //set field value
            'value'     => $model->getIdentifier()
        ));
    }
}

2) Now create a configuration file for our new module.

\local\ Sandipvaghasiya \CMS\etc\config.xml


<?xml version="1.0"?>
<config>
    <modules>
        <Sandipvaghasiya_CMS>
            <version>0.0.1</version>
        </Sandipvaghasiya_CMS>
    </modules>
    <global>
        <models>
            <sandipvaghasiyacms>
                <class>Sandipvaghasiya_CMS_Model</class>
            </sandipvaghasiyacms>
        </models>
        <events>
            <adminhtml_cms_page_edit_tab_main_prepare_form>
                <observers>
                    <sandipvaghasiya_page_edit_tab_main>
                        <type>singleton</type>
                        <class>Sandipvaghasiya_CMS_Model_Observer</class>
                        <method>cmsField</method>
                    </sandipvaghasiya_page_edit_tab_main>
                </observers>
            </adminhtml_cms_page_edit_tab_main_prepare_form>
        </events>
        <resources>
            <sandipvaghasiyacms_setup>
                <setup>
                    <module>Sandipvaghasiya_CMS</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </sandipvaghasiyacms_setup>
            <sandipvaghasiyacms_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </sandipvaghasiyacms_write>
            <sandipvaghasiyacms_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </sandipvaghasiyacms_read>
        </resources>
    </global>
</config>

3) And now third and last create a file that is used to add our custom field to magento database

\local\Sandipvaghasiya\CMS\sql\sandipvaghasiyacms_setup\mysql4-install-0.0.1.php

<?php
$installer = $this;
$installer->startSetup();
$installer->run("ALTER TABLE  {$this->getTable('cms_page')} ADD `identifier` varchar( 250 ) NOT NULL DEFAULT 'DEFAULT';");

$installer->endSetup();

4) Tell magento that our module to make active, create one xml file

\app\etc\modules\Sandipvaghasiya_CMS.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Sandipvaghasiya_CMS>
      <active>true</active>
      <codePool>local</codePool>
      <name>Sandipvaghasiya_CMS</name>
    </Sandipvaghasiya_CMS>
  </modules>
</config>

That’s it we have created a module that will add our custom field to product information tab in magento .

Now clear the magento cache and login to admin panel you will see and newly added field in the information tab.

Related Topic