I want to create the Grid Serializer
functionality same as coming in Manage categories
If Anyone create that type functionality then please guide me.
grid-serlizationmagento-1.7magento-1.8
I want to create the Grid Serializer
functionality same as coming in Manage categories
If Anyone create that type functionality then please guide me.
Following I give some example which I have done before. Mine contains one edit form and one ajax grid, but with this logic, you can add any number of tabs and grids in this form. File structuire:
1./app/code/local/Ssd/Experts/Block/Adminhtml/Experts.php
/**
* Experts management Grid container, row click opents expert edit page
*/
class Ssd_Experts_Block_Adminhtml_Experts extends Mage_Adminhtml_Block_Widget_Grid_Container
{
public function _construct()
{
$this->_controller = 'adminhtml_experts';
$this->_blockGroup = 'experts';
$this->_headerText = Mage::helper('experts')->__('Manage experts');
$this->_addButtonLabel = Mage::helper('brands')->__('Add Expert');
parent::_construct();
}
}
2./app/code/local/Ssd/Experts/Block/Adminhtml/Experts/Grid.php
class Ssd_Experts_Block_Adminhtml_Experts_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
public function __construct()
{
parent::__construct();
$this->setId('expertsGrid');
$this->setDefaultSort('name');
$this->setDefaultDir('ASC');
$this->setSaveParametersInSession(true);
}
protected function _prepareCollection()
{
$collection = Mage::getModel('experts/experts')->getCollection()->addCategoryData();
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn('expert_name', array(
'header' => Mage::helper('experts')->__('Name'),
'align' => 'left',
'index' => 'expert_name',
'width' => '250',
));
/**
* another columns here
*/
return parent::_prepareColumns();
}
}
3./app/code/local/Ssd/Experts/Block/Adminhtml/Experts/Edit.php
/**
* Expert edit form container
*/
class Ssd_Experts_Block_Adminhtml_Experts_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
public function __construct()
{
parent::__construct();
$this->_objectId = 'id';
$this->_blockGroup = "experts";
$this->_controller = "adminhtml_experts";
$this->updateButton('delete', 'label', Mage::helper('experts')->__('Delete Expert'));
$this->updateButton('save', 'label', Mage::helper('experts')->__('Save Expert'));
}
}
4./app/code/local/Ssd/Experts/Block/Adminhtml/Experts/Edit/Tabs.php
class Ssd_Experts_Block_Adminhtml_Experts_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs
{
public function __construct()
{
parent::__construct();
$this->setId('experts_tab');
$this->setDestElementId('edit_form');
}
protected function _beforeToHtml()
{
//edit form tab
$this->addTab('expert_form_edit', array(
'label' => Mage::helper('brands')->__('Edit Expert'),
'title' => Mage::helper('brands')->__('Edit Expert'),
'content' => $this->getLayout()->createBlock('experts/adminhtml_experts_edit_tab_form')->toHtml(),
'active' => true
));
if (Mage::registry('experts_data') && Mage::registry('experts_data')->getId()) {
//grid tab
$this->addTab('expert_advices', array(
'label' => Mage::helper('experts')->__('Expert Advices'),
'title' => Mage::helper('experts')->__('Expert Advices'),
'content' => $this->getLayout()->createBlock('experts/adminhtml_experts_edit_tab_advices_grid')->toHtml(),
));
}
//also you can add another grid tab
/*$this->addTab('expert_advices', array(
'label' => Mage::helper('experts')->__('Another Grid'),
'title' => Mage::helper('experts')->__('Another Grid'),
'content' => $this->getLayout()->createBlock('experts/adminhtml_experts_edit_tab_another_grid')->toHtml(),
));*/
return parent::_beforeToHtml();
}
}
5./app/code/local/Ssd/Experts/Block/Adminhtml/Experts/Edit/Form.php
class Ssd_Experts_Block_Adminhtml_Experts_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
$form = new Varien_Data_Form(array(
'id' => 'edit_form',
'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
'method' => 'post',
'enctype' => 'multipart/form-data',
));
$form->setUseContainer(true);
$this->setForm($form);
return parent::_prepareForm();
}
}
6./app/code/local/Ssd/Experts/Block/Adminhtml/Experts/Edit/Tab/Form.php
class Ssd_Experts_Block_Adminhtml_Experts_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
$form = new Varien_Data_Form();
$fieldset = $form->addFieldset('personal_information', array(
'legend' => Mage::helper('experts')->__('Personal Information'),
'class' => 'fieldset-wide',
));
$fieldset->addField('expert_name', 'text', array(
'label' => Mage::helper('experts')->__('Expert Name'),
'required' => true,
'name' => 'expert_name',
));
/**
* another elements here
* this is typical edit form
*/
return parent::_prepareForm();
}
}
7./app/code/local/Ssd/Experts/Block/Adminhtml/Experts/Edit/Tab/Advices/Grid.php
/**
* this is typical grid, but it also impelents tab interface
*/
class Ssd_Experts_Block_Adminhtml_Experts_Edit_Tab_Advices_Grid extends Mage_Adminhtml_Block_Widget_Grid
implements Mage_Adminhtml_Block_Widget_Tab_Interface
{
public function _construct()
{
parent::_construct();
$this->setId('expert_advice_grid');
$this->setDefaultSort('page_title');
$this->setDefaultDir('ASC');
//enable ajax grid
$this->setUseAjax(true);
$this->setSaveParametersInSession(true);
}
protected function _prepareCollection()
{
$collection = Mage::getModel('experts/advices')
->getCollection()
->addExpertData()
->addFieldToFilter('main_table.expert_id', Mage::app()->getRequest()->getParam('id'));
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn('advice_title', array(
'header' => Mage::helper('experts')->__('Title'),
'align' => 'left',
'index' => 'advice_title',
));
/**
* add another columns here
*/
return parent::_prepareColumns();
}
//this method is reuired if you want ajax grid
public function getGridUrl()
{
return $this->getUrl('*/*/grid', array('_current' => true));
}
public function canShowTab()
{
return true;
}
public function isHidden()
{
return false;
}
public function getTabLabel()
{
return $this->__('Settings');
}
public function getTabTitle()
{
return $this->__('Expert Advices');
}
}
8./app/code/local/Ssd/Experts/Block/Adminhtml/Experts/Edit/Tab/Another/Grid.php
class Ssd_Experts_Block_Adminhtml_Experts_Edit_Tab_Another_Grid extends Mage_Adminhtml_Block_Widget_Grid
implements Mage_Adminhtml_Block_Widget_Tab_Interface
{
public function _construct()
{
parent::_construct();
$this->setId('expert_another_grid');
//enable ajax grid
$this->setUseAjax(true);
}
protected function _prepareCollection()
{
/**
* prepare collection what you want
*/
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
/**
* add another columns here
*/
return parent::_prepareColumns();
}
//this method is reuired if you want ajax grid
public function getGridUrl()
{
return $this->getUrl('*/*/anothergrid', array('_current' => true));
}
/**
* implement tab interface methods
*/
}
9./app/code/local/Ssd/Experts/controllers/Adminhtml/ExpertsController.php
class Ssd_Experts_Adminhtml_ExpertsController extends Mage_Adminhtml_Controller_Action
{
//this is ajax grid request action
public function gridAction()
{
echo $this->getLayout()->createBlock('experts/adminhtml_experts_edit_tab_advices_grid')->toHtml();
}
public function editAction()
{
$expertModel = Mage::getModel('experts/experts')->load($this->getRequest()->getParam('id'));
if ($expertModel->getId()) {
Mage::register('experts_data', $expertModel);
$this->loadLayout();
//attention here, what blocks are creating, left is tab, content is edit container
$this->_addLeft($this->getLayout()->createBlock('experts/adminhtml_experts_edit_tabs'));
$this->_addContent($this->getLayout()->createBlock('experts/adminhtml_experts_edit'));
$this->renderLayout();
} else {
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('experts')->__('Expert does not exist'));
$this->_redirect('*/*/');
}
}
/**
* another actions here: index, delete, mass....
*/
}
I hope you can understand file structure using class names. some codes are cutted out. My experts edit form contains edit form container, tabs, end each tab content is can be form controls, grid or any content what you want. Additional comments are in the code. Hope this example helps you.
Try this for get dynamic dropdown of customer attibutes like customer_type .you also change attributes name or put static values.
$this->addColumn('column_name', array(
'header' => Mage::helper('customer')->__('Attribute label'),
'align' => 'left',
'width' => '100px',
'index' => 'atribute_code',
'type' => 'options',
'options' => $this->_getAttributeOptions('atribute_code'),
));
protected function _getAttributeOptions($attribute_code)
{
$attribute = Mage::getModel('eav/config')->getAttribute('customer', $attribute_code);
$options = array();
foreach( $attribute->getSource()->getAllOptions(false) as $option ) {
$options[$option['value']] = $option['label'];
}
return $options;
}
Best Answer
Assuming you have the module without the product relation here is what you need in addition.
First create a relation table between your entity and the products. Add this in
config.xml
inside theglobal/models/[module]_resource/entities
Add this in one of the
upgrade scripts.
Now create the grid block.
[Namespace]/[Module]/Block/Adminhtml/[Entity]/Edit/Tab/Product.php
Now add this tab in the list of tabs. In
[Namespace]_[Module]_Block_Adminhtml_[Entity]_Edit_Tabs::_beforeToHtml
add this below the main tab.Now you need the controller actions to handle the products.
Add these methods to the admin controller for you entity:
Now the layout for these 2 actions. In the admin layout file for your module add these 2 handles.
Now saving the data. In the
saveAction
of your admin controller add this right before calling$[entity]->save()
In your entity model add these methods and a member variable that will process the product relation:
Now you need the entity-product relation model.
Create
[Namespace]/[Module]/Model/[Entity]/Product.php
You also need a resource model.
[Namespace]/[Module]/Model/Resource/[Entity]/Product.php
and a collection resource model. I promise that this is the last one.
[Namespace]/[Module]/Model/Resource/[Entity]/Product/Collection.php
All you need to do is to replace the values between
[]
([Namespace]
,[Module]
,[module]
, ...) with your real values.You may encounter some errors, because the way you structured your module may be a little different of what I have in mind. But with some debugging and changes you can get it to work. All the heavy lifting is there.
That's it.
Note: The code above was copy/pasted (and renamed the file names) from what was generated with UMC. You can use that to create your full module without having to worry about linking your entity to products. You just say in the UI "Link entity to products:Yes".
This is not spam. The extension is free.