2012-08-03 70 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; 
      } 
     } 
    } 
}