atwix

Adding custom attribute to a Magento CMS page

Sometimes we need to add a custom attribute to a CMS page. I’ll try to describe how we can do this as simply as possible using a custom module with observers.

First of all, you should create your module registration file under app/etc/modules:

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

after this, we need to create a folder that contains our own module under app/code/local, in my case it is app/code/local/Atwix/CMS. Next step is to create module’s configuration file (app/code/local/Atwix/CMS/etc/config.xml):

<?xml version="1.0"?>
<config>
    <global>
        <models>
            <atwixcms>
                <class>Atwix_CMS_Model</class>
            </atwixcms>
        </models>
        <events>
            <adminhtml_cms_page_edit_tab_content_prepare_form>
                <observers>
                    <atwix_page_edit_tab_content>
                        <type>singleton</type>
                        <class>Atwix_CMS_Model_Observer</class>
                        <method>cmsField</method>
                    </atwix_page_edit_tab_content>
                </observers>
            </adminhtml_cms_page_edit_tab_content_prepare_form>
        </events>
    </global>
</config>

IMPORTANT: we need to add a column ‘content_custom’ to the ‘cms_page’ table. That can be done using Magento install scripts (read how to create your own installer here)
if you`re using Magento Enterprise, you should add a column ‘content_custom’ to the ‘enterprise_cms_page_revision’ table as well.

and finally, create Observer file (app/code/local/Atwix/CMS/Model/Observer.php):

<?php

class Atwix_CMS_Model_Observer
{
    public function cmsField($observer)
    {
        //get CMS model with data
        $model = Mage::registry('cms_page');
        //get form instance
        $form = $observer->getForm();
        //create new custom fieldset 'atwix_content_fieldset'
        $fieldset = $form->addFieldset('atwix_content_fieldset', array('legend'=>Mage::helper('cms')->__('Custom'),'class'=>'fieldset-wide'));
        //add new field
        $fieldset->addField('content_custom', 'text', array(
            'name'      => 'content_custom',
            'label'     => Mage::helper('cms')->__('Content Custom'),
            'title'     => Mage::helper('cms')->__('Content Custom'),
            'disabled'  => false,
            //set field value
            'value'     => $model->getContentCustom()
        ));
        
    }
}

That’s all folks. Hope this article is helpful.

Read Part 2: Displaying a custom CMS attribute on the Magento frontend



Enjoyed this post? us on Facebook and on Twitter to stay tuned.

  • Eric

    That’s helpful, thank you!

  • James Gardiner

    I am able to edit and save these additional attributes via the CMS but I’d like to know if there is a way to display the custom attributes on the front end, ideally “automatically” from the layout (i.e. without calling getAttribute or getData in my template) in the same way as “content” works?

    Thanks!

  • VMG

    Thanks for this great post.
    But… where can I learn how to handle the POST? I mean, I can see the fields in the CMS page, but they aren’t saved to the DB yet.

    • unclejimrocks

      My content doesn’t save to the database either following these exact steps. Magento 1.7.0.2.

  • vmg

    In addition to my last post.

    I’ve added this code to the config.xml:

    singleton
    Atwix_CMS_Model_Observer
    savePage

    So I would assume that in Oberserver.php the public function savePage() would start working, but it doesn’t… (I want to use this to upload a file)

    Any thoughts? Thanks in advance for your help

  • Anonymous

    Hi Nick!

    Really appreciate the work you’ve done here and shared. Gotta quick question though – I’m using Johann Reinke’s Clever CMS extension which writes all the data to a different table (MAGPREFIX_cms_page_tree instead of MAGPREFIX_cms_page. If I manually put content into the database it shows up nicely in the field I’ve created with your instructions, and I can get it to display perfectly on the front end, but I can’t get it to SAVE to the correct table.

    I’m a Magento noob, but I tried changing the obvious $model = line above to Mage::registry(‘cms_page_tree’) but that completely broke the editing screen in the backend.
    Any thoughts?

  • http://www.facebook.com/landofmoab Matthew Cronkhite

    Thank you! It didn’t take much to get this working for me. I’m using 1.7.0.2

  • http://www.facebook.com/landofmoab Matthew Cronkhite

    Thank you! This worked without much effort. I’m using 1.7.0.2

  • luke

    for anyone who wants the sql install script to go along with this here it is…

    $installer = $this;
    $installer->startSetup();

    $conn = $installer->getConnection();
    $table = $installer->getTable(‘cms_page’);
    $conn->addColumn($table, ‘content_custom’, ‘varchar(100)’);

    $installer->endSetup();

    • Norman Francis

      Thank you. This was very helpful.

  • Norman Francis

    Thank you. This was very helpful.
    However, I would like to note that I ran into an issue with using the CMS as my module name. Apparently there is already an entry in the core_resources called cms_setup. I would suggest naming your module something else as you are following this tutorial.

    • NickKravchuk

      Hi Norman, you’re right, that’s why I called it atwixcms_setup, thanks for comment.

  • Muzafar Ali

    thank you. this was very helpful,

    where we add the installer setup code and how we handle to save in db..

    Thanks

    • NickKravchuk

      Hi, you can read more about SQL setup here, also Luke gave us an example in comments.