2012-08-03 69 views
1

我是Magento的新手。我試圖建立一個模塊,它動態地插入代碼xml到其佈局xml之前 - 類似於如何構建CMS>頁面。動態插入Magento的佈局xml

就像我們如何在頁面的設計部分(admin> cms> page)中指定layout xml一樣,我想通過我的模塊插入到layout.xml中。

基本上,我想

  • 已經從那裏我可以通過在數據庫中的 形式和存儲進入佈局代碼管理部分 - 我有這部分
  • 想出了Magento的將這些要素的代碼存儲在數據庫中,並在彙總和解釋佈局文件之前創建一個xml文件。 - 我無法構建這部分。

任何幫助,將不勝感激。

回答

5

只是一點點啓示 您可以通過使用觀察者添加這些佈局的xml, 比方說,你想添加的佈局XML生成

我們可以使用事件controller_action_layout_generate_xml_before

這裏的XML之前的示例代碼(在​​3210)

<frontend> 
    <events> 
     <controller_action_layout_generate_xml_before> 
      <observers> 
       <add_new_layout> 
        <class>test/observer</class> 
        <method>addNewLayout</method> 
       </add_new_layout> 
      </observers> 
     </controller_action_layout_generate_xml_before> 
    </events> 
</frontend> 

這裏的Observer.php

public function addNewLayout($observer){ 
    $layout = $observer->getEvent()->getLayout(); 
    $update = $layout->getUpdate(); 

    //$action = $observer->getEvent()->getAction(); 
    //$fullActionName = $action->getFullActionName(); 
    //in case you're going to add some conditional (apply these new layout xml on these action or other things, you can modify it by yourself) 

    //here is the pieces of layout xml you're going to load (you get it from database) 
    $xml = "<reference name='root'><remove name='footer'></remove></reference>"; 
    $update->addUpdate($xml); 

    return; 
} 
0

另一個posibility是使用core_layout_update_updates_get_after -event和佔位符(不存在的)佈局的xml:

<frontend> 
    <layout> 
     <updates> 
      <foobar> 
       <file>foo/bar.xml</file> 
      </foobar> 
     </updates> 
    </layout> 
    <events> 
     <core_layout_update_updates_get_after> 
      <observers> 
       <foobar> 
        <type>singleton</type> 
        <class>foobar/observer</class> 
        <method>coreLayoutUpdateUpdatesGetAfter</method> 
       </foobar> 
      </observers> 
     </core_layout_update_updates_get_after> 
    </events> 
</frontend> 

例PHP在觀察者:

/** 
* Event dispatched when loading the layout 
* @param Varien_Event_Observer $observer 
*/ 
public function coreLayoutUpdateUpdatesGetAfter($observer) 
{ 
    /** @var Mage_Core_Model_Config_Element $updates */ 
    if (Mage::getStoreConfig('foobar/general/enabled')) { 
     $file = Mage::getStoreConfig('foobar/general/layout_xml'); 
     if (!empty($file)) { 
      $updates = $observer->getUpdates(); 
      if ($updates->foobar) { 
       $updates->foobar->file = $file; 
      } 
     } 
    } 
}