I need to create a module in Magento that will have several database tables. One of the functions of the module is to add multiple images. For example, being on the β Add a new item β or β Change an item β page in the admin, I have tabs on the left side, one of them is β Item images . When I click, I want the contents of this tab to be my own. Digging into the code, it turned out that Magento uses one of the Varien_Data_Form_Element classes to display this content.for each item in full form. I want to add my own class here, which will display the form elements the way I want. Is this good practice for this, or is there another more elegant way to add your own content to admin forms? EDIT: I have to add that none of the existing classes help my problem.
SOLUTION: I have a controller in my custom module, which is located in Mypackage / Mymodule / controller / Adminhtml / Item.php . In the editAction () method , which I use to add and create new elements, I create 2 blocks: one for the form and one for the tabs:
$ this -> _ addContent ($ this-> getLayout () -> createBlock ('item / adminhtml_edit'))
-> _ addLeft ($ this-> getLayout () -> createBlock ('item / adminhtml_edit_tabs'));
$ this-> renderLayout ();
The Block / Adminhtml / Edit / Tabs.php block creates two tabs on the left: General Information and Image Elements , each of them, using the block classes, render different contents on the right.
protected function _beforeToHtml ()
{
$ this-> addTab ('item_info', array (
'label' => Mage :: helper ('mymodule') -> __ ('Item Info'),
'content' => $ this-> getLayout () -> createBlock ('item / adminhtml_edit_tab_form') -> toHtml (),
));
$ this-> addTab ('item_images', array (
'label' => Mage::helper('mymodule')->__('Item Images'),
'active' => ( $this->getRequest()->getParam('tab') == 'item_images' ) ? true : false,
'content' => $this->getLayout()->createBlock('item/adminhtml_images')->toHtml(),
));
return parent::_beforeToHtml();
}
, item_images , varien .
class Mypackage_Mymodule_Block_Adminhtml_Images extends Mage_Core_Block_Template
{
public function __construct()
{
parent::__construct();
$this->setTemplate('item/images.phtml'); //This is in adminhtml design
}
public function getPostId()
{
return $this->getRequest()->getParam('id');
}
public function getExistingImages()
{
return Mage::getModel('mymodule/item')->getImages($this->getPostId());
}
}
app/design/adminhtml/default/default/template/item/images.phtml :
//You can add your own custom form fields here and all of them will be included in the form
foreach($this->getExistingImages() as $_img):
//Do something with each image
endforeach;
//You can add your own custom form fields here and all of them will be included in the form