2015-05-11 134 views
1

我有一個自定義塊,我想在Magento的管理面板的順序視圖頁面的現有核心塊下添加。在core_block_abstract_to_html_after事件中使用觀察者獲取自定義phtml內容事件

我開發了我的自定義模塊。

爲了避免核心模板PHTML文件的修改來加載我的自定義塊,我儘量遵循的最佳做法和我建立在core_block_abstract_to_html_after

一個觀察者,如果你想知道爲什麼更多兩優上述

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

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

文章然而雖然塊,我想追加,將含有大量的HTML我希望把牛逼他的HTML在一個自定義的phtml文件中,而不是直接在PHP中,使其更容易爲設計師定製。

我創造所以在下面的文件夾

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

但是如何加載從觀察者這個PHTML內容的phtm文件?

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

在我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; 
     } 
+0

爲什麼你就不能在佈局加擋? –

+0

感謝您的輸入,我沒有這樣做,因爲它意味着通過在佈局中插入塊的回顯來修改app \ design \ adminhtml \ default \ default \ template \ sales \ order \ view \ tab \ info.phtml 。因此,它不是通用的,當有人安裝mymodule時,如果這個文件已經有一些修改,並且可能發生衝突等。 – Anselme

+0

您的代碼看起來正確。你確定你的模塊輸出沒有被禁用?另外,您的觀察者是否定義了self :: MODULE_NAME? –

回答

1

的方法創建一個塊是相當容易的,當你的佈局(這只是一個叫createBlock()你想要的塊型和setTemplate()與的事你想使用的模板)。
而且您始終可以從另一個塊獲取佈局。

所以你的情況:

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

非常感謝b.enoit.be讓整個事情變得更加清晰。我有我的自定義塊我更換 - > createBlock('adminhtml /模板')與 - > createBlock('blocklayoutdeclaration/customblock')這樣我可以有所有我的方法可用這個$。再次感謝,真的幫助。 Magento架構即使可以很簡單也可能非常複雜。 – Anselme