2

我正在努力创建我希望有一天会成为公开可用的 Magento 扩展的东西(我提到这部分是因为我在这里做“正确的事情”对我很重要)。我想做的一件事是在默认的 Magento 仪表板中添加一个框,基本上是一个新的“框”,与“Top 5 Search Terms”完全一样,除了我自己的内容。我希望我的新自定义框成为显示的最后一个框(理想情况下)。

我遇到的问题是负责渲染仪表板的模板调用了要渲染的特定块,这些块嵌套在 html 中。换句话说,通常会有一个区域将子块渲染成一个很好的合理的 HTML 元素,在这种情况下,似乎会渲染特定的块。这是/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 来相对轻松地实现这一点,添加我需要渲染块的内容,例如:

<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>

但是,这不是我自己的商店,所以这似乎不是一个选择。

现在,经过一些思考,我的选择似乎如下(请注意,其中大多数看起来“糟糕”,而不是我在公众中看到的超级自豪的东西):

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

1)我可能(也许?)能够将我的自定义块添加到该区域中的这些其他块之一(“topSearches”、“sales”等)并渲染我的块。这似乎不是很“干净”

2)我也许可以在仪表板页面上的其他地方呈现块,然后使用 javascript 将其移动到正确的位置。我猜这会相当容易,但由于明显的原因感觉非常“hacky”。

有没有人对这样做的方式有任何反馈,或者如果有办法?请记住,我想再次公开发布这个模块,所以我的目标是做好工作并尽可能少地“黑客”。

非常感谢您的阅读!

4

1 回答 1

9

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.

<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>

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 <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

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

于 2012-02-10T09:01:06.447 回答