Custom Module with Custom Database Table

Last modified by ahawks122 on Wed, December 22, 2010 12:33
Source|Old Revisions  

This is an old revision of the document!


Module Creator

Thanks to member Danieln for creating this PHP script to automatically create a base module given a Namespace and Module.

Credits: Danieln (creator), Alistek (contributor), Damián Culotta (contributor), Somesid (contributor), Ahsan Shahzad (contributor)

Download Module Creator (v0.0.9.1) or Install it as an Extension (beta)

modcreator0.0.9.0_small.jpg

Updated: 25/08/2008 - 0.0.9.1 Release Notes

#NOTE: v0.0.9.1 holds a bug that do not allow to ACL to manage module’s user level access. I am currently working on it to fix.

#ADDED: The module is now created with an upload field for example.

#ADDED: Code in the frontend controller shows how to get an object by param and by custom query .

#FIXED: You can now use blocks to get data on the frontend (see http://www.magentocommerce.com/boards/viewreply/54110/ for more details) .

#NOTE: Wiki post has yet to be updated with new code and explanations.

Updated: 07/04/2008 - 04:00AM CST - 0.0.9.0 Release Notes

#NEW FEATURE: Now with module uninstaller and better look&feel.

#NEW FEATURE: Point to your Magento Install Directory to get you module copied to the right places right away.

#NOTE: Thanks to Barbanet the below problem should be alleviated with entering in your interface and theme at module creation time.

#NOTE: A common problem with the Module Creator has been the confusion over the use of ‘interface’ and ‘theme’.

The directories:

  1. /new/app/design/adminhtml/interface/theme/
  2. /new/app/design/frontend/interface/theme/

In a new Magento installation correspond to:

  1. /app/design/adminhtml/default/default/
  2. /app/design/frontend/default/default/

Please make sure to put your <module>.xml files in the correct ‘layout’ directories as these will determine whether your module is shown or not.

Installing the Module Creator

To install the Module Creator, you can use the Magento Connect Manager with the extension key, or install it manually as follows. Note that if you install via the Connect Manager the path it uses is /moduleCreator.

Copy the index.php file and the Blank folder to your webserver. You may want to put it in it’s own folder so that the index file does not conflict with other index files. Example: mysite.com/module_creator/

Go to the module creator index page in your web browser, and fill in the desired module information. If you enter your Magento directory, the module creator will attempt to install the module directly into Magento, though file permissions must be set correctly for this to happen. If your file permissions are incorrect, or you do not want the module installed right away, you can leave the Magento Directory blank, and the module creator will create the module in a folder called [i]new[/i], and place it in the module creator’s folder.

Please note that if you enter in your magento directory to install to you must have the correct path. Let’s say that you have Magento installed here:

  1. http://www.my_site.com/magento/

And you have the Module Creator installed here:

  1. http://www.my_site.com/module_creator/

This means that the path that you would need to enter in the Module Creator is this:

  1. ../magento

The ‘..’ signifies one directory up.

Document Conventions

I am going to use a few common conventions here that should make it easier to figure out what you need to replace as I am making this as generic as possible.

Anything in angled brackets (including the angled brackets): < >, needs to replaced with the appropriate text.

Anything in square brackets (including the square brackets): [ ], needs to replaced with the appropriate text.

The reason I am using two different conventions here are that XML files already use angled brackets so you will only see the square brackets in use in XML files.

NOTE: All directory, file, class names are Case Sensitive unless otherwise noted.

Create Directories

Magento Modules follow a common naming scheme, Namespace_Module. This is very important to remember as you need to be careful about how you name your classes. Every custom module will be created in the directory:

/app/code/local

The first step in creating your custom module is determining the namespace and module name. The namespace is simply an extra division that you can create you module in. This means that you can have two modules named the same thing if they are in two different namespaces. One way to use namespaces is to use your company name or initials for all of your modules. If my company name is Acme and I am creating a module called News the full module name would be Acme_News. Magento uses the namespace Mage. There is nothing stopping you from using that namespace under local, i.e. you could create Mage_News and that would work just fine.

Note : You can not use underscore within your module name

Note2: It seems that currently, if you use upper case characters in module names (expecting to show word starts since neither - nor _ is allowed)... the install will fail or rather the module will not work. Suggestion: use a single upper case character, at the beginning of the name.

Let’s setup our directory structure:

/app/code/local/<Namespace>/<Module>/

  1. Block/
  2. controllers/
  3. etc/
  4. Model/
  5.     Mysql4/
  6.         <Module>/
  7. sql/
  8.     <module>_setup/

/app/design/frontend/<interface>/<theme>/

  1. template/
  2.     <module>/

Activate Module

Magento requires there to be an XML file that tells Magento to look for and use your custom module.

/app/etc/modules/<Namespace>_<Module>.xml

  1. <?xml version="1.0"?>
  2. <config>
  3.     <modules>
  4.         <[Namespace]_[Module]>
  5.             <active>true</active>
  6.             <codePool>local</codePool>
  7.         </[Namespace]_[Module]>
  8.     </modules>
  9. </config>

Also you can disable your module in the Configuration menu on the backend via the Advanced tab.

NOTE: Due to a bug in Magento, whitespace is not treated correctly. This means that if you leave space in the values between node names (anything in angled brackets <> is a node), Magento will break.

As an explanation of the above code you will see that all you are changing is the [Namespace]_[Module] text and leaving everything else the same. Please note the capital P in codePool. If this is lowercase this module will not be active.

Create Controller

/app/code/local/<Namespace>/<Module>/controllers/IndexController.php

  1. <?php
  2. class <Namespace>_<Module>_IndexController extends Mage_Core_Controller_Front_Action
  3. {
  4.     public function indexAction()
  5.     {
  6.             $this->loadLayout();
  7.             $this->renderLayout();
  8.     }
  9. }

NOTE: You may notice that there is no closing, ?>, PHP tag in the code. This is a common coding style that Magento core classes use. Magento Coding Standard is similar (with some exceptions) to Zend Framework PHP Coding Standard and you can find the detailed explanations of this rule in Zend Framework Documentation

Create Configuration XML

/app/code/local/<Namespace>/<Module>/etc/config.xml

  1. <?xml version="1.0"?>
  2. <config>
  3.     <modules>
  4.         <[Namespace]_[Module]>
  5.             <version>0.1.0</version>
  6.         </[Namespace]_[Module]>
  7.     </modules>
  8.     <frontend>
  9.         <routers>
  10.             <[module]>
  11.                 <use>standard</use>
  12.                 <args>
  13.                     <module>[Namespace]_[Module]</module>
  14.                     <frontName>[module]</frontName>
  15.                 </args>
  16.             </[module]>
  17.         </routers>
  18.         <layout>
  19.             <updates>
  20.                 <[module]>
  21.                     <file>[module].xml</file>
  22.                 </[module]>
  23.             </updates>
  24.         </layout>
  25.     </frontend>   
  26.     <global>
  27.         <models>
  28.             <[module]>
  29.                 <class>[Namespace]_[Module]_Model</class>
  30.                 <resourceModel>[module]_mysql4</resourceModel>
  31.             </[module]>
  32.             <[module]_mysql4>
  33.                 <class>[Namespace]_[Module]_Model_Mysql4</class>
  34.                 <entities>
  35.                     <[module]>
  36.                         <table>[module]</table>
  37.                     </[module]>
  38.                 </entities>
  39.             </[module]_mysql4>
  40.         </models>
  41.         <resources>
  42.             <[module]_setup>
  43.                 <setup>
  44.                     <module>[Namespace]_[Module]</module>
  45.                 </setup>
  46.                 <connection>
  47.                     <use>core_setup</use>
  48.                 </connection>
  49.             </[module]_setup>
  50.             <[module]_write>
  51.                 <connection>
  52.                     <use>core_write</use>
  53.                 </connection>
  54.             </[module]_write>
  55.             <[module]_read>
  56.                 <connection>
  57.                     <use>core_read</use>
  58.                 </connection>
  59.             </[module]_read>
  60.         </resources>
  61.         <blocks>
  62.             <[module]>
  63.                 <class>[Namespace]_[Module]_Block</class>
  64.             </[module]>
  65.         </blocks>
  66.         <helpers>
  67.             <[module]>
  68.                 <class>[Namespace]_[Module]_Helper</class>
  69.             </[module]>
  70.         </helpers>
  71.     </global>
  72. </config>

NB : You can use the frontName of your choice without any link to your module name. IE : Mage_Catalog could have “mycatalog” as a frontName.

Create Helper

/app/code/local/<Namespace>/<Module>/Helper/Data.php

  1. <?php
  2.  
  3. class <Namespace>_<Module>_Helper_Data extends Mage_Core_Helper_Abstract
  4. {
  5.  
  6. }

Create Models

/app/code/local/<Namespace>/<Module>/Model/<Module>.php

  1. <?php
  2.  
  3. class <Namespace>_<Module>_Model_<Module> extends Mage_Core_Model_Abstract
  4. {
  5.     public function _construct()
  6.     {
  7.         parent::_construct();
  8.         $this->_init('<module>/<module>');
  9.     }
  10. }

/app/code/local/<Namespace>/<Module>/Model/Mysql4/<Module>.php

  1. <?php
  2.  
  3. class <Namespace>_<Module>_Model_Mysql4_<Module> extends Mage_Core_Model_Mysql4_Abstract
  4. {
  5.     public function _construct()
  6.     {   
  7.         $this->_init('<module>/<module>', '<module>_id');
  8.     }
  9. }

NOTE: The ‘<module>_id’ refers to the PRIMARY KEY in your database table.

/app/code/local/<Namespace>/<Module>/Model/Mysql4/<Module>/Collection.php

  1. <?php
  2.  
  3. class <Namespace>_<Module>_Model_Mysql4_<Module>_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
  4. {
  5.     public function _construct()
  6.     {
  7.         //parent::__construct();
  8.         $this->_init('<module>/<module>');
  9.     }
  10. }

SQL Setup

/app/code/local/<Namespace>/<Module>/sql/<module>_setup/mysql4-install-0.1.0.php

  1. <?php
  2.  
  3. $installer = $this;
  4.  
  5. $installer->startSetup();
  6.  
  7. $installer->run("
  8.  
  9. -- DROP TABLE IF EXISTS {$this->getTable('<module>')};
  10. CREATE TABLE {$this->getTable('<module>')} (
  11.   `<module>_id` int(11) unsigned NOT NULL auto_increment,
  12.   `title` varchar(255) NOT NULL default '',
  13.   `content` text NOT NULL default '',
  14.   `status` smallint(6) NOT NULL default '0',
  15.   `created_time` datetime NULL,
  16.   `update_time` datetime NULL,
  17.   PRIMARY KEY (`<module>_id`)
  18. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  19.  
  20.     ");
  21.  
  22. $installer->endSetup();

NOTE: Please note the <module> text that needs to be replaced. This SQL structure is up to you, this is merely a starting point.

Template Design

/app/design/frontend/<interface>/<theme>/layout/<module>.xml

  1. <?xml version="1.0"?>
  2. <layout version="0.1.0">
  3.     <[module]_index_index>
  4.         <reference name="content">
  5.             <block type="[module]/[module]" name="[module]" />
  6.         </reference>
  7.     </[module]_index_index>
  8. </layout>

NOTE: The block type will automatically figure out what template file to use based on the second [module] declaration.

As an alternate way of declaring what template file to use you can use this: /app/design/frontend/<interface>/<theme>/layout/<module>.xml

  1. <?xml version="1.0"?>
  2. <layout version="0.1.0">
  3.     <[module]_index_index>
  4.         <reference name="content">
  5.             <block type="core/template" name="[module]" template="[module]/[module].phtml" />
  6.         </reference>
  7.     </[module]_index_index>
  8. </layout>

/app/design/frontend/<interface>/<theme>/template/<module>/<module>.phtml

  1. <h4><?php echo $this->__('Module List') ?></h4>
  2.  
  3. <?php   
  4.         /*
  5.         This will load one record from your database table.
  6.         load(<module>_id) will load whatever ID number you give it.
  7.         */
  8.     /*
  9.     $news = Mage::getModel('<module>/<module>')->load(1);
  10.     echo $news->get<Module>Id();
  11.     echo $news->getTitle();
  12.     echo $news->getContent();
  13.     echo $news->getStatus();
  14.     */
  15.  
  16.         /*
  17.         This block of code loads all of the records in the database table.
  18.         It will iterate through the collection and the first thing it will do
  19.         is set the Title to the current value of $i which is incremented each
  20.         iteration and then echo that value back out.  At the very end it will
  21.         save the entire collection.
  22.         */
  23.     /*
  24.     $i = 0;
  25.          
  26.     $collection = Mage::getModel('<module>/<module>')->getCollection();
  27.     $collection->setPageSize(5);
  28.     $collection->setCurPage(2);
  29.     $size = $collection->getSize();
  30.     $cnt = count($collection);
  31.     foreach ($collection as $item) {
  32.         $i = $i+1;
  33.         $item->setTitle($i);
  34.         echo $item->getTitle();
  35.     }
  36.  
  37.     $collection->walk('save');   
  38.     */
  39.  
  40.         /*
  41.         This shows how to load one value, change something and save it.
  42.         */
  43.  
  44.     /*
  45.     $object = Mage::getModel('<module>/<module>')->load(1);
  46.     $object->setTitle('This is a changed title');
  47.     $object->save();
  48.     */
  49. ?>

NOTE: Uncomment anything that you would like to use and this is just a starting point and some common methods for you to try and pull the data out.

In this section I am utilizing the built-in Grid Widgets and form capabilities to create a form to allow editing and creating new items for your custom database.

Directory Additions

Here is the revised directory setup due to the additions and changes we need for the backend module.

/app/code/local/<Namespace>/<Module>/

  1. Block/
  2.     Adminhtml/
  3.         <Module>/
  4.             Edit/
  5.                 Tab/
  6. controllers/
  7.     Adminhtml/
  8. etc/
  9. Helper/
  10. Model/
  11.     Mysql4/
  12.         <Module>/
  13. sql/
  14.     <module>_setup/

Blocks

These control the setup and appearance of your grids and the options that they display.

NOTE: Please note the fact that Block comes before Adminhtml in the class declaration. In any of the Magento modules in Adminhtml it is the opposite. For your module to work it has to be Block_Adminhtml otherwise you will get a ‘Cannot redeclare module...’ error.

/app/code/local/<Namespace>/<Module>/Block/Adminhtml/<Module>.php

  1. <?php
  2.  
  3. class <Namespace>_<Module>_Block_Adminhtml_<Module> extends Mage_Adminhtml_Block_Widget_Grid_Container
  4. {
  5.     public function __construct()
  6.     {
  7.         $this->_controller = 'adminhtml_<module>';
  8.         $this->_blockGroup = '<module>';
  9.         $this->_headerText = Mage::helper('<module>')->__('Item Manager');
  10.         $this->_addButtonLabel = Mage::helper('<module>')->__('Add Item');
  11.         parent::__construct();
  12.     }
  13. }

/app/code/local/<Namespace>/<Module>/Block/Adminhtml/<Module>/Edit.php

  1. <?php
  2.  
  3. class <Namespace>_<Module>_Block_Adminhtml_<Module>_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
  4. {
  5.     public function __construct()
  6.     {
  7.         parent::__construct();
  8.                
  9.         $this->_objectId = 'id';
  10.         $this->_blockGroup = '<module>';
  11.         $this->_controller = 'adminhtml_<module>';
  12.  
  13.         $this->_updateButton('save', 'label', Mage::helper('<module>')->__('Save Item'));
  14.         $this->_updateButton('delete', 'label', Mage::helper('<module>')->__('Delete Item'));
  15.     }
  16.  
  17.     public function getHeaderText()
  18.     {
  19.         if( Mage::registry('<module>_data') && Mage::registry('<module>_data')->getId() ) {
  20.             return Mage::helper('<module>')->__("Edit Item '%s'", $this->htmlEscape(Mage::registry('<module>_data')->getTitle()));
  21.         } else {
  22.             return Mage::helper('<module>')->__('Add Item');
  23.         }
  24.     }
  25. }

/app/code/local/<Namespace>/<Module>/Block/Adminhtml/<Module>/Grid.php

  1. <?php
  2.  
  3. class <Namespace>_<Module>_Block_Adminhtml_<Module>_Grid extends Mage_Adminhtml_Block_Widget_Grid
  4. {
  5.     public function __construct()
  6.     {
  7.         parent::__construct();
  8.         $this->setId('<module>Grid');
  9.         // This is the primary key of the database
  10.         $this->setDefaultSort('<module>_id');
  11.         $this->setDefaultDir('ASC');
  12.         $this->setSaveParametersInSession(true);
  13.     }
  14.  
  15.     protected function _prepareCollection()
  16.     {
  17.         $collection = Mage::getModel('<module>/<module>')->getCollection();
  18.         $this->setCollection($collection);
  19.         return parent::_prepareCollection();
  20.     }
  21.  
  22.     protected function _prepareColumns()
  23.     {
  24.         $this->addColumn('<module>_id', array(
  25.             'header'    => Mage::helper('<module>')->__('ID'),
  26.             'align'     =>'right',
  27.             'width'     => '50px',
  28.             'index'     => '<module>_id',
  29.         ));
  30.  
  31.         $this->addColumn('title', array(
  32.             'header'    => Mage::helper('<module>')->__('Title'),
  33.             'align'     =>'left',
  34.             'index'     => 'title',
  35.         ));
  36.  
  37.         /*
  38.         $this->addColumn('content', array(
  39.             'header'    => Mage::helper('<module>')->__('Item Content'),
  40.             'width'     => '150px',
  41.             'index'     => 'content',
  42.         ));
  43.         */
  44.  
  45.         $this->addColumn('created_time', array(
  46.             'header'    => Mage::helper('<module>')->__('Creation Time'),
  47.             'align'     => 'left',
  48.             'width'     => '120px',
  49.             'type'      => 'date',
  50.             'default'   => '--',
  51.             'index'     => 'created_time',
  52.         ));
  53.  
  54.         $this->addColumn('update_time', array(
  55.             'header'    => Mage::helper('<module>')->__('Update Time'),
  56.             'align'     => 'left',
  57.             'width'     => '120px',
  58.             'type'      => 'date',
  59.             'default'   => '--',
  60.             'index'     => 'update_time',
  61.         ));   
  62.  
  63.  
  64.         $this->addColumn('status', array(
  65.  
  66.             'header'    => Mage::helper('<module>')->__('Status'),
  67.             'align'     => 'left',
  68.             'width'     => '80px',
  69.             'index'     => 'status',
  70.             'type'      => 'options',
  71.             'options'   => array(
  72.                 1 => 'Active',
  73.                 0 => 'Inactive',
  74.             ),
  75.         ));
  76.  
  77.         return parent::_prepareColumns();
  78.     }
  79.  
  80.     public function getRowUrl($row)
  81.     {
  82.         return $this->getUrl('*/*/edit', array('id' => $row->getId()));
  83.     }
  84.  
  85.  
  86. }

/app/code/local/<Namespace>/<Module>/Block/Adminhtml/<Module>/Edit/Form.php

  1. <?php
  2.  
  3. class <Namespace>_<Module>_Block_Adminhtml_<Module>_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
  4. {
  5.     protected function _prepareForm()
  6.     {
  7.         $form = new Varien_Data_Form(array(
  8.                                         'id' => 'edit_form',
  9.                                         'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
  10.                                         'method' => 'post',
  11.                                      )
  12.         );
  13.  
  14.         $form->setUseContainer(true);
  15.         $this->setForm($form);
  16.         return parent::_prepareForm();
  17.     }
  18. }

/app/code/local/<Namespace>/<Module>/Block/Adminhtml/<Module>/Edit/Tabs.php

  1. <?php
  2.  
  3. class <Namespace>_<Module>_Block_Adminhtml_<Module>_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs
  4. {
  5.  
  6.     public function __construct()
  7.     {
  8.         parent::__construct();
  9.         $this->setId('<module>_tabs');
  10.         $this->setDestElementId('edit_form');
  11.         $this->setTitle(Mage::helper('<module>')->__('News Information'));
  12.     }
  13.  
  14.     protected function _beforeToHtml()
  15.     {
  16.         $this->addTab('form_section', array(
  17.             'label'     => Mage::helper('<module>')->__('Item Information'),
  18.             'title'     => Mage::helper('<module>')->__('Item Information'),
  19.             'content'   => $this->getLayout()->createBlock('<module>/adminhtml_<module>_edit_tab_form')->toHtml(),
  20.         ));
  21.        
  22.         return parent::_beforeToHtml();
  23.     }
  24. }

/app/code/local/<Namespace>/<Module>/Block/Adminhtml/<Module>/Edit/Tab/Form.php

  1. <?php
  2.  
  3. class <Namespace>_<Module>_Block_Adminhtml_<Module>_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
  4. {
  5.     protected function _prepareForm()
  6.     {
  7.         $form = new Varien_Data_Form();
  8.         $this->setForm($form);
  9.         $fieldset = $form->addFieldset('<module>_form', array('legend'=>Mage::helper('<module>')->__('Item information')));
  10.        
  11.         $fieldset->addField('title', 'text', array(
  12.             'label'     => Mage::helper('<module>')->__('Title'),
  13.             'class'     => 'required-entry',
  14.             'required'  => true,
  15.             'name'      => 'title',
  16.         ));
  17.  
  18.         $fieldset->addField('status', 'select', array(
  19.             'label'     => Mage::helper('<module>')->__('Status'),
  20.             'name'      => 'status',
  21.             'values'    => array(
  22.                 array(
  23.                     'value'     => 1,
  24.                     'label'     => Mage::helper('<module>')->__('Active'),
  25.                 ),
  26.  
  27.                 array(
  28.                     'value'     => 0,
  29.                     'label'     => Mage::helper('<module>')->__('Inactive'),
  30.                 ),
  31.             ),
  32.         ));
  33.        
  34.         $fieldset->addField('content', 'editor', array(
  35.             'name'      => 'content',
  36.             'label'     => Mage::helper('<module>')->__('Content'),
  37.             'title'     => Mage::helper('<module>')->__('Content'),
  38.             'style'     => 'width:98%; height:400px;',
  39.             'wysiwyg'   => false,
  40.             'required'  => true,
  41.         ));
  42.        
  43.         if ( Mage::getSingleton('adminhtml/session')->get<Module>Data() )
  44.         {
  45.             $form->setValues(Mage::getSingleton('adminhtml/session')->get<Module>Data());
  46.             Mage::getSingleton('adminhtml/session')->set<Module>Data(null);
  47.         } elseif ( Mage::registry('<module>_data') ) {
  48.             $form->setValues(Mage::registry('<module>_data')->getData());
  49.         }
  50.         return parent::_prepareForm();
  51.     }
  52. }

Controller

/app/code/local/<Namespace>/<Module>/controllers/Adminhtml/<Module>Controller.php

NOTE: you need to manually add line 16, which is currently missing in this file. As per suggestion from mkd at page http://www.magentocommerce.com/boards/viewthread/11228/

  1. <?php
  2.  
  3. class <Namespace>_<Module>_Adminhtml_<Module>Controller extends Mage_Adminhtml_Controller_Action
  4. {
  5.  
  6.     protected function _initAction()
  7.     {
  8.         $this->loadLayout()
  9.             ->_setActiveMenu('<module>/items')
  10.             ->_addBreadcrumb(Mage::helper('adminhtml')->__('Items Manager'), Mage::helper('adminhtml')->__('Item Manager'));
  11.         return $this;
  12.     }   
  13.    
  14.     public function indexAction() {
  15.         $this->_initAction();       
  16.         $this->_addContent($this->getLayout()->createBlock('<module>/adminhtml_<module>'));
  17.         $this->renderLayout();
  18.     }
  19.  
  20.     public function editAction()
  21.     {
  22.         $<module>Id     = $this->getRequest()->getParam('id');
  23.         $<module>Model  = Mage::getModel('<module>/<module>')->load($<module>Id);
  24.  
  25.         if ($<module>Model->getId() || $<module>Id == 0) {
  26.  
  27.             Mage::register('<module>_data', $<module>Model);
  28.  
  29.             $this->loadLayout();
  30.             $this->_setActiveMenu('<module>/items');
  31.            
  32.             $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item Manager'), Mage::helper('adminhtml')->__('Item Manager'));
  33.             $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item News'), Mage::helper('adminhtml')->__('Item News'));
  34.            
  35.             $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
  36.            
  37.             $this->_addContent($this->getLayout()->createBlock('<module>/adminhtml_<module>_edit'))
  38.                  ->_addLeft($this->getLayout()->createBlock('<module>/adminhtml_<module>_edit_tabs'));
  39.                
  40.             $this->renderLayout();
  41.         } else {
  42.             Mage::getSingleton('adminhtml/session')->addError(Mage::helper('<module>')->__('Item does not exist'));
  43.             $this->_redirect('*/*/');
  44.         }
  45.     }
  46.    
  47.     public function newAction()
  48.     {
  49.         $this->_forward('edit');
  50.     }
  51.    
  52.     public function saveAction()
  53.     {
  54.         if ( $this->getRequest()->getPost() ) {
  55.             try {
  56.                 $postData = $this->getRequest()->getPost();
  57.                 $<module>Model = Mage::getModel('<module>/<module>');
  58.                
  59.                 $<module>Model->setId($this->getRequest()->getParam('id'))
  60.                     ->setTitle($postData['title'])
  61.                     ->setContent($postData['content'])
  62.                     ->setStatus($postData['status'])
  63.                     ->save();
  64.                
  65.                 Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully saved'));
  66.                 Mage::getSingleton('adminhtml/session')->set<Module>Data(false);
  67.  
  68.                 $this->_redirect('*/*/');
  69.                 return;
  70.             } catch (Exception $e) {
  71.                 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  72.                 Mage::getSingleton('adminhtml/session')->set<Module>Data($this->getRequest()->getPost());
  73.                 $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
  74.                 return;
  75.             }
  76.         }
  77.         $this->_redirect('*/*/');
  78.     }
  79.    
  80.     public function deleteAction()
  81.     {
  82.         if( $this->getRequest()->getParam('id') > 0 ) {
  83.             try {
  84.                 $<module>Model = Mage::getModel('<module>/<module>');
  85.                
  86.                 $<module>Model->setId($this->getRequest()->getParam('id'))
  87.                     ->delete();
  88.                    
  89.                 Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully deleted'));
  90.                 $this->_redirect('*/*/');
  91.             } catch (Exception $e) {
  92.                 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  93.                 $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
  94.             }
  95.         }
  96.         $this->_redirect('*/*/');
  97.     }
  98.     /**
  99.      * Product grid for AJAX request.
  100.      * Sort and filter result for example.
  101.      */
  102.     public function gridAction()
  103.     {
  104.         $this->loadLayout();
  105.         $this->getResponse()->setBody(
  106.                $this->getLayout()->createBlock('importedit/adminhtml_<module>_grid')->toHtml()
  107.         );
  108.     }
  109. }

XML Configuration Changes

/app/code/local/<Namespace>/<Module>/etc/config.xml

  1. <?xml version="1.0"?>
  2. <config>
  3.     <modules>
  4.         <[Namespace]_[Module]>
  5.             <version>0.1.0</version>
  6.         </[Namespace]_[Module]>
  7.     </modules>
  8.     <frontend>
  9.         <routers>
  10.             <[module]>
  11.                 <use>standard</use>
  12.                 <args>
  13.                     <module>[Namespace]_[Module]</module>
  14.                     <frontName>[module]</frontName>
  15.                 </args>
  16.             </[module]>
  17.         </routers>
  18.         <layout>
  19.             <updates>
  20.                 <[module]>
  21.                     <file>[module].xml</file>
  22.                 </[module]>
  23.             </updates>
  24.         </layout>
  25.     </frontend>
  26.     <admin>
  27.         <routers>
  28.             <[module]>
  29.                 <use>admin</use>
  30.                 <args>
  31.                     <module>[Namespace]_[Module]</module>
  32.                     <frontName>[module]</frontName>
  33.                 </args>
  34.             </[module]>
  35.         </routers>
  36.     </admin>
  37.     <adminhtml>
  38.         <menu>
  39.             <[module] module="[module]">
  40.                 <title>[Module]</title>
  41.                 <sort_order>71</sort_order>               
  42.                 <children>
  43.                     <items module="[module]">
  44.                         <title>Manage Items</title>
  45.                         <sort_order>0</sort_order>
  46.                         <action>[module]/adminhtml_[module]</action>
  47.                     </items>
  48.                 </children>
  49.             </[module]>
  50.         </menu>
  51.         <acl>
  52.             <resources>
  53.                 <all>
  54.                     <title>Allow Everything</title>
  55.                 </all>
  56.                 <admin>
  57.                     <children>
  58.                         <[module]>
  59.                             <title>[Module] Module</title>
  60.                             <sort_order>200</sort_order>
  61.                         </[module]>
  62.                     </children>
  63.                 </admin>
  64.             </resources>   
  65.         </acl>
  66.         <layout>
  67.             <updates>
  68.                 <[module]>
  69.                     <file>[module].xml</file>
  70.                 </[module]>
  71.             </updates>
  72.         </layout>
  73.     </adminhtml>   
  74.     <global>
  75.         <models>
  76.             <[module]>
  77.                 <class>[Namespace]_[Module]_Model</class>
  78.                 <resourceModel>[module]_mysql4</resourceModel>
  79.             </[module]>
  80.             <[module]_mysql4>
  81.                 <class>[Namespace]_[Module]_Model_Mysql4</class>
  82.                 <entities>
  83.                     <[module]>
  84.                         <table>[module]</table>
  85.                     </[module]>
  86.                 </entities>
  87.             </[module]_mysql4>
  88.         </models>
  89.         <resources>
  90.             <[module]_setup>
  91.                 <setup>
  92.                     <module>[Namespace]_[Module]</module>
  93.                 </setup>
  94.                 <connection>
  95.                     <use>core_setup</use>
  96.                 </connection>
  97.             </[module]_setup>
  98.             <[module]_write>
  99.                 <connection>
  100.                     <use>core_write</use>
  101.                 </connection>
  102.  
  103.  
  104.             </[module]_write>
  105.             <[module]_read>
  106.                 <connection>
  107.                     <use>core_read</use>
  108.                 </connection>
  109.             </[module]_read>
  110.         </resources>
  111.         <blocks>
  112.             <[module]>
  113.                 <class>[Namespace]_[Module]_Block</class>
  114.             </[module]>
  115.         </blocks>
  116.         <helpers>
  117.             <[module]>
  118.                 <class>[Namespace]_[Module]_Helper</class>
  119.             </[module]>
  120.         </helpers>
  121.     </global>
  122. </config>

XML Layout

/app/design/adminhtml/<interface>/<theme>/layout/<module>.xml

  1. <?xml version="1.0"?>
  2. <layout version="0.1.0">
  3.     <[module]_adminhtml_[module]_index>
  4.         <reference name="content">
  5.             <block type="[module]/adminhtml_[module]" name="[module]" />
  6.         </reference>
  7.     </[module]_adminhtml_[module]_index>
  8. </layout>



 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs