What is the proper way to add a custom dashboard “box” in the Magento backend without editing default templates?

后端 未结 1 1170
隐瞒了意图╮
隐瞒了意图╮ 2021-01-22 21:43

I am working on creating what I hope one day will be a publicly available Magento extension (this part I mention because it\'s important to me that I do the \"right thing\" here

1条回答
  •  感情败类
    2021-01-22 22:13

    There is no really clean option, as you said, the template is coded in a non-extendable way, so there will always be some degree of hackiness. This is my personal preferred way of doing it by using event observers. This way it at least doesn't conflict with other modules.

    First, add an observer for the core_block_abstract_prepare_layout_after and core_block_abstract_to_html_after event.

    
        
            
                
                    
                        your_module/observer
                        coreBlockAbstractPrepareLayoutAfter
                    
                
            
            
                
                    
                        your_module/observer
                        coreBlockAbstractToHtmlAfter
                    
                
            
        
    
    

    These two events are dispatched for every block that is instantiated and rendered in Mage_Core_Block_Abstract. In my experience it's not such an issue using them in the adminhtml interface, but on the frontend observers for these events add too much overhead.

    Back to the task at hand, you need to create the observer class.

    class Your_Module_Model_Observer
    {
        public function coreBlockAbstractPrepareLayoutAfter(Varien_Event_Observer $observer)
        {
            if (Mage::app()->getFrontController()->getAction()->getFullActionName() === 'adminhtml_dashboard_index')
            {
                $block = $observer->getBlock();
                if ($block->getNameInLayout() === 'dashboard')
                {
                    $block->getChild('topSearches')->setUseAsDashboardHook(true);
                }
            }
        }
    
        public function coreBlockAbstractToHtmlAfter(Varien_Event_Observer $observer)
        {
            if (Mage::app()->getFrontController()->getAction()->getFullActionName() === 'adminhtml_dashboard_index')
            {
                if ($observer->getBlock()->getUseAsDashboardHook())
                {
                    $html = $observer->getTransport()->getHtml();
                    $myBlock = $observer->getBlock()->getLayout()
                        ->createBlock('you_module/block')
                        ->setTheValuesAndTemplateYouNeed('HA!');
                    $html .= $myBlock->toHtml();
                    $observer->getTransport()->setHtml($html);
                }
            }
        }
    }
    

    Your template will need to accomodate for the fact that you are inserting a sibling

    from inside the sibling, but otherwise you should be fine.

    Your Module

    Your Content

    Leave it at that, because the parent template will be closing the

    and the
    for you (ugly as heck, I know).

    0 讨论(0)
提交回复
热议问题