How can i call admin configuration values like enable,input field value in template file.
-
Does this answer your question? Get config value for specific store from admin area– MsquareOct 7, 2021 at 10:46
-
@Msquare am new to this can explain clearly– User0434Oct 7, 2021 at 10:50
-
Try this magento.stackexchange.com/a/84484/82670– MsquareOct 7, 2021 at 10:52
-
do you have block file and helper file into your extension?– Dhiren VasoyaOct 7, 2021 at 10:54
-
@DhirenVasoya am having core module that config values(enable,sitekey) i need to call in phtml file– User0434Oct 7, 2021 at 11:07
1 Answer
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:
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!
-
am using magento2.2 template file is there...need to overide block or use any existing block file– User0434Oct 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. Oct 7, 2021 at 21:30
-
my template file already override...how to add block in that plz explain– User0434Oct 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 Oct 8, 2021 at 13:26