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

标签 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/>

谢谢。

最佳答案

这是您需要查看 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 代码来呈现输入。因此,如果您想在模板中执行相同的操作,我会尝试这样的操作

在类中,为整个元素分配一个 block 属性。这实际上比从元素中提取值更有效,因为 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”,而是想“我需要以与其他队友相同的方式将此功能添加到商店”。然后看看核心团队是如何做到的,并复制他们的实现,根据需要进行尽可能少的更改。

您使用系统的次数越多,它确实会变得更容易!

关于configuration - Magento 配置 - 使用 frontend_model 时填充字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5634960/

相关文章:

variables - 如何在 Grails 配置文件中定义私有(private)变量?

单击任何编辑链接时,Magento 单页链接会向当前 url 添加哈希(#)?

Magento:如何以编程方式创建子 block ?

php - magento 不发送任何邮件,如何调试?

configuration - 使用Spring Boot和Thymeleaf加载静态资源

java - IntelliJ Idea突然丢失JBOSS管理端口配置

version-control - 修订与版本

android - 何时对配置更改做出准确 react

mysql - Google Data Studio MySQL 日期问题

php - 无法重新声明类 Magento