2013-12-09 55 views
0

我創建了新字段並將其包含到system.xml(Pin_Init_Block_Adminhtml_System_Robots) 顯示由_getElementHtml或render控制。 我可以保存嗎?Magento。重寫保存爲管理員自定義字段

<?php 
    class Pin_Init_Block_Adminhtml_System_Robots extends Mage_Adminhtml_Block_System_Config_Form_Field 
    { 
     protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element) 
     { 
      //self::__writeRobots($element->getEscapedValue()); 

      $html = '<textarea id="'.$element->getHtmlId().'" name="'.$element->getName() 
       .'" '.$element->serialize($element->getHtmlAttributes()).'>'.self::__readRobots().'</textarea>'."\n"; 
      $html.= $element->getAfterElementHtml(); 
      return $html; 
     } 

     protected function __readRobots() 
     { 
      $file = fopen(Mage::getBaseDir().'/robots.txt', "r"); 
      if(!$file) return $this->content; 

      $content = ''; 

      while (!feof($file)): 
       $content .= fgets($file); 
      endwhile; 

      fclose($file); 
      return $content; 
     } 

     protected function __writeRobots($content) 
     { 
      $file = @fopen(Mage::getBaseDir().'/robots.txt', 'w'); 
      @fwrite($file, "$content"); 
     } 

     protected function _beforeSave() 
     { 
      self::__writeRobots('text1'); 
     } 

     protected function _afterSave() 
     { 
      self::__writeRobots('text1'); 
     } 
    } 
?> 

文件:/應用/代碼/社區/銷/初始化/座/ Adminhtml /系統 XML:

<robots translate="label"> 
          <label>Robots.txt</label> 
          <comment></comment> 
          <frontend_type>text</frontend_type> 
          <frontend_model>init/adminhtml_system_robots</frontend_model> 
          <sort_order>3</sort_order> 
          <show_in_default>1</show_in_default> 
          <show_in_website>1</show_in_website> 
          <show_in_store>1</show_in_store> 
         </robots> 

回答

0

可以指定你的元素的backend_model然後在模型實現方法_afterSave_beforeSave
您可以使用web/unsecure/base_url元素作爲示例。它在app/code/core/Mage/Core/etc/system.xml中定義,並具有後端模型adminhtml/system_config_backend_baseurl
這相當於Mage_Adminhtml_Model_System_Config_Backend_Baseurl類。
看看裏面發生了什麼。
UPDATE
讓你的配置是這樣的:

<robots translate="label"> 
    <label>Robots.txt</label> 
    <comment></comment> 
    <frontend_type>text</frontend_type> 
    <frontend_model>init/adminhtml_system_robots</frontend_model> 
    <backend_model>init/adminhtml_system_backend_robots</backend_model><!-- add this line --> 
    <sort_order>3</sort_order> 
    <show_in_default>1</show_in_default> 
    <show_in_website>1</show_in_website> 
    <show_in_store>1</show_in_store> 
</robots> 

現在創建下面的類(把它放在正確的文件):

<?php 
class Pin_Init_Block_Adminhtml_System_Backend_Robots extends Mage_Core_Model_Config_Data{ 
    protected function _beforeSave() 
    { 
     //this is called before the value is saved 
     $value = $this->getValue();//this is how you can get the value 
     //do your magic with $value 
    } 
    protected function _afterSave() 
    { 
     //this is called after the value is saved 
     $value = $this->getValue();//this is how you can get the value 
     //do your magic with $value 
    } 
} 
+0

感謝。我添加了有問題的代碼。函數_afterSave和_beforeSave不適用於我。你有什麼想法嗎? – Vladimir

+0

@Vladimir。我編輯了答案。看看這是否有助於你。 – Marius

+0

謝謝你的工作。但我使用「Pin_Init_Model_Robots」和「 init/robots」 – Vladimir