3
votes

I need to create module in Magento which will have few database tables. One of the function of the module is adding multiple images. For example while being on the "Add new item" or "Edit item" page in the admin, from the left side I have tabs, one of them is "Item Images". When being clicked I want the content of this tab to be my own custom one. After digging into the code, found out that the way it renders this content, Magento is using one of the Varien_Data_Form_Element classes for each element in the full form. I want to add my own class here that will render form elements the way I want. Is this a good practice to do so, or there is some other more elegant way of adding own content in the admin forms? EDIT: I must add that none of the existing classes is helping my problem.

SOLUTION EDIT: I have a controller in my custom module that is in Mypackage/Mymodule/controllers/Adminhtml/Item.php. In the editAction() method which I am using for adding and creating new items, I am creating 2 blocks, one for the form and one left 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 is creating 2 tabs on the left: General Info and Item Images, each of them are rendering different content on the right side using Block classes.

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();
}

I wanted the tab item_images to render my own form elements and values, not the default varien form elements.

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());
    }
}

Then in the template app/design/adminhtml/default/default/template/item/images.phtml you can use these values:

//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
1

1 Answers

5
votes

No, it's not. You should never edit or add to files that provided by a vendor. If you absolutely must replace a class file you should use the local code pool. For example, if you wanted to change the behavior of a text field,

lib/Varien/Data/Form/Element/Text.php

You should place a file in the local or community code pool

app/code/local/Varient/Data/Form/Element/Text.php

However, doing the replaces the class, and it becomes your responsibility to maintain compatibility with future versions. That means if Magento Inc. changes lib/Varien/Data/Form/Element/Text.php, you need to update your version to be compatible.

Based on what you said I'd look into creating a class rewrite for the Block class that renders the form.