3

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 ?

1
  • 1
    For any future googlers; Sandip's answer works well. But even if you have all caches disabled, you should still delete /var/cache folder to get the data to save to the database
    – Maurice
    Commented Jul 15, 2014 at 8:08

1 Answer 1

9

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.

9
  • @sadip,I can see the field in page information tab. But value entered is that field are not getting saved to database.
    – Anu
    Commented May 15, 2014 at 9:17
  • @Anu, can you please verify that in the cms_page table your new custom field column is added and it is saved there and please make sure to clear the cache and try this after re login into admin side Commented May 15, 2014 at 9:48
  • 2
    cache is disabled.Column is there in cms_page table.If i add data directly into table means its getting displayed.But when i change and save its not saving.
    – Anu
    Commented May 15, 2014 at 10:00
  • i forgot one thing here that might creating issue for that i am updating the answer please see if it is working Commented May 15, 2014 at 10:08
  • ,Where you have made the change? Can you mention please..
    – Anu
    Commented May 15, 2014 at 10:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.