简体   繁体   English

在core_block_abstract_to_html_after事件中使用观察器获取自定义phtml内容

[英]Get custom phtml content using observer on core_block_abstract_to_html_after event

I have a custom block that I would like to append under a existing core block in the order view page in the admin panel of Magento. 我有一个自定义模块,我想在Magento管理面板的订单视图页面中的现有核心模块下追加。

I developed my custom module. 我开发了自定义模块。

In order to avoid modification of a core template phtml file to load my custom block, I try to follow the best practices and I built an observer on the core_block_abstract_to_html_after 为了避免修改核心模板phtml文件以加载我的自定义块,我尝试遵循最佳做法,并在core_block_abstract_to_html_after上构建了一个观察core_block_abstract_to_html_after

if you want to know why Read more two excellent articles above 如果您想知道为什么,请阅读上面的两篇优秀文章

http://www.atwix.com/magento/best-practices/ http://www.atwix.com/magento/best-practices/

http://inchoo.net/magento/how-you-could-build-your-magento-extensions-without-view-files/ http://inchoo.net/magento/how-you-could-build-your-magento-extensions-without-view-files/

However while the block, that I want to append, will contain lots of html I want to put this html in a custom phtml file and not directly in the php, to make it more easily customizable for designers. 但是,尽管我要附加的代码块将包含很多html,但我想将此html放在自定义phtml文件中,而不是直接放在php中,以使其更易于为设计人员定制。

I created so a phtm file in the following folder 我在以下文件夹中创建了一个phtm文件

app\design\adminhtml\default\default\template\custommodulefolder\customhtmlfileforadminorderview.phtml

But how to load this phtml content from the observer ? 但是如何从观察者中加载这个phtml内容呢?

<adminhtml>
        <events>
            <core_block_abstract_to_html_after>
                <observers>
                    <custommodule>
                        <class>NameSpace_CustomModule_Model_Observer</class>
                        <method>RenderBlockCustomdAdmin</method>
                    </referencefield>
                </custommodule>
            </core_block_abstract_to_html_after>
        </events>
    </adminhtml>

And the method in my Observer.php 以及我的Observer.php的方法

    public function RenderBlockCustomdAdmin($observer = NULL)
        {
            if (!$observer) {
                return;
            }

            if ('order_info' == $observer->getEvent()->getBlock()->getNameInLayout()) {

                if (!Mage::getStoreConfig('advanced/modules_disable_output/'.self::MODULE_NAME)) {

                    $transport = $observer->getEvent()->getTransport();

// here I would like to find a way to load the content of a custom of phtml
                    $htmfromablock= function_which_will_allow-me_to_get_content_phtmlfile() 
                    $transportOldHtml =$transport->getHtml();
                    $transport->setHtml($transportOldHtml.'<br />'.$htmfromablock);
                }
            }

            return $this;
        }

Creating a block is fairly easy when you get the layout (it is just a matter of calling createBlock() with the block type you want and setTemplate() with the template you want to use). 当获得布局时,创建一个块是相当容易的(只需使用所需的块类型调用createBlock()并使用要使用的模板调用setTemplate()即可)。
And you can always get the layout from another block. 而且,您始终可以从另一个块获取布局。

So in your case : 因此,在您的情况下:

$observer->getEvent()
         ->getBlock()
         ->getLayout()
         ->createBlock('adminhtml/template')
         ->setTemplate('custommodulefolder/customhtmlfileforadminorderview.phtml')
         ->toHtml();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM