skip to Main Content

I’ve created a configuration screen for my Magento2 module, on this file (System.xml) I’ve an input text field like following:

<field id="postback_url" type="text"...>
     <backend_model>VendornameModulenameModelConfigSourceConfigs<backend_model>
</field>

I need to insert a default value. This value will the BaseUrl + /some-endpoint

How do I insert a default value on my text field?
i’m not quite sure how it can be done in Magento2.x
In magento 1.x I’ve used this:

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

But apparently, it doesn’t work on Magento 2.x

Thank you in advance!

2

Answers


  1. In your modules etc folder create a config.xml file:

    <?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>
    
    Login or Signup to reply.
  2. To get a dynamic value, for example, to get a dynamic value inside the comment and /comment, you should use something like NamespaceModuleNameModelConfigSourceConfigCommentin the model class and then in class NamespaceModuleNameModelConfigSourceConfigComment.php

    class ConfigComment implements MagentoConfigModelConfigCommentInterface
    {
    
      public function __construct(
        MagentoFrameworkViewElementTemplateContext $context,
        MagentoStoreModelStoreManagerInterface $storeManager
    ) {
          $this->_storeManager = $storeManager;
      }
    
    
      public function getCommentText($elementValue)
      {
         $baseurl = $this->_storeManager->getStore()->getBaseUrl();
         return $baseurl;
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search