Magento - Override adminhtml template file

烂漫一生 提交于 2019-11-28 19:42:20

Adminhtml uses the same theming fallback as the frontend, therefore you need only declare a custom template theme for your installation in module config XML:

<stores>
    <admin>
        <design>
            <theme>
                <template>custom</template>
            </theme>
        </design>
    </admin>
</stores>

Then you can create app/design/adminhtml/default/custom/template/widget/grid.phtml with any customizations you like, and this file will be used in preference to the one from the default/default adminhtml theme. Your solution then would be to add an ACL check in the logic which renders the export control:

<?php if($this->getExportTypes() && {ACL LOGIC}}): ?>
    <td class="export a-right">
        <img src="<?php echo $this->getSkinUrl('images/icon_export.gif') ?>" alt="" class="v-middle"/>&nbsp; <?php echo $this->__('Export to:') ?>
        <select name="<?php echo $this->getId() ?>_export" id="<?php echo $this->getId() ?>_export" style="width:8em;">
        <?php foreach ($this->getExportTypes() as $_type): ?>
            <option value="<?php echo $_type->getUrl() ?>"><?php echo $_type->getLabel() ?></option>
        <?php endforeach; ?>
        </select>
        <?php echo $this->getExportButtonHtml() ?>
    </td>
<?php endif; ?>

While this logic might be more appropriately implemented in the block class, the class rewrite system does not accommodate rewriting of parent classes, leaving you to rewrite every subclass. In this instance, obeying DRY outweighs embedding too much logic in templates. Moreover, the change is obvious and easily maintained.

Ideally the core team would have implemented this check in the Mage_Adminhtml_Block_Widget_Grid class or at least provided a public setter for the _exportTypes property, which would have made this logic a bit cleaner to implement.

It might seem the simplest solution to rewrite the block but that's more of a dirty hack than a clean solution. Class rewrites should be used very carefully and always avoided if possible. Otherwise you will quickly run into conflicts and also updating Magento gets a hell.

Usually you can change templates by a custom layout update (i.e. in your local.xml), but in this case it is a widget, which are not configured via layout XML.

So, enter observers: create a module that contains the following in its config.xml

<adminhtml>
    <events>
        <adminhtml_block_html_before>
            <observers>
                <yourmodulename_observer>
                    <class>yourmodulename/observer</class>
                    <method>changeWidgetTemplate</method>
                </yourmodulename_observer>
            </observers>
        </adminhtml_block_html_before>
    </events>
</adminhtml>

If you don't understand any of the above, read about Magento Events and Observers.

Now you will need the observer itself to actually change the template, but only for this block type:

class Your_Modulename_Observer
{
    public function changeWidgetTemplate(Varien_Event_Observer $observer)
    {
        $block = $observer->getEvent()->getBlock();
        if ($block instanceof Mage_Adminhtml_Block_Widget_Grid) {
            // consider getting the template name from configuration
            $template = '...';
            $block->setTemplate($template);
        }
    }
}

Magento - Override adminhtml template file add below code to config.xml file of extension (you created)

   <stores>
    <admin>
        <design>
            <theme>
                <default>default</default>
                <template>rwd</template>
            </theme>
        </design>
    </admin>
</stores>

Now create rwd folder under adminhtml/default/rwd package. and create template and layout file as you want to override.

like we want to override order comment history.phtml file.

<root>\app\design\adminhtml\default\default\template\sales\order\view\history.phtml
<root>\app\design\adminhtml\default\rwd\template\sales\order\view\history.phtml

Template definition can be found here

class Mage_Adminhtml_Block_Widget_Grid extends Mage_Adminhtml_Block_Widget

in

public function __construct($attributes=array())

So you need to rewrite sales grid block if you want to remove export csv from Sales Order Grid (use this guide if you don't know how http://www.magentocommerce.com/wiki/groups/174/changing_and_customizing_magento_code) and to change __construct to be like

public function __construct($attributes=array())
{
    parent::__construct($attributes);
    $this->setTemplate('...'); //here is your template
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!