I am trying to add a custom tab in magento backened Promotions/shopping cart price rules. What I need is when I choose any of rule, a custom tab shows below "Manage Coupons Code" and when I click on custom tab I must get a grid table with save and save and continue edit buttons. Can anyone suggest me the right way to do this ?
This is the window where I want to add new custom tab below "Manage Coupons Code":
For full explanation you can refer this http://www.newgenray.com/2015/04/22/creating-custom-tab-on-edit-page-in-an-existing-module-of-magento/
First you need to create a module and you have to define two things one is layout and block
<config>
<modules>
<Newgenray_Coupon>
<version>0.0.1</version>
</Newgenray_Coupon>
</modules>
<adminhtml>
<layout>
<updates>
<newgenray_coupon>
<file>coupon.xml</file>
</newgenray_coupon>
</updates>
</layout>
</adminhtml>
<global>
<blocks>
<newgenray_coupon>
<class>Newgenray_Coupon_Block</class>
</newgenray_coupon>
</blocks>
</global>
Then in file app/code/local/Newgenray/Block/Adminhtml/Promo/Quote/Edit/Tab/Custom.php
class Newgenray_Coupon_Block_Adminhtml_Promo_Quote_Edit_Tab_Custom extends Mage_Adminhtml_Block_Widget_Form
implements Mage_Adminhtml_Block_Widget_Tab_Interface
{
/*public function __construct(){
$this->setTemplate('newgenray_coupon/custom_tab.phtml');
}*/
/**
* Mandatory to override as we are implementing Widget Tab interface
* Return Tab Title
*
* #return string
*/
public function getTabTitle(){
return Mage::helper('salesrule')->__('Custom Tab');
}
/**
* Mandatory to override as we are implementing Widget Tab interface
* Return Tab Label
*
* #return string
*/
public function getTabLabel(){
return Mage::helper('salesrule')->__('Custom Tab');
}
/**
* Mandatory to override as we are implementing Widget Tab interface
* Can show tab in tabs
* Here you can write condition when the tab should we shown or not. Like you see when we create shopping cart rule
* Manage coupon tab doesn't come. If you want that then just make a function and check whether you have information
* in registry or not
*
* #return boolean
*/
public function canShowTab(){
return true;
}
/**
* Mandatory to override as we are implementing Widget Tab interface
* Tab is Hidden
*
* #return boolean
*/
public function isHidden(){
return false;
}
/**
* Defines after which tab this tab should come like you asked you need it below Manage Coupon codes
*
* #return string
*/
public function getAfter(){
return 'coupons_section';
}
public function _prepareForm(){
/* To set the data in the form you need to get the data in registry In my example
* I don't have any registry so I am commenting it. My Form will be blank
*/
//$model = Mage::registry('custom_tab_form_data');
$form = new Varien_Data_Form();
$form->setHtmlIdPrefix('rule_');
$fieldset = $form->addFieldset('custom_fieldset', array(
'legend'=>Mage::helper('salesrule')->__('Your Custom Field Set ')
));
$fieldset->addField('name', 'text', array(
'label' => Mage::helper('salesrule')->__('Name'),
'class' => 'required-entry',
'required' => true,
'name' => 'name',
'note' => Mage::helper('salesrule')->__('The name of the example.'),
));
$fieldset->addField('description', 'text', array(
'label' => Mage::helper('salesrule')->__('Description'),
'class' => 'required-entry',
'required' => true,
'name' => 'description',
));
$fieldset->addField('other', 'text', array(
'label' => Mage::helper('salesrule')->__('Other'),
'class' => 'required-entry',
'required' => true,
'name' => 'other',
));
//$form->setValues($model->getData());
$this->setForm($form);
return parent::_prepareForm();
}
}
Then in file app/app/design/adminhtml/default/default/layout/coupon.xml
<?xml version="1.0"?>
<layout version="0.1.0">
<adminhtml_promo_quote_edit>
<reference name="promo_quote_edit_tabs">
<action method="addTab">
<name>promo_quote_edit_tab_custom</name>
<block>newgenray_coupon/adminhtml_promo_quote_edit_tab_custom</block>
</action>
</reference>
</adminhtml_promo_quote_edit>
</layout>
Last step will be activating the module that we have just created
<?xml version="1.0"?>
<config>
<modules>
<Newgenray_Coupon>
<active>true</active>
<codePool>local</codePool>
</Newgenray_Coupon>
</modules>
</config>
Related
I want to add new column in my existing table. I created an upgradeSchema.php file. And changed version in module.xml file. But after executing upgrade command, nothing happened. Here is my code
<?php
namespace Bridge\Tradeuser\Setup;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
class UpgradeSchema implements UpgradeSchemaInterface
{
/**
* {#inheritdoc}
*/
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context) {
$installer = $setup;
$installer->startSetup();
if (version_compare($context->getVersion(), '3.0.1', '<')) {
$installer->getConnection()
->addColumn(
$installer->getTable('batchcode_entity'),
'status',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 50,
'nullable' => false,
'default' => 'Active',
'comment' => 'Batchcode status'
]
);
}
$installer->endSetup();
}
}
?>
Then I run the following commands:
bin/magento setup:upgrade
bin/magento setup:static-content:deploy -f
bin/magento setup:di:compile
bin/magento cache:flush
Hey Try using this method just change the version number and columns names to match it, I tried right now and it worked
<?php
namespace SimplifiedMagento\Database\Setup;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Db\Ddl\Table;
class UpgradeSchema implements UpgradeSchemaInterface
{
/**
* Upgrades DB schema for a module
*
* #param SchemaSetupInterface $setup
* #param ModuleContextInterface $context
* #return void
*/
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
if(version_compare($context->getVersion(),'0.0.2','<'))
{
//if version of your project is lower than 0.0.2 then implement this
$setup->getConnection()->addColumn(
$setup->getTable('affiliate_member'),
'phonenumber',
['nullable' => false,'type' => Table::TYPE_TEXT,'comment' => 'Phone number column']
);
}
$setup->endSetup();
}
}
But make sure your module.xml file is having similar version which you wrote in if condition:
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="SimplifiedMagento_Database" setup_version="0.0.2">
</module>
</config>
I'm trying to make product SKU attribute not editable in Admin Products->Inventory->Catalog.
I'm using this previous response as an example: Magento read-only and hidden product attributes
I've created an Observer to use lockAttribute("attribute_code") method.
Here is the Vendor/Module/etc/adminhtml/events.xml:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="catalog_product_edit_action">
<observer name="vendor_admin_lock" instance="Vendor\Module\Observer\ProductLockAttributes" />
</event>
</config>
And in ProductLockAttributes.php:
class ProductLockAttributes implements ObserverInterface
{
/**
* #param \Magento\Framework\Event\Observer $observer
* #return void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$event = $observer->getEvent();
$product = $event->getProduct();
$product->lockAttribute('sku');
$product->lockAttribute('sku_type');
}
}
The event gets called but in the admin interface when editing a product the SKU is still editable.
Can anyone tell me what is wrong with my code?
Thanks.
I currently use a extention to display the zipcode and SKU inside the grid.
I want to change this, so that the SKU is replaced by my new attribute with code "dpn".
How should I expand my code?
CODE:
app/code/local/Atwix/ExtendedGrid/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Atwix_ExtendedGrid>
<version>1.0.1</version>
</Atwix_ExtendedGrid>
</modules>
<global>
<blocks>
<atwix_extendedgrid>
<class>Atwix_ExtendedGrid_Block</class>
</atwix_extendedgrid>
</blocks>
<helpers>
<atwix_extendedgrid>
<class>Atwix_ExtendedGrid_Helper</class>
</atwix_extendedgrid>
</helpers>
<models>
<atwix_extendedgrid>
<class>Atwix_ExtendedGrid_Model</class>
</atwix_extendedgrid>
<sales_resource>
<rewrite>
<order_grid_collection>Atwix_ExtendedGrid_Model_Resource_Sales_Order_Grid_Collection</order_grid_collection>
</rewrite>
</sales_resource>
</models>
</global>
<adminhtml>
<events>
<sales_order_grid_collection_load_before>
<observers>
<atwix_exgrid>
<model>atwix_extendedgrid/observer</model>
<method>salesOrderGridCollectionLoadBefore</method>
</atwix_exgrid>
</observers>
</sales_order_grid_collection_load_before>
</events>
<layout>
<updates>
<atwix_extendedgrid>
<file>atwix/extendedgrid.xml</file>
</atwix_extendedgrid>
</updates>
</layout>
</adminhtml>
</config>
app/code/local/Atwix/ExtendedGrid/Helper/Data.php
class Atwix_ExtendedGrid_Helper_Data extends Mage_Core_Helper_Abstract
{
/**
* parameters for addColumnAfter method
* #return array
*/
public function getSkusColumnParams()
{
return array(
'header' => 'SKU',
'index' => 'skus',
'type' => 'text',
'filter_condition_callback' => array('Atwix_ExtendedGrid_Model_Observer', 'filterSkus'),
);
}
public function getPostcodeColumnParams()
{
return array(
'header' => 'Postcode',
'index' => 'postcode',
'type' => 'text',
);
}
}
app/code/local/Atwix/ExtendedGrid/Model/Observer.php
class Atwix_ExtendedGrid_Model_Observer
{
/**
* Joins extra tables for adding custom columns to Mage_Adminhtml_Block_Sales_Order_Grid
* #param Varien_Object $observer
* #return Atwix_Exgrid_Model_Observer
*/
public function salesOrderGridCollectionLoadBefore($observer)
{
$collection = $observer->getOrderGridCollection();
$select = $collection->getSelect();
$select->joinLeft(array('payment' => $collection->getTable('sales/order_payment')), 'payment.parent_id=main_table.entity_id', array('payment_method' => 'method'));
$select->join(
array('address' => $collection->getTable("sales/order_address")),
'main_table.entity_id = address.parent_id AND address.address_type = "shipping"',
array('postcode')
);
$select->join('sales_flat_order_item', '`sales_flat_order_item`.order_id=`main_table`.entity_id', array('skus' => new Zend_Db_Expr('group_concat(`sales_flat_order_item`.sku SEPARATOR ", ")')));
$select->group('main_table.entity_id');
}
/**
* callback function used to filter collection
* #param $collection
* #param $column
* #return $this
*/
public function filterSkus($collection, $column)
{
if (!$value = $column->getFilter()->getValue()) {
return $this;
}
$collection->getSelect()->having(
"group_concat(`sales_flat_order_item`.sku SEPARATOR ', ') like ?", "%$value%");
return $this;
}
}
app/code/local/Atwix/ExtendedGrid/Model/Resource/Sales/Order/Grid/Collection.php
class Atwix_ExtendedGrid_Model_Resource_Sales_Order_Grid_Collection extends Mage_Sales_Model_Resource_Order_Grid_Collection
{
/**
* Get SQL for get record count
*
* #return Varien_Db_Select
*/
public function getSelectCountSql()
{
$countSelect = parent::getSelectCountSql();
if (Mage::app()->getRequest()->getControllerName() == 'sales_order') {
$countSelect->reset(Zend_Db_Select::GROUP);
$countSelect->reset(Zend_Db_Select::COLUMNS);
$countSelect->columns("COUNT(DISTINCT main_table.entity_id)");
$havingCondition = $countSelect->getPart(Zend_Db_Select::HAVING);
if (count($havingCondition)) {
$countSelect->where(
str_replace("group_concat(`sales_flat_order_item`.sku SEPARATOR ', ')", 'sales_flat_order_item.sku', $havingCondition[0])
);
$countSelect->reset(Zend_Db_Select::HAVING);
}
}
return $countSelect;
}
/**
* Init select
* #return Mage_Core_Model_Resource_Db_Collection_Abstract
*/
protected function _initSelect()
{
$this->addFilterToMap('store_id', 'main_table.store_id')
->addFilterToMap('created_at', 'main_table.created_at')
->addFilterToMap('updated_at', 'main_table.updated_at');
return parent::_initSelect();
}
}
app/design/adminhtml/default/default/layout/atwix/extendedgrid.xml
<layout>
<sales_order_grid_update_handle>
<reference name="sales_order.grid">
<action method="addColumnAfter">
<columnId>payment_method</columnId>
<arguments>
<header>Betaalmethode</header>
<index>payment_method</index>
<filter_index>payment.method</filter_index>
<type>text</type>
</arguments>
<after>shipping_name</after>
</action>
<action method="addColumnAfter">
<columnId>skus</columnId>
<arguments helper="atwix_extendedgrid/getSkusColumnParams" />
<after>payment_method</after>
</action>
<action method="addColumnAfter">
<columnId>postcode</columnId>
<arguments helper="atwix_extendedgrid/getPostcodeColumnParams" />
<after>skus</after>
</action>
</reference>
</sales_order_grid_update_handle>
<adminhtml_sales_order_grid>
<!-- apply layout handle defined above -->
<update handle="sales_order_grid_update_handle" />
</adminhtml_sales_order_grid>
<adminhtml_sales_order_index>
<!-- apply layout handle defined above -->
<update handle="sales_order_grid_update_handle" />
</adminhtml_sales_order_index>
</layout>
I'm trying to add a custom tab in catalog, I'm following FishPig's tutorial.
So I want to achieve something like this
I have followed every instructions in the tutorial but still can't get it right.
I have disabled the catching and enabled debugging. I have checked the logs but I don't get any errors related this module.
My code
app/etc/modules/Fishpig_Customtabs.xml
<config>
<modules>
<Fishpig_Customtabs>
<active>true</active>
<codePool>local</codePool>
</Fishpig_Customtabs>
</modules>
</config>
app/code/local/Fishpig/Customtabs/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Fishpig_CustomTabs>
<version>0.1.0</version>
</Fishpig_CustomTabs>
</modules>
<global>
<blocks>
<customtabs>
<class>Fishpig_Customtabs_Block</class>
</customtabs>
</blocks>
<models>
<customtabs>
<class>Fishpig_Customtabs_Model</class>
</customtabs>
</models>
</global>
<adminhtml>
<layout>
<updates>
<customtabs>
<file>customtabs.xml</file>
</customtabs>
</updates>
</layout>
<events>
<catalog_product_save_after>
<observers>
<fishpig_save_product_data>
<type>singleton</type>
<class>customtabs/observer</class>
<method>saveProductTabData</method>
</fishpig_save_product_data>
</observers>
</catalog_product_save_after>
</events>
</adminhtml>
</config>
app/code/local/Fishpig/Customtabs/Block/Adminhtml/Catalog/Product/Tab.php
<?php
class Fishpig_Customtabs_Block_Adminhtml_Catalog_Product_Tab
extends Mage_Adminhtml_Block_Template
implements Mage_Adminhtml_Block_Widget_Tab_Interface {
/**
* Set the template for the block
*
*/
public function _construct()
{
parent::_construct();
$this->setTemplate('customtabs/catalog/product/tab.phtml');
}
/**
* Retrieve the label used for the tab relating to this block
*
* #return string
*/
public function getTabLabel()
{
return $this->__('My Custom Tab');
}
/**
* Retrieve the title used by this tab
*
* #return string
*/
public function getTabTitle()
{
return $this->__('Click here to view your custom tab content');
}
/**
* Determines whether to display the tab
* Add logic here to decide whether you want the tab to display
*
* #return bool
*/
public function canShowTab()
{
return true;
}
/**
* Stops the tab being hidden
*
* #return bool
*/
public function isHidden()
{
return false;
}
/**
* AJAX TAB's
* If you want to use an AJAX tab, uncomment the following functions
* Please note that you will need to setup a controller to recieve
* the tab content request
*
*/
/**
* Retrieve the class name of the tab
* Return 'ajax' here if you want the tab to be loaded via Ajax
*
* return string
*/
# public function getTabClass()
# {
# return 'my-custom-tab';
# }
/**
* Determine whether to generate content on load or via AJAX
* If true, the tab's content won't be loaded until the tab is clicked
* You will need to setup a controller to handle the tab request
*
* #return bool
*/
# public function getSkipGenerateContent()
# {
# return false;
# }
/**
* Retrieve the URL used to load the tab content
* Return the URL here used to load the content by Ajax
* see self::getSkipGenerateContent & self::getTabClass
*
* #return string
*/
# public function getTabUrl()
# {
# return null;
# }
}
app/design/adminhtml/default/default/layout/customtabs.xml
<?xml version="1.0"?>
<layout>
<adminhtml_catalog_product_edit>
<reference name="product_tabs">
<action method="addTab">
<name>my_custom_tab</name>
<block>customtabs/adminhtml_catalog_product_tab</block>
</action>
</reference>
</adminhtml_catalog_product_edit>
</layout>
app/design/adminhtml/default/default/template/customtabs/catalog/product/tab.phtml
<div class="input-field">
<label for="custom_field">Custom Field</label>
<input type="text" class="input-text" name="custom_field" id="custom_field" />
</div>
I have checked the code twice still the tab does not appear, am I missing something? How do I check if I have made a mistake in my module?
To be sure I have also added the model code
app/code/local/Fishpig/Customtabs/Model/Observer.php
class Fishpig_Customtabs_Model_Observer
{
/**
* Flag to stop observer executing more than once
*
* #var static bool
*/
static protected $_singletonFlag = false;
/**
* This method will run when the product is saved from the Magento Admin
* Use this function to update the product model, process the
* data or anything you like
*
* #param Varien_Event_Observer $observer
*/
public function saveProductTabData(Varien_Event_Observer $observer)
{
if (!self::$_singletonFlag) {
self::$_singletonFlag = true;
$product = $observer->getEvent()->getProduct();
try {
/**
* Perform any actions you want here
*
*/
$customFieldValue = $this->_getRequest()->getPost('custom_field');
/**
* Uncomment the line below to save the product
*
*/
//$product->save();
}
catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
}
}
}
/**
* Retrieve the product model
*
* #return Mage_Catalog_Model_Product $product
*/
public function getProduct()
{
return Mage::registry('product');
}
/**
* Shortcut to getRequest
*
*/
protected function _getRequest()
{
return Mage::app()->getRequest();
}
}
Still it does not work. I'm new to magento and I have no idea what has gone wrong. Can someone please point out my mistake or a sample code?
My full code can be found here and the archive can be found here.
Your extension will not working because of the following points
1. Disable the Magento Compiler functionality
The Magento Compiler is found through System > Tools > Compilation. The compiler collects Magento's files and stores a compressed copy under /includes/src. Every time your files change, you must refresh the Compiler by clicking 'Run Compilation Process'. This both refreshes the compressed files as well as enable the Compiler.
Make sure the Compiler is disabled nefore continuing.
2. Check read/write permissions :
Although usually not necessary, in some cases you may need to verify the copied files permissions. On Unix/Linux systems you should set the files to readable by the server
3. Flush caches
Once you have disabled the compiler and copied the files it is time to flush the Magento caches. Go to System > Cache Management and flush all caches, including CSS, image and external caches.
3. Log out of Magento and log back in
Now it is necessary to log out of Magento's admin, and log back in. This is needed to enable any new permissions for the permission system.
**4 Confirm Extension Conflation **
Must be confirm your new extension whether conflict to another controller or no?
Look How to install an Appmerce Magento extension manually
In order to register the custom tab in the new product and the edit product
I changed my code to
app/design/adminhtml/default/default/layout/customtabs.xml
<?xml version="1.0"?>
<layout>
<adminhtml_catalog_product_new>
<reference name="product_tabs">
<action method="addTab">
<name>my_custom_tab</name>
<block>customtabs/adminhtml_catalog_product_tab</block>
</action>
</reference>
</adminhtml_catalog_product_new>
<adminhtml_catalog_product_edit>
<reference name="product_tabs">
<action method="addTab">
<name>my_custom_tab</name>
<block>customtabs/adminhtml_catalog_product_edit</block>
</action>
</reference>
</adminhtml_catalog_product_edit>
</layout>
and added another block called Edit.php besides Tab.php
I am trying to create a custom module in magento admin. I have reached the point where a new link has been added to the menu and by clicking on it, I can navigate to the index action of the controller of the module. But here I cannot see the grid, only the header text and the button which has been added in the block construct appear.
my namespace is: Mmnamespace
my module is: Mmmodule
i also created a table named mmmodule_event using the following commands
CREATE TABLE mmmodule_event (
`event_id` INTEGER AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(255),
`start` DATETIME,
`end` DATETIME,
`created_at` DATETIME,
`modified_at` DATETIME
);
there are currently two items in the table that i populated it with using magento's admin
I can see that since this block extends the Mage_Adminhtml_Block_Widget_Grid_Container class, it will by itself add the grid block inside this module as its child.
my config setup xml files are as follows:
my config.xml
<?xml version="1.0"?>
<!DOCTYPE config>
<!--
/**
* app/code/local/Mmnamespace/Mmmodule/etc/config.xml
*
*
*
*
* #author Omatsola Isaac Sobotie <tsola2002#yahoo.co.uk>
* #category Mmnamspace
* #package Mmmodule
*
*/
-->
<config>
<modules>
<Mmnamespace_Mmmodule>
<version>0.0.0</version>
</Mmnamespace_Mmmodule>
</modules>
<global>
<!-- this code tells magento to use resources -->
<models>
<mmmodule>
<class>Mmnamespace_Mmmodule_Model</class>
<resourceModel>mmmodule_resource</resourceModel>
</mmmodule>
<mmmodule_resource>
<class>Mmnamespace_Mmmodule_Model_Resource</class>
<entities>
<event>
<table>mmmodule_event</table>
</event>
<event_registrant>
<table>mmmodule_event_registrant</table>
</event_registrant>
</entities>
</mmmodule_resource>
</models>
<blocks>
<mmmodule>
<class>Mmnamespace_Mmmodule_Block</class>
</mmmodule>
</blocks>
<helpers>
<mmmodule>
<class>Mmnamespace_Mmmodule_Helper</class>
</mmmodule>
</helpers>
<!-- initializing a predispatch observer gets fired anytime a controller is about to render an action -->
<events>
<controller_action_predispatch>
<observers>
<mmmodule_observer>
<class>mmmodule/observer</class>
<method>controllerActionPredispatch</method>
</mmmodule_observer>
</observers>
</controller_action_predispatch>
</events>
</global>
<!-- routing front page menu to appropriate controller -->
<admin>
<routers>
<adminhtml>
<args>
<modules>
<mmmodule before="Mage_Adminhtml">Mmnamespace_Mmmodule_Adminhtml</mmmodule>
</modules>
</args>
</adminhtml>
</routers>
</admin>
<!--used to route urls with module name to module-->
<frontend>
<routers>
<mmmodule>
<use>standard</use>
<args>
<frontName>mmmodule</frontName>
<module>Mmnamespace_Mmmodule</module>
</args>
</mmmodule>
</routers>
<layout>
<updates>
<mmmodule>
<file>mmmodule.xml</file>
</mmmodule>
</updates>
</layout>
</frontend>
</config>
my adminhtml.xml
<?xml version="1.0"?>
<!DOCTYPE config>
<!--
/**
* app/code/local/Mmnamespace/Mmmodule/etc/adminhtml.xml
*
*
* #category Mmnamespace
* #package Mmmodule
*/
-->
<!-- this code will process urls with that front name -->
<config>
<menu>
<mmmodule translate="title" module="mmmodule">
<title>Events</title>
<sort_order>1000</sort_order>
<action>adminhtml/event</action>
</mmmodule>
</menu>
</config>
my system.xml
<?xml version="1.0"?>
<!DOCTYPE config>
<!--
/**
* app/code/local/Mmnamespace/Mmmodule/etc/system.xml
*
*
* #category Mmnamespace
* #package Mmmodule
*/
-->
<!-- this code adds a fieldset to an existing general section -->
<config>
<sections>
<general translate="label">
<groups>
<mmmodule translate="label">
<label>Mmmodule Options</label>
<sort_order>0</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<fields>
<some_field translate="label">
<label>Mmmodule Field</label>
<frontend_type>text</frontend_type> <!-- More frontend types can be found in the lib/Varien/Data/Form/Element folder -->
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</some_field>
</fields>
</mmmodule>
</groups>
</general>
</sections>
</config>
my Mmnamespace_Mmmodule.xml
<?xml version="1.0"?>
<!DOCTYPE config>
<!--
/**
* app/etc/modules/Mmnamespace_Mmmodule.xml
*
*
*
*
* #author Omatsola Isaac Sobotie <tsola2002#yahoo.co.uk>
* #category Mmnamespace
* #package Mmodule
*
*/
-->
<config>
<modules>
<Mmnamespace_Mmmodule>
<active>true</active>
<codePool>local</codePool>
<depends />
</Mmnamespace_Mmmodule>>
</modules>
</config>
my EventController.php
<?php
/**
*
* Package: magentocore
* Filename: MmmoduleController.php
* Author: solidstunna101
* Date: 25/02/14
* Time: 18:40
* * app/code/local/Mmnamespace/controllers/Adminhtml/EventController.php
*/
class Mmnamespace_Mmmodule_Adminhtml_EventController extends Mage_Adminhtml_Controller_Action
{
//this function adds block content to main layout
public function indexAction()
{
$this->loadLayout();
$this->_setActiveMenu('mmmodule/events');
/*$this->_addContent(
$this->getLayout()->createBlock('mmmodule/adminhtml_event_edit')
);*/
$this->_addContent(
$this->getLayout()->createBlock('mmmodule/adminhtml_event')
);
return $this->renderLayout();
}
public function saveAction()
{
//gathering form field parameters from the url
$eventId = $this->getRequest()->getParam('event_id');
$eventModel = Mage::getModel('mmmodule/event')->load($eventId);
//save event to database
if ( $data = $this->getRequest()->getPost() ) {
try {
$eventModel->addData($data)->save();
Mage::getSingleton('adminhtml/session')->addSuccess(
$this->__("Your event has been saved!")
);
} catch ( Exception $e ) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
}
}
$this->_redirect('*/*/index');
}
}
my event.php block
<?php
/**
*
* Package: magentocore
* Filename: Event.php
* Author: solidstunna101
* Date: 27/02/14
* Time: 08:14
* Location: app/code/local/Mmnamespace/Mmmodule/Block/Adminhtml/Event.php
*/
class Mmnamespace_Mmmodule_Block_Adminhtml_Event extends Mage_Adminhtml_Block_Widget_Grid_Container {
public function __construct(){
$this->_blockGroup = 'mmmodule';
$this->_controller = 'adminhtml_event';
$this->_headerText = Mage::helper('mmmodule')->__('Events');
$this->_addButtonLabel = Mage::helper('mmmodule')->__('Add New Event');
parent::__construct();
}
}
my Grid.php
<?php
/**
*
* Package: magentocore
* Filename: Grid.php
* Author: solidstunna101
* Date: 27/02/14
* Time: 08:27
* Location: app/code/local/Mmnamespace/Mmmodule/Block/Adminhtml/Event/Grid.php
*/
class Mmnamespace_Mmmodule_Block_Adminhtml_Event_Grid extends Mage_Adminhtml_Block_Widget_Grid {
protected function _prepareColumns()
{
$this->addColumn('name', array(
'type' => 'text',
'index' => 'name',
'header' => $this->__('Name')
));
$this->addColumn('start', array(
'type' => 'date',
'index' => 'start',
'header' => $this->__('Start Date')
));
$this->addColumn('end', array(
'type' => 'date',
'index' => 'end',
'header' => $this->__('End Date')
));
return $this;
}
public function _prepareCollection()
{
$collection = Mage::getModel('mmmodule/event')->getCollection();
$this->setCollection($collection);
return parent::_prepareCollection();
}
}
my resource event.php
<?php
/**
*
* Package: magentocore
* Filename: Event.php
* Author: solidstunna101
* Date: 26/02/14
* Time: 08:30
* Location: app/code/local/Mmnamespace/Mmmodule/Model/Event.php
*/
class Mmnamespace_Mmmodule_Model_Event extends Mage_Core_Model_Abstract
{
//this function initializes resources to be used
public function _construct()
{
$this->_init('mmmodule/event');
}
}
my resource event abstract.php
<?php
/**
*
* Package: magentocore
* Filename: Event.php
* Author: solidstunna101
* Date: 26/02/14
* Time: 08:33
*Location: app/code/local/Mmnamespace/Mmmodule/Model/Resource/Event.php
*/
class Mmnamespace_Mmmodule_Model_Resource_Event extends Mage_Core_Model_Resource_Db_Abstract
{
//initializes primary key in the table
public function _construct()
{
$this->_init('mmmodule/event', 'event_id');
}
}
my block edit.php
<?php
/**
*
* Package: magentocore
* Filename: Edit.php
* Author: solidstunna101
* Date: 26/02/14
* Time: 14:31
* Location: app/code/local/Mmnamespace/Mmmodule/Block/Adminhtml/Event/Edit.php
*/
class Mmnamespace_Mmmodule_Block_Adminhtml_Event_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
public function __construct()
{
//creating components required to render the form
$this->_objectId = 'event_id';
$this->_blockGroup = 'mmmodule';
$this->_controller = 'adminhtml_event';
parent::__construct();
}
/**
* Get edit form container header text
*
* #return string
*/
public function getHeaderText()
{
return Mage::helper('mmmodule')->__('New Event');
}
public function getSaveUrl()
{
return $this->getUrl('*/event/save');
}
}
form.php
<?php
/**
*
* Package: magentocore
* Filename: Form.php
* Author: solidstunna101
* Date: 26/02/14
* Time: 14:34
* Location: app/code/local/Mmnamespace/Mmmodule/Block/Adminhtml/Event/Edit/Form.php
*/
class Mmnamespace_Mmmodule_Block_Adminhtml_Event_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
//this function will override the prepare form function add data forms/fieldset
public function _prepareForm()
{
$form = new Varien_Data_Form(
array('id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post')
);
$fieldset = $form->addFieldset('base_fieldset', array('legend' => Mage::helper('mmmodule')->__('General Information'), 'class' => 'fieldset-wide'));
$fieldset->addField('name', 'text', array(
'name' => 'name',
'label' => Mage::helper('mmmodule')->__('Event Name'),
'title' => Mage::helper('mmmodule')->__('Event Name'),
'required' => true
));
$dateFormatIso = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
$fieldset->addField('start', 'date', array(
'name' => 'start',
'format' => $dateFormatIso,
'image' => $this->getSkinUrl('images/grid-cal.gif'),
'label' => Mage::helper('mmmodule')->__('Start Date'),
'title' => Mage::helper('mmmodule')->__('Start Date'),
'required' => true
));
$fieldset->addField('end', 'date', array(
'name' => 'end',
'format' => $dateFormatIso,
'image' => $this->getSkinUrl('images/grid-cal.gif'),
'label' => Mage::helper('mmmodule')->__('End Date'),
'title' => Mage::helper('mmmodule')->__('End Date'),
'required' => true
));
$form->setUseContainer(true);
$this->setForm($form);
}
}
P.S my edit form displays but my grid does not display, is there anything i'm missing here
Please follow to create front end and admin modules as per the below link.
It will work clearly without making any issues in the admin grid.
Custom Module Creation Link
try to call parent::_prepareColumns in Grid.php _prepareColumns. Your grid might not be triggered to render without calling the parent.
p.s. in my admin modules I actually return parent::_prepareColumns instead of $this.
In Mmnamespace_Mmmodule_Adminhtml_EventController change return $this->renderLayout(); to $this->renderLayout();
protected function indexAction()
{
$this->loadLayout()
->_setActiveMenu('mmmodule/items')
->_addBreadcrumb(Mage::helper('adminhtml')->__('Items Manager'), Mage::helper('adminhtml')->__('Item Manager'));
$this->_addContent($this->getLayout()->createBlock('mmmodule/adminhtml_event'));
$this->renderLayout(); //<-- not return $this->renderLayout();
}
Take a look # Custom_module_with_custom_database_table because your code seem to have other minor issue