0

I've seen quite a lot of modules where they'll display a grid, but above that will be a block (sometimes an encouragement to upgrade, sometimes some additional information for the user).

I would like to add an Adminhtml form there.

The form is complete, and the grid is complete, but I can't seem to find any sort of hook to include the form in the grid.

I've already tried overriding the getMainButtonsHtml() method, but that seems to put the block underneath the filters. Ideally it needs to be above the filters. Is there a similar function for the HTML above the filters?

My other option would be to create an additional block that gets initialised in the tab, this one would include the form and the grid; so instead of adding the form to the grid, I'd add both the form and the grid to a container element.

Is there another (cleaner) way to do this?

  • are you talking sort in grid – Amit Bera Sep 1 '14 at 9:46
  • No, a block. As mentioned, any block of any design to be inserted above a grid. – Dan Hanly Sep 1 '14 at 13:35
3

I was able to override the block's _toHtml method and include the block:

/**
 * Override this method in descendants to produce html
 *
 * @return string
 */
protected function _toHtml()
{
    $html = $this->getLayout()->createBlock('mymoudule/adminhtml_myblock')->toHtml() . '<br />';
    $html .= parent::_toHtml();

    return $html;
}
0

If it is a custom grid you create, I think it is absolutely okay and clean to use an additional block as a container which contains the form as well as the grid.

If you extend an already existing grid, it may be a better option to use the core_block_abstract_to_html_after event and inject your form HTML there as shown in the following example code.

In your config.xml:

<adminhtml>
    <events>
        <core_block_abstract_to_html_after>
            <observers>
                <namespace_module>
                    <class>namespace_module/observer</class>
                    <method>coreBlockAbstractToHtmlAfter</method>
                </namespace_module>
            </observers>
        </core_block_abstract_to_html_after>
    </events>
</adminhtml>

In your Observer.php:

<?php

class Namespace_Module_Model_Observer
{
    public function coreBlockAbstractToHtmlAfter(Varien_Event_Observer $observer)
    {
        $block = $observer->getBlock();
        if ($block instanceof Mage_Adminhtml_Block_Sales_Order_Grid) {
            $html = $observer->getTransport()->getHtml();
            $myFormHtml = 'Get your form HTML';
            $html = $myFormHtml . $html;
            $observer->getTransport()->setHtml($html);
        }
    }
}
  • I know my reply is late, but thanks for this, before you answered I'd managed to solve it :) – Dan Hanly Feb 4 '15 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.