2

I have found where the buttons (reset, delete and save) get added in app/design/adminhtml/default/default/template/catalog/category/edit/form.phtml:

Line 28:

<div class="content-header">
        <h3 class="icon-head head-categories"><?php echo $this->htmlEscape($this->getHeader()) . ($this->getCategoryId() ? ' (' . Mage::helper('catalog')->__('ID: %s', $this->getCategoryId()) . ')' : '') ?></h3>
        <p class="content-buttons form-buttons">
            <?php echo $this->getResetButtonHtml() ?>
            <?php if($this->getCategoryId()): ?>
                    <?php echo $this->getDeleteButtonHtml() ?>
            <?php endif; ?>
            <?php echo $this->getAdditionalButtonsHtml(); ?>
            <?php echo $this->getSaveButtonHtml() ?>
        </p>
</div>

And in app/code/core/Mage/Adminhtml/Block/Catalog/Category/Edit/Form.php are the methods and logic to add them.

I would like to add my own but I really don't want to be copying form.phtml into my local directory nor would I really want to overwrite Form.php. Is there a better way? Possibly with my layout xml for my module?

Thanks in advance.

beingalex
  • 727
  • 2
  • 11
  • 25

1 Answers1

8

Here is how I usually do it.

  • I create a new module with a block inside it.
  • That block contains a method that adds a button and it should be placed as a child for category.edit block.

You will need the following files:

app/etc/modules/[Namespace]_[Module].xml - the declaration file

<?xml version="1.0"?>
<config>
    <modules>
        <[Namespace]_[Module]>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Adminhtml />
             </depends>
        </[Namespace]_[Module]>
    </modules>
</config>

app/code/local/[Namespace]/[Module]/etc/config.xml - the configuration file

<?xml version="1.0"?>
<config>
    <modules>
        <[Namespace]_[Module]>
            <version>0.0.1</version>
        </[Namespace]_[Module]>
    </modules>
    <global>
        <blocks>
            <[module]>
                <class>[Namespace]_[Module]_Block</class>
            </[module]>
        </blocks>
        <helpers>
            <[module]>
                <class>[Namespace]_[Module]_Helper</class>
            </[module]>
        </helpers>
    </global>
    <adminhtml>
        <layout>
            <updates>
                <[module]>
                    <file>[namespace]_[module].xml</file>
                </[module]>
            </updates>
        </layout>
    </adminhtml>
</config>

app/design/adminhtml/default/default/layout/[namespace]_[module].xml - the admin layout file

<?xml version="1.0"?>
<layout>
    <adminhtml_catalog_category_edit>
        <reference name="category.edit">
            <block name="[module].buttons" type="[module]/adminhtml_catalog_category_edit_buttons">
                <action method="addButtons"></action>
            </block>
        </reference>
    </adminhtml_catalog_category_edit>
</layout>

app/code/local/[Namespace]/[Module]/Block/Adminhtml/Catalog/Category/Edit/Buttons.php - the block with the buttons.

<?php
class [Namespace]_[Module]_Block_Adminhtml_Catalog_Category_Edit_Buttons 
    extends Mage_Adminhtml_Block_Catalog_Category_Abstract {
    public function addButtons()
    {
        $this->getParentBlock()->getChild('form')
                    ->addAdditionalButton('some_button_code_here', array(
                        'label' => $this->helper('[module]')->__('Button label here'),
                        'class' => 'add',
                        'onclick' => 'alert(\'It works\')' //change the action of the button to what you need
        ));
        return $this;
    }
}

app/code/local/[Namespace]/[Module]/Helper/Data.php - the module helper

<?php 
class [Namespace]_[Module]_Helper_Data extends Mage_Core_Helper_Abstract{
}
kb.
  • 191
  • 7
Marius
  • 197,939
  • 53
  • 422
  • 830