Magento - Override adminhtml template file
Asked Answered
J

4

12

I have read several posts on stack overflow

and a couple threads on the magento forum

However, None of these posts attempt to do what I am trying to do

I would like to override the

app/design/adminhtml/default/default/template/widget/grid.phtml 

file, as this file contains a portion of html that allows anyone to export from the sales->order view.

Note: We have disabled all of the export options for this user role in the permissions->role view

The code that displays the "Export to: " -> "CSV/Excel XML" feature is included in the path I have listed above. I would like to remove that chunk of html and override the file included with Magento.

Justinn answered 21/1, 2013 at 23:21 Comment(3)
I would try to add a local.xml - or better a new layout.xml to your extension and change the template, and remove the export things. Afterwards you need to implement an observer which listens on controller_action_predispatch_adminhtml_sales_order_export_excel and controller_action_predispatch_adminhtml_sales_order_export_csv which prevents these actions if the admin_user_group is the wrongJost
@FabianBlechschmidt Instead of edit core file what is the best alternate like do changes app/design/frontend/default/theme instead of app/design/frontend/default/defaultDupondius
@Dupondius that what benmarks describes in the accepted solution. Define an admin theme and then use it like a frontend theme - copy file and change what is needed.Jost
V
25

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.

Versicolor answered 22/1, 2013 at 13:47 Comment(3)
Is it possibile to declare multiple themes as a fallback tree?Henrieta
If you move the core adminhtml theme from "default/default" to "base/default" then you can specify three custom themes.Versicolor
@Versicolor Instead of edit core file what is the best alternate like do changes app/design/frontend/default/theme instead of app/design/frontend/default/defaultDupondius
P
5

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);
        }
    }
}
Polar answered 22/1, 2013 at 0:4 Comment(3)
doing this with a local.xml in the admin layout path might be better.Jost
@FabianBlechschmidt if you know how to configure widgets in local.xml, please let me know. This would indeed be a better solution.Polar
there is no need of a widget. this might work: <adminhtml_sales_order_index><reference name="sales_order.grid"><action method="setTemplate"><template>newTemplate.phtml</template></... But I think this won't work, because the block is not created, when the layout is loaded. therefore you are right. The template needs to be changed in an observer.Jost
G
2

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
Godson answered 3/11, 2017 at 13:43 Comment(0)
D
1

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
}
Disgusting answered 21/1, 2013 at 23:29 Comment(5)
This will not work. Block rewriting does not apply to parent classes.Versicolor
Yes, the solution suggested by fab is better. But this example will work because you directly rewrite Mage_Adminhtml_Block_Widget_Grid, call its __construct (in which template will be set the first time) and then set your template.Disgusting
You seem to misunderstand how class rewrites work in Magento. createBlock('group/name') uses the config object, but that will autoload only the rewritten class definition, which then specifies the parent class by name. The autoloader - which is solely responsible when it comes to loading the parent class - does not in any way evaluate class rewrites from configuration XML. Do a simple test with a subclass of Mage_Adminhtml_Block_Widget_Grid and you will see what I mean.Versicolor
yes, I know how it works and it was my mistake. sales order grid needed to be rewriten in this question directly. I changed my answer.Disgusting
@Disgusting Instead of edit core file what is the best alternate like do changes app/design/frontend/default/theme instead of app/design/frontend/default/defaultDupondius

© 2022 - 2024 — McMap. All rights reserved.