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>
感謝。我添加了有問題的代碼。函數_afterSave和_beforeSave不適用於我。你有什麼想法嗎? – Vladimir
@Vladimir。我編輯了答案。看看這是否有助於你。 – Marius
謝謝你的工作。但我使用「Pin_Init_Model_Robots」和「 init/robots backend_model>」 –
Vladimir