I have created a tab in the admin panel (Add tab to Product Information in admin panel) and now want to create a grid to show, filter, etc the data from a custom table.
How do I extend my module to show a grid driven by a custom table of data?
adminhtmlce-1.7.0.2gridgrid-serlization
I have created a tab in the admin panel (Add tab to Product Information in admin panel) and now want to create a grid to show, filter, etc the data from a custom table.
How do I extend my module to show a grid driven by a custom table of data?
Best Answer
Assuming you already have your entity and a grid for it in the backend here is what you need. In my example, my entity is called
article
and the module isEasylife_Press
. Adjust the names to fit your module.First you need a new table to remember the relation between products and your entities. For this add in your
config.xml
the table declaration inside the resource model tag. (models->press->press_resource->entities
)Now you need to create the table. In one of your upgrade scripts add this:
If the PK of your entity table is not
entity_id
change it. Sorry that I don't have the DDL install script. It's easier to understand this way. If you want I will write one.Now you need a model to handle this table.
app/code/local/Easylife/Press/Model/Article/Product.php
And a model that maps on a table requires a resource model.
app/code/local/Easylife/Press/Model/Resource/Article/Product.php
...and a collection resource model:
app/code/local/Easylife/Press/Model/Resource/Article/Product/Collection.php
Then you need to add a new tab in the product add/edit form and save the values from it. For this you need to observe the event
core_block_abstract_prepare_layout_after
. Add the following to yourconfig.xml
file inside the<adminhtml>
tag.Now the observer that adds the tab and saves the relations
app/code/local/Easylife/Press/Model/Adminhtml/Observer.php
One helper to make it easier to get the related entities
app/code/local/Easylife/Press/Helper/Product.php
:Now the actual grid block.
app/code/local/Easylife/Press/Block/Adminhtml/Catalog/Product/Edit/Tab/Article.php
And last, a controller to handle the ajax requests for your custom grid.
app/code/local/Easylife/Press/controllers/Adminhtml/Press/Article/Catalog/ProductController.php
Just kidding, that wasn't the last thing. You still need to define the layouts for the controller actions. In one of the admin layout files add this:
I hope I didn't forget any files. Clear the cache and give it a try.
Seams complicated. Well it is. But it can be easy. The code above was auto-generated by the Ultimate Module Creator extension. All you need to do with it is to configure the fields of your entity and say that you want a many to may relation with the products. I hope this is not considered self promotion, because the extension is free, you can get it directly from Magento connect, and it really solves this kind of problems.