Magento的。重写管理自定义字段的保存

时间:2013-12-09 11:40:38

标签: magento field admin

我创建了新字段并将其包含在system.xml中(Pin_Init_Block_Adminhtml_System_Robots) 显示由_getElementHtml控制或渲染。 我怎么能抓住救人?

   <?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');
        }
    }
?>

文件:/ app / code / community / Pin / Init / Block / Adminhtml / System 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>

1 个答案:

答案 0 :(得分: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 看看那里发生了什么 的更新
使您的配置看起来像这样:

<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
    }
}