2014-03-27 46 views
1

我想遍歷每個配置場讓每一個字段的值,每個範圍級別的價值標籤。這裏的代碼到目前爲止:如何獲得存儲配置領域與源模型

$ path將是像'一般/國家/默認','一般/國家/允許','一般/區域/ display_all'等配置路徑的數組。迭代每個$ path元素。

$value = Mage::getConfig()->getNode($path, 'default'); 

// ... 

foreach (Mage::app()->getWebsites() as $website) { 
    $value = Mage::getConfig()->getNode($path, 'website', $website->getCode()); 

    // ... 

    foreach ($website->getGroups() as $group) {     
     foreach ($group->getStores() as $store) { 
      $value = Mage::getConfig()->getNode($path, 'store', $store->getCode()); 

      // ... 

     } 
    } 
} 

這工作正常,除了下拉菜單和其他領域。在是/否下拉菜單中,它將返回1/0而不是「是/否」。對一個國家下拉菜單,將返回美國,而不是美國等

我敢肯定,我需要運行通過源模型中的返回值,但我不知道如何獲得源模型爲每個$編程路徑?

或者,也許有另一種方式......

回答

4

這裏是你如何能得到一個配置設置的源模型。
您可以將它集成到腳本中。

步驟1
你需要一個方法來獲得system.xml文件的內容。

$config = Mage::getConfig()->loadModulesConfiguration('system.xml')->applyExtends(); 

步驟2
您需要一種方法來 '翻譯' 的配置節點名稱(general/country/allow)從system.xmlsections/general/groups/country/fields/allow)的路徑。
的機制是這樣的

general/country/allow -------------------| 
    |  |        | 
    |  |--------------|    | 
    |      |    | 
    |-------|    |    | 
      |    |    | 
sections/general/groups/country/fields/allow 
    |    |    | 
    |    |    | 
    |-------always the same---------| 

下面是一個簡單的函數。

function getSystemPath($path) { 
    $newPath = ''; 
    $parts = explode('/', $path); 
    if (count($parts) != 3) { //you must have at least 3 parts in the node name 
     return ''; 
    } 
    return 'sections/'.$parts[0].'/groups/'.$parts[1].'/fields/'.$parts[2]; 

} 

第3步:
現在得到的source_model節點

$path = 'general/country/allow' 
$node = $config->getNode(getSystemPath($path)); //get the corresponding system.xml path from the config loaded at step 1. 
if ($node && $node->source_model){ //if there is a source model 
    //instantiate the model - use getSingleton in case there are more fields that use the same source model 
    $model = Mage::getSingleton((string)$node->source_model); 
    //get options 
    $options = $model->toOptionArray(); 
    //do something with $options. 
} 

[編輯]

如果要加載system.xml文件單個模塊,你可以做這個:

$configFile = Mage::getConfig()->getModuleDir('etc', 'Mage_Catalog').DS.'system.xml'; 
$string = file_get_contents($configFile); 
$xml = simplexml_load_string($string, 'Varien_Simplexml_Element'); 

但請記住這一點。模塊可以覆蓋或添加其他模塊的配置區域中的元素。

+0

很好解釋謝謝... +1你@Marius –

+0

有可能加載system.xml文件的特定模塊? @Marius –

+0

@KeyurShah。我已經添加了一種方法來做到這一點的答案。它的回答看起來比評論中更好。 – Marius