System.xml 中文本字段的 Magento2 setValue()

Magento2 setValue() of a text field in System.xml

我在这个文件 (System.xml) 上为我的 Magento2 模块创建了一个配置屏幕,我有一个输入文本字段,如下所示:

<field id="postback_url" type="text"...>
     <backend_model>Vendorname\Modulename\Model\Config\Source\Configs<backend_model>
</field>

我需要插入一个默认值。该值将 BaseUrl + /some-endpoint

如何在文本字段中插入默认值? 我不太确定如何在 Magento2.x 中完成 在 magento 1.x 我用过这个:

class myClassName extends Mage_Core_Model_Config_Data{
    protected function _afterLoad(){
       $this->setValue("my URL goes here");
    }

但显然,它不适用于 Magento 2.x

提前致谢!

在您的模块等文件夹中创建一个 config.xml 文件:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <yoursectionid>
            <yourgroupid>
                <yourfieldid>somevalue</yourfieldid>
            </yourgroupid>
        </yoursectionid>
    </default>
</config>

要获取动态值,例如,要在评论和/评论中获取动态值,您应该使用类似 Namespace\ModuleName\Model\Config\Source\ConfigCommentin 模型 class 然后在 class Namespace\ModuleName\Model\Config\Source\ConfigComment.php

class ConfigComment implements \Magento\Config\Model\Config\CommentInterface
{

  public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Store\Model\StoreManagerInterface $storeManager
) {
      $this->_storeManager = $storeManager;
  }


  public function getCommentText($elementValue)
  {
     $baseurl = $this->_storeManager->getStore()->getBaseUrl();
     return $baseurl;
  }
}