添加自定义仪表板“框"的正确方法是什么?在Magento后端中无需编辑默认模板? [英] What is the proper way to add a custom dashboard "box" in the Magento backend without editing default templates?

查看:65
本文介绍了添加自定义仪表板“框"的正确方法是什么?在Magento后端中无需编辑默认模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力创建我希望有一天成为公开可用的Magento扩展程序(我提到这一部分是因为在这里对我而言,做正确的事情"对我来说很重要).我想做的一件事情是在默认的Magento仪表板中添加一个框,除了我自己的内容外,基本上是一个与前5个搜索字词"完全相同的新框".我希望新的自定义框成为(理想情况下)显示的最后一个框.

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). One of the things I would like to do is add a box in the default Magento dashboard, basically a new "box" exactly like "Top 5 Search Terms" except with my own content. I would like my new custom box to be the last box that is displayed (ideally).

我遇到的问题是负责渲染仪表板的模板调用了要渲染的特定块,并且这些块嵌套在html内.换句话说,通常会有一个区域将子块呈现为一个不错的合理HTML元素,在这种情况下,似乎会呈现特定的块.这是/app/design/adminhtml/default/default/template/dashboard/index.phtml

The issue that I'm running into is that the template that is responsible for rendering the dashboard calls out specific blocks to be rendered, and these blocks are nested inside of html. In other words, where often there is an area where child blocks will be rendered into a nice reasonable HTML element, in this situation it appears that specific blocks are rendered. Here is the contents of /app/design/adminhtml/default/default/template/dashboard/index.phtml

  <div class="dashboard-container">
            <?php echo $this->getChildHtml('store_switcher') ?>
            <table cellspacing="25" width="100%">
                <tr>
                    <td><?php echo $this->getChildHtml('sales') ?>
                        <div class="entry-edit">
                            <div class="entry-edit-head"><h4><?php echo $this->__('Last 5 Orders') ?></h4></div>
                            <fieldset class="np"><?php echo $this->getChildHtml('lastOrders'); ?></fieldset>
                        </div>
                        <div class="entry-edit">
                            <div class="entry-edit-head"><h4><?php echo $this->__('Last 5 Search Terms') ?></h4></div>
                            <fieldset class="np"><?php echo $this->getChildHtml('lastSearches'); ?></fieldset>
                        </div>
                        <div class="entry-edit">
                            <div class="entry-edit-head"><h4><?php echo $this->__('Top 5 Search Terms') ?></h4></div>
                            <fieldset class="np"><?php echo $this->getChildHtml('topSearches'); ?></fieldset>
                        </div>
                    </td>
                    <td>
                        <div class="entry-edit" style="border:1px solid #ccc;">
                            <?php echo $this->getChildHtml('diagrams') ?>
                            <?php if (is_array($this->getChild('diagrams')->getTabsIds())) : ?>
                                <div id="diagram_tab_content"></div>
                            <?php endif; ?>
                            <div style="margin:20px;">
                                <?php echo $this->getChildHtml('totals') ?>
                            </div>
                            <div style="margin:20px;">
                                <?php echo $this->getChildHtml('grids') ?>
                                <div id="grid_tab_content"></div>
                            </div>
                        </div>
                    </td>
                </tr>
            </table>
        </div>

如果我在自己的商店中进行此操作,我相信可以通过编辑上面的基本Magento仪表板模板index.phtml相对容易地实现这一点,添加需要渲染块的内容,例如:

If I was doing this in my own store, I believe I could achieve this relatively easily by editing the base Magento dashboard template index.phtml above, add what I need to have my block render, something like:

<div class="entry-edit">
     <div class="entry-edit-head">
         <h4><?php echo $this->__('Top 5 Search Terms') ?></h4></div>
         <fieldset class="np"><?php echo $this->getChildHtml('myDashboardBox'); ?></fieldset>
     </div>
</div>

但是,这不是我自己的商店,因此这似乎并不是一种选择.

But, this isn't my own store, so this doesn't really seem like an option.

现在,经过一番思考,我的选择似乎如下(请注意,这些选择中的大多数似乎都是不好的",而不是让我在公众场合见到的超级自豪的东西):

Now, after some thought my options seem to be as follows (note that most of these seem "bad" and not something I'd be super proud to have seen in the public):

0)显而易见,我没有看到您会告诉我的是完美/正确的解决方案

0) Something obvious that I'm not seeing that you will tell me is the perfect/right solution

1)我也许可以(也许?)将自定义块添加到该区域的其他块之一("topSearches","sales"等)中,并渲染我的块.这似乎不太干净"

1) I might (maybe?) be able to add my custom block inside of one of these other blocks in this area ("topSearches", "sales", etc) and have my block rendered. This does not seem very "clean"

2)我也许能够在仪表板页面上的其他位置呈现该块,然后使用javascript将其移动到正确的位置.我猜这很容易,但是由于明显的原因,感觉很"hacky".

2) I might be able to have the block rendered somewhere else on the dashboard page, and then move it with javascript to the correct place. This would be fairly easy I'm guessing, but feels very "hacky" for obvious reasons.

有人对执行此操作的方式有任何反馈吗?或者是否有解决方法?请记住,我想再次公开发布此模块,因此我的目标是做好工作,并尽可能减少黑客"行为.

Does anybody have any feedback on the way to do this, or IF there is a way? Keep in mind that again I would like to release this module publicly so my goal is to do a good job and do as little "hacking" as possible.

非常感谢您的阅读!

推荐答案

没有真正干净的选择,正如您所说的那样,模板是以不可扩展的方式编码的,因此总会有一定程度的黑客攻击.这是我个人使用事件观察器的首选方式.这样至少不会与其他模块冲突.

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.

首先,为core_block_abstract_prepare_layout_aftercore_block_abstract_to_html_after事件添加观察者.

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

<adminhtml>
    <events>
        <core_block_abstract_prepare_layout_after>
            <observers>
                <your_module>
                    <class>your_module/observer</class>
                    <method>coreBlockAbstractPrepareLayoutAfter</method>
                </your_module>
            </observers>
        </core_block_abstract_prepare_layout_after>
        <core_block_abstract_to_html_after>
            <observers>
                <your_module>
                    <class>your_module/observer</class>
                    <method>coreBlockAbstractToHtmlAfter</method>
                </your_module>
            </observers>
        </core_block_abstract_to_html_after>
    </events>
</adminhtml>

对于在Mage_Core_Block_Abstract中实例化并呈现的每个块,将分派这两个事件.以我的经验,在adminhtml界面中使用它们并不是一个问题,但是在前端观察器中,这些事件会增加过多的开销.

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);
            }
        }
    }
}

您的模板将需要适应以下事实,即您正在从兄弟姐妹的内部插入兄弟姐妹<div>,否则应该没问题.

Your template will need to accomodate for the fact that you are inserting a sibling <div> from inside the sibling, but otherwise you should be fine.

</fieldset></div>
<div class="entry-edit">
    <div class="entry-edit-head"><h4>Your Module</h4></div>
    <fieldset class="np">Your Content

将其保留,因为父模板将为您关闭<fieldset><div>(我知道这很丑).

Leave it at that, because the parent template will be closing the <fieldset> and the <div> for you (ugly as heck, I know).

这篇关于添加自定义仪表板“框"的正确方法是什么?在Magento后端中无需编辑默认模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆