16

On a fresh installation of Magento, it comes with several default CMS pages that you can edit in CMS > Pages. However, it also comes with a couple of "non-editable" CMS Pages: Orders and Returns and Contact Us... both of which are pages with forms on them, and highlights a shortcoming in Magento CE: creating and editing forms.

I have managed to override the default Contact Us with my own form, but I would like to add another form, and may need to add more forms in the future. I am somewhat familiar with creating Magento Modules to override existing functions and pages, as I've done thus far.

I have started working on a module that would enable the ability to create form pages in Magento, but they have to be invisible to the CMS management like the default forms are. I have found answers to programmatically creating a CMS page, but that adds it to Magento's CMS > Pages.

How do I create a CMS page that is only editable by a Magento Module?

andyjv
  • 3,232
  • 5
  • 26
  • 49
  • Good to know! Is it kosher to cross-post or what is the accepted practice since I've posted this here already? – andyjv Apr 18 '13 at 13:27
  • I would click on the "flag" link and ask a mod to move it for you. Generally speaking cross-posting is frowned upon. – John Conde Apr 18 '13 at 13:30
  • If you are looking for a custom contact form within a CMS page, see http://magento.stackexchange.com/questions/79602/how-to-tell-a-custom-contact-form-to-use-a-certain-transactional-template or more detailed http://stackoverflow.com/q/1066127/664108 – Fabian Schmengler Mar 21 '16 at 11:05

1 Answers1

21

Actually 'Contact Us' and 'Orders and returns' are not CMS pages. They are actually pages from a separate module. They are more like the 'Login' or 'Register' page than like CMS pages. To create a page like this you can create a simple module with a controller, one block and one template. Let's call the extension Easylife_Customform. For this you will need the following files.
app/etc/modules/Easylife_Customform.xml - module declaration file

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

app/code/local/Easylife/Customform/etc/config.xml - config file

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Customform>
            <version>0.0.1</version>
        </Easylife_Customform>
    </modules>
    <global>
        <blocks>
            <customform><!-- block alias -->
                <class>Easylife_Customform_Block</class>
            </customform>
        </blocks>
        <helpers>
            <customform><!-- helper alias -->
                <class>Easylife_Customform_Helper</class>
            </customform>
        </helpers>
    </global>
    <frontend>
        <routers>
            <customform>
                <use>standard</use>
                <args>
                    <module>Easylife_Customform</module>
                    <frontName>customform</frontName><!-- url key for module -->
                </args>
            </customform>
        </routers>
        <layout>
            <updates>
                <easylife_customform>
                    <file>easylife_customform.xml</file><!-- frontend layout file -->
                </easylife_customform>
            </updates>
        </layout>
        <translate>
            <modules>
                <Easylife_Customform>
                    <files>
                        <default>Easylife_Customform.csv</default><!-- translation file (not mandatory) -->
                    </files>
                </Easylife_Customform>
            </modules>
        </translate>
    </frontend>
</config>

app/design/frontend/base/default/layout/easylife_customform.xml - frontend layout file

<?xml version="1.0"?>
<layout>
    <customform_index_index translate="label" module="customform">
        <label>Custom form</label>
        <reference name="root">
            <action method="setTemplate"><template>page/2columns-right.phtml</template></action><!-- can be different -->
        </reference>        
        <reference name="content">
            <block type="core/template" name="customform" template="easylife_customform/form.phtml" /><!-- content of page -->
        </reference>
    </customform_index_index>
</layout>

app/code/local/Easylife/Customform/Helper/Data.php - default module helper

<?php
class Easylife_Customform_Helper_Data extends Mage_Core_Helper_Abstract{
}

app/design/frontend/base/default/template/easylife_customform/form.phtml - the actual html for the form - make this look like you need

<form action="<?php echo $this->getUrl('customform/index/send')?>">
    <input type="text" name="name" />
    <input type="submit" />
</form>

app/code/local/Easylife/Customform/controllers/IndexController.php - the module controller

<?php 
class Easylife_Customform_IndexController extends Mage_Core_Controller_Front_Action{
    public function indexAction(){ //this will display the form
        $this->loadLayout();
        $this->_initLayoutMessages('core/session'); //this will allow flash messages
        $this->renderLayout();
    }
    public function sendAction(){ //handles the form submit
        $post = $this->getRequest()->getPost();
        //do something with the posted data
        Mage::getSingleton('core/session')->addSuccess($this->__('Your message was sent'));//add success message.
        $this->_redirect('*/*');//will redirect to form page
    }
}

This should be it. Clear the cache and you should be able to access the form at mysite.com/customform
I hope I wrote the code correctly and didn't miss something

Marius
  • 197,939
  • 53
  • 422
  • 830
  • 2
    you really went the extra mile on this answer. +1 – philwinkle Apr 22 '13 at 04:29
  • @philwinkle: is that good or bad? :) – Marius Apr 22 '13 at 14:29
  • Really great guide Marius, thank you! I am trying to set a page title, the

    <reference name="head"> <action method="setTitle" translate="title"><title>Subscribe to our Newsletter</title></action> </reference>

    won't work.

    – loeffel Jun 22 '15 at 14:35
  • @loeffel. Maybe you have something else that overrides the title. In theory the code should work. – Marius Jun 22 '15 at 14:37
  • @Marius This is very handy, but how can we add Error Messages? I tried adding Mage::getSingleton('core/session')->addError("Error"); but no luck. Its only showing success message. Any help? – Aamir Siddique May 12 '16 at 15:31
  • @AamirSiddique. Strange. It should work with error messages also. I have no idea what's wrong. – Marius May 12 '16 at 15:36
  • @Marius This is working perfect (except the error not being shown for me) but i would like to know how can we change the Title of the page when using this module? – Aamir Siddique May 12 '16 at 15:56
  • @Marius nevermind, i firgured it out. Using the $this->getLayout()->getBlock('head')->setTitle("New Title"); inside the file IndexController.php -> indexAction() – Aamir Siddique May 12 '16 at 16:21
  • @Marius: Thanks for your code. Am using your code, when am run http://127.0.0.1/magento/customform showing 404 error page. Pls help me to fix this issue – Madhumitha Aug 31 '17 at 13:17