skip to Main Content

I am trying to get backend model class name from system.xml
Right now i am using this code.

magento/app/code/Company/Sso/etc/adminhtml/system.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
    <section id="admin">
        <group id="sso_saml" translate="label" sortOrder="100" showInDefault="1" showInWebsite="0" showInStore="0" >
            <label>Single Sign on(SAML)</label>
            <field id="is_enabled" translate="label comment" type="select" sortOrder="0" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>Is Enabled SAML</label>
                <comment>Enable Single Sign On</comment>
                <source_model>MagentoConfigModelConfigSourceYesno</source_model>
            </field>
        </group>
    </section>
</system>

Magento Code

$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$structure = $objectManager->create('MagentoConfigModelConfigStructureFactory');
$field = $objectManager->create('MagentoConfigModelConfigStructure')->getElementByConfigPath('admin/sso_saml/is_enabled');
print json_encode($field->getData());

Output:

{"id":"is_enabled","path":"admin/sso_saml","_elementType":"field"}

But i need source_model, can anyone help?

2

Answers


  1. Chosen as BEST ANSWER

    Here is full solution after implementing Matt's solution.

    $objectManager = MagentoFrameworkAppObjectManager::getInstance();
    $appState = $objectManager->get('MagentoFrameworkAppState');
    $field = $appState->emulateAreaCode('adminhtml', function($path){
        $bootstrap = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);
        $objectManager = MagentoFrameworkAppObjectManager::getInstance();
        $appState = $objectManager->get('MagentoFrameworkAppState');
        $appState->setAreaCode('adminhtml');
        $field = $objectManager->create('MagentoConfigModelConfigStructure')
            ->getElementByConfigPath($path->getPath());
        return $field;
    }, [$path]);
    
    $value = $path->getData()['backend_model'];
    

  2. You need to be in the adminhtml area in order to load the rest of the data:
    e.g

    <?php
    require __DIR__ . '/app/bootstrap.php';
    $bootstrap = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    $appState = $objectManager->get('MagentoFrameworkAppState');
    $appState->setAreaCode('adminhtml');
    $field = $objectManager->create('MagentoConfigModelConfigStructure')->getElementByConfigPath('customer/create_account/auto_group_assign');
    echo $field->getData()['source_model'];
    
    > MagentoConfigModelConfigSourceYesno
    

    You could potentially use MagentoFrameworkAppState::emulateAreaCode to fetch the data from within a non-admin context if necessary.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search