Create a sample admin module for form processing

Last modified by CVinson86 on Fri, June 25, 2010 09:05
Source|Old Revisions  

This is an old revision of the document!


Create a sample admin module for form processing

Let’s say your module is called Mage_Sintax

Module declaration

Filename: app/etc/modules/Mage_Sintax.xml

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

Module configuration file

Filename: app/code/local/Mage/Sintax/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Mage_Sintax>
            <version>0.1.0</version>
        </Mage_Sintax>
    </modules>
    
    <global>
        <models>
            <sintax>
                <class>Mage_Sintax_Model</class>
                <resourceModel>sintax_mysql4</resourceModel>
            </sintax>
            <sintax_mysql4>
                <class>Mage_Sintax_Model_Mysql4</class>
            </sintax_mysql4>
        </models>
        <blocks>
            <sintax>
                <class>Mage_Sintax_Block</class>
            </sintax>
        </blocks>
        <helpers>
            <sintax>
                <class>Mage_Sintax_Helper</class>
            </sintax>  
        </helpers>
    </global>
    
    <admin>
        <routers>
            <sintax>
                <use>admin</use>
                <args>
                    <module>Mage_Sintax</module>
                    <frontName>sintax</frontName>
                </args>
            </sintax>
        </routers>
    </admin>
    
    <adminhtml>
        <translate>
            <modules>
                <mage_adminhtml>
                    <files>
                        <sintax>Mage_Sintax.csv</sintax>
                    </files>
                </mage_adminhtml>
            </modules>
        </translate>
        
        <menu>
            <catalog>
                <children>
                    <sintax_adminform translate="title" module="sintax">
                        <title>My Form</title>
                        <action>sintax/adminhtml_myform</action>
                    </sintax_adminform>
                </children>
            </catalog>
        </menu>
        
        <acl>
            <resources>
                <admin>
                    <children>
                        <catalog>
                            <children>
                                <sintax_adminform>
                                    <title>My Form</title>
                                </sintax_adminform>
                            </children>
                        </catalog>
                    </children>
                </admin>
            </resources>
        </acl>
        
        <layout>
            <updates>
                <sintax>
                    <file>sintax.xml</file>
                </sintax>
            </updates>
        </layout>
    </adminhtml>
</config>

Controller

Filename: app/code/local/Mage/Sintax/controllers/Adminhtml/MyformController.php

<?php

class Mage_Sintax_Adminhtml_MyformController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {
        $this->loadLayout()->renderLayout();
    }
    
    public function postAction()
    {
        $post = $this->getRequest()->getPost();
        try {
            if (empty($post)) {
                Mage::throwException($this->__('Invalid form data.'));
            }
            
            /* here's your form processing */
            
            $message = $this->__('Your form has been submitted successfully.');
            Mage::getSingleton('adminhtml/session')->addSuccess($message);
        } catch (Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
        }
        $this->_redirect('*/*');
    }
}

Form block

Filename: app/code/local/Mage/Sintax/Block/Myform.php

// this step is optional

Layout updates for your form

Filename: app/design/adminhtml/default/default/layout/sintax.xml

<?xml version="1.0"?>
<layout>
    <sintax_adminhtml_myform_index>
        <update handle="sintax_myform_index"/>
        <reference name="content">
            <block type="adminhtml/template" name="myform" template="sintax/myform.phtml"/>
        </reference>
    </sintax_adminhtml_myform_index>
</layout>

Form template

Filename: app/design/adminhtml/default/default/template/sintax/myform.phtml

<div class="content-header">
    <table cellspacing="0" class="grid-header">
        <tr>
            <td><h3><?=$this->__('My form title')?></h3></td>
            <td class="a-right">
                <button onclick="editForm.submit()" class="scalable save" type="button"><span>Submit my form</span></button>
            </td>
        </tr>
    </table>
</div>
<div class="entry-edit">
    <form id="edit_form" name="edit_form" method="post" action="<?=$this->getUrl('*/*/post')?>">
        <h4 class="icon-head head-edit-form fieldset-legend"><?=$this->__('This fieldset name')?></h4>
        <fieldset id="my-fieldset">
            <table cellspacing="0" class="form-list">
                <tr>
                    <td class="label"><?=$this->__('Field label')?> <span class="required">*</span></td>
                    <td class="input-ele"><input class="input-text required-entry" name="myform[myfield]" /></td>
                </tr>
            </table>
        </fieldset>
    </form>
</div>
<script type="text/javascript">
    var editForm = new varienForm('edit_form');
</script>

Translations

Filename: app/locale/en_US/Mage_Sintax.csv

// this step is optional

Helper

Filename: app/code/local/Mage/Sintax/Helper/Data.php

<?PHP

class Mage_Sintax_Helper_Data extends Mage_Core_Helper_Abstract {

}



 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs