0

How can i call admin configuration values like enable,input field value in template file.

8

1 Answer 1

0

The best way to illustrate this is by examining what Magento does in its core code. We can use downloadable products for this example.

In Stores > Settings > Configuration > Catalog > Catalog, you can set Default Link Title:

Downloadable Product Options

This is Magento's default value to display a title for the downloadable links section. The question is how we can get this value from the admin panel into the frontend template.

First, the admin value is stored at a config path. There are multiple routes to discover the name of this path. We can examine XML configurations (this one is configured here). We can inspect the admin page in our browser for clues. We can search for it in the core_config_data table. In this case, we're lucky: Magento's DevDocs tell us the path is catalog/downloadable/links_title.

Second, the Block class that's bound to our template retrieves and returns the value at this path. Magento makes this easy. Any class that extends ultimately from \Magento\Framework\View\Element\AbstractBlock has access to the \Magento\Framework\App\Config\ScopeConfigInterface $_scopeConfig property. This gives us access to values configured in admin.

Here's how it works in our core example. \Magento\Downloadable\Model\Link stores the config path in a constant:

const XML_PATH_LINKS_TITLE = 'catalog/downloadable/links_title';

The \Magento\Downloadable\Block\Catalog\Product\Links Block class uses this constant in its getLinksTitle function to retrieve the configured value at that path using $_scopeConfig:

public function getLinksTitle()
{
    if ($this->getProduct()->getLinksTitle()) {
        return $this->getProduct()->getLinksTitle();
    }
    return $this->_scopeConfig->getValue(
        \Magento\Downloadable\Model\Link::XML_PATH_LINKS_TITLE,
        \Magento\Store\Model\ScopeInterface::SCOPE_STORE
    );
}

And just like that, our Block class knows the admin value.

Third, the template that's bound to this Block class calls the appropriate function from the Block. In this example, Magento/Downloadable/view/frontend/templates/catalog/product/links.phtml calls the getLinksTitle function and displays the configured admin value in this way:

<label class="label"><span><?= $block->escapeHtml($block->getLinksTitle()) ?></span></label>

That's really all there is to it. Find the path, build a function in your template Block that uses $_scopeConfig to retrieve and return the value at that path, and call that Block function in your template to display that value. You'll do this a lot in Magento, so getting comfortable with this process is fundamental to being a Magento developer. Good luck!

4
  • am using magento2.2 template file is there...need to overide block or use any existing block file
    – User0434
    Oct 7, 2021 at 15:46
  • The mechanics are the same. You'll need to extend that block in order to add a function to retrieve the value you need, and then override the template to call the function.
    – kookaburra
    Oct 7, 2021 at 21:30
  • my template file already override...how to add block in that plz explain
    – User0434
    Oct 8, 2021 at 5:10
  • This answer (below) gives a good example of how to do this. You'll need to (1) create a custom block that extends whichever block is bound to the template, (2) add a function to your custom block that retrieves the value you need, and (3) add a preference for your custom block to di.xml. Here are some visual examples of how to do that: magento.stackexchange.com/a/305713/76310
    – kookaburra
    Oct 8, 2021 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.