Magento配置 - 使用frontend_model时填充字段

时间:2011-04-12 12:10:46

标签: configuration magento

我在system.xml中为输入字段指定了frontend_model。我这样做是因为我想把字段设为只读。可能有一种更直接的方式来实现这一点,但这就是我现在所处的位置。问题是我不能让字段显示它应该的数据。

我有一个按钮,按下后,填充只读字段。这很好。但是当我点击“保存配置”时,数据会从字段中消失。它消失的原因是因为我无法找出我应该设置字段值的内容。下面尝试使用Varien_Data_Form_Element_Abstract的getEscapedValue()方法,但它不返回任何内容。和Magento一样,没有文档可以说。

class Mypackage_MyModule_Block_Adminhtml_System_Config_DisabledText extends Mage_Adminhtml_Block_System_Config_Form_Field
{
    protected function _prepareLayout()
    {
        parent::_prepareLayout();
        if (!$this->getTemplate()) {
            $this->setTemplate('mypackage/system/config/disabled_text.phtml');
        }
        return $this;
    }

    public function render(Varien_Data_Form_Element_Abstract $element)
    {
        $element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
        return parent::render($element);
    }

    protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
    {
        $originalData = $element->getOriginalData();
        $this->addData(array(
            'my_value' => $element->getEscapedValue(),
            'html_id' => $element->getHtmlId(),
        ));
        return $this->_toHtml();
    }
}

disabled_text.phtml包含以下内容:

<input id="<?php echo $this->getHtmlId() ?>" value="<?php echo $this->getMyValue(); ?>" class=" input-text" type="text" disabled/>

感谢。

1 个答案:

答案 0 :(得分:6)

这是您需要了解Magento本身是如何做类似于您想做的事情的地方之一。如果您查看Mage_Adminhtml_Block_System_Config_Form_Field班级的_getElementHtml

protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
{        
    return $element->getElementHtml();
}

你可以看到这个方法接受一个已经实例化的表单元素(基于system.xml中的内容),然后这个元素用getElementHtml呈现自己。这意味着当Magento需要渲染(并且反过来获取值)时,它会从元素对象中进行渲染。一些粗略的调试将告诉我们getElementHtml可以找到的位置

protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
{        
    var_dump(get_class($element));    
    return $element->getElementHtml();
}

Varien_Data_Form_Element_Text之类的内容将被转储到屏幕上。反过来,此类继承Varien_Data_Form_Element_Abstract形式,其中包含以下定义

public function getElementHtml()
{
    $html = '<input id="'.$this->getHtmlId().'" name="'.$this->getName()
         .'" value="'.$this->getEscapedValue().'" '.$this->serialize($this->getHtmlAttributes()).'/>'."\n";
    $html.= $this->getAfterElementHtml();
    return $html;
}

因此,当 Magento 想要获取系统配置字段的值时,它会使用上面的PHP代码来呈现输入。所以,如果你想在你的模板中做同样的事情,我会尝试这样的事情

在类中,使用整个元素分配块属性。这实际上更有效的是从元素中提取值,因为所有PHP需要存储都是对象引用。

protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
{
    $this->setMyElement($element);
    return $this->_toHtml();
}

然后,在模板中,从magento的渲染中复制代码,用保存的元素替换“$ this”关键字

<?php $_element = $this->getMyElement(); ?>
<!-- check the quoting/escaping on this html/php, I didn't actually run it, but the concept is sound -->
<input  disabled="disabled" id="<?php echo $_element->getHtmlId();?>" name="<?php echo $_element->getName();?>" 
        value="<?php echo $_element->getEscapedValue();?>" 
        <?php echo $_element->serialize($_element->getHtmlAttributes());?>
/>
<?php echo $_element->getAfterElementHtml(); ?>            

当您与Magento合作时,请尝试像Magento开发人员一样思考。而不是“我需要弄清楚如何使它做X”,想想“我需要以与其他队友一样的方式将此功能添加到商店”。然后看看核心团队是如何做到的,并复制他们的实现,尽可能少地改变。

使用该系统的次数越多越容易!

相关问题