3

I need the value of selected value from system config file.

Below is my code example.

system.xml file

<field id="test" translate="label" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
     <label>Select Product Tab</label>
     <source_model>Vendor\Modulename\Model\Config\Source\Products</source_model>                 
</field>

Model file

namespace Vendor\Modulename\Model\Config\Source;

use Magento\Framework\Option\ArrayInterface;

class Products implements ArrayInterface
{

    /*
     * Option getter
     * @return array
     */
    public function toOptionArray()
    {
        $arr = $this->toArray();
        $ret = [];
        foreach ($arr as $key => $value) {
            $ret[] = [
                'value' => $key,
                'label' => $value
            ];
        }
        return $ret;
    }

    /*
     * Get options in "key-value" format
     * @return array
     */
    public function toArray()
    {
        $valList = [
            '1' => 'A',
            '2' => 'B',
            '3' => 'C',
            '4' => 'D'

        ];
        return $valList;
    }
}

I need selected value in block or template file

1
  • have you got answer? need your comments. Commented Dec 27, 2016 at 9:02

3 Answers 3

0

system.xml

        <field id="super_admin"
               translate="label"
               type="select"
               sortOrder="10"
               showInDefault="1"
               showInWebsite="1"
               showInStore="1">
            <label>Select Admin</label>
            <source_model>VendoreName\ModuleName\Model\Adminhtml\System\Config\Source\AdminUserList</source_model>
          <validate>required-entry</validate>
        </field>

app\code\VendoreName\ModuleName\Model\Adminhtml\System\Config\Source

AdminUserList.php

<?php

namespace VendoreName\ModuleName\Model\Adminhtml\System\Config\Source;

use Magento\User\Model\ResourceModel\User\CollectionFactory as UserCollectionFactory;

class AdminUserList implements \Magento\Framework\Option\ArrayInterface
{
    protected $_userFactory;

    public function __construct(
        UserCollectionFactory $userFactory
    ) {
        $this->_userFactory = $userFactory;
    }
    public function getOptionArray()
    {
        $adminUsers = $this->_userFactory->create();
        $options = [];
        foreach ($adminUsers as $adminUser) {
            $options[$adminUser->getId()] = $adminUser->getUsername();
        }
        return $options;
    }
    public function getAllOptions()
    {
        $res = $this->getOptions();
        array_unshift($res, ['value' => '', 'label' => '']);
        return $res;
    }
    public function getOptions()
    {
        $res = [];
        foreach ($this->getOptionArray() as $index => $value) {
            $res[] = ['value' => $index, 'label' => $value];
        }
        return $res;
    }
    public function toOptionArray()
    {
        return $this->getOptions();
    }
}

app\code\VendoreName\ModuleName\Helper

Data.php

<?php

namespace VendoreName\ModuleName\Helper;

use Magento\Store\Model\ScopeInterface;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{

    const ADMIN_USER_NAME = 'custom_module/general/super_admin'; // sectionName/GroupName/FieldName from system.xml

    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
    ) {
        $this->scopeConfig = $scopeConfig;
    }

    public function setStoreScope()
    {
        return ScopeInterface::SCOPE_STORE;
    }
    public function isEnabled()
    {
        return $this->scopeConfig->getValue(static::ADMIN_USER_NAME, $this->setStoreScope());
    }
}

I Hope This Helps You.

0

You need to inject the instance of \Magento\Framework\App\Config\ScopeConfigInterface in your block.

protected $scopeConfig;
public function __construct(
    ....
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    ....
) {
    ...
    $this->scopeConfig = $scopeConfig;
    ....
}

Then create the method getStoreValue()

public function getStoreValue()
{
    return $this->scopeConfig->getValue(
        'general/store_information/name',
        \Magento\Store\Model\ScopeInterface::SCOPE_STORE
    );
}

and call in your template echo $this->getStoreValue()

-1

You can use ScopeConfigInterface class to get value

<?php 

protected $_storeConfig;

    public function __construct(
        ---
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    ) {
        $this->_storeConfig = $scopeConfig;
        echo $this->_storeConfig->getValue('sectionName/groupName/fieldName');die;
        parent::__construct($context);
    }
1
  • this code is not working...
    – Deep Shah
    Commented Oct 28, 2021 at 11:04

Your Answer

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

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