2

Writing a new module and in my system.xml I can put the following:

<field id="environment" translate="label" type="select" showInDefault="1" showInWebsite="1" showInStore="1">
    <label>Environment</label>
    <source_model>Magento\Config\Model\Config\Source\Enabledisable</source_model>  
</field>

And I get a select input with two options, Enable and Disable.

If I write my own Source class, and put the correct class namespace path, I get the following error:

Exception #0 (ReflectionException): Class Vendor\Module\Model\Config\Source\Environment does not exist

Environment.php

<?php

namespace Vendor\Module\Model\Config\Source;

use Magento\Framework\Data\OptionSourceInterface;

class Environment implements OptionSourceInterface
{
    public function toOptionArray()
    {
        return [
            ['value' => 0, 'label' => __('Sandbox')],
            ['value' => 1, 'label' => __('Production')]
        ];
    }
}

Updated system.xml field

<field id="environment" translate="label" type="select" showInDefault="1" showInWebsite="1" showInStore="1">
    <label>Environment</label>                        
    <source_model>Vendor\Module\Model\Config\Source\Environment</source_model>
</field>
4
  • Did you get a solution for this, magento really makes it hard for developers, one would be really out of options to use it May 19 at 4:21
  • Sorry bud, I can't remember. I quit the job not long after, and I've been a NodeJS developer since. Thankfully forgotten how to develop for Magento.
    – Mr Pablo
    May 20 at 13:12
  • Hahaha, Php had the man running... Though I managed to figure it out, Magento changed the way Models are loaded with each having a config file in a related directory. The issue is we get our way around using online tutorials (maybe because Magento documentation is probably written by a bunch of farmers, hard to follow) which are now all outdated May 20 at 14:35
  • Yup, the official docs suck, and I've told people at Magento many a time. It was Magento that had me running, not PHP :P
    – Mr Pablo
    May 20 at 15:22

2 Answers 2

1

Try This :-

<?php

namespace Vendor\Module\Model\Config\Source;


class Environment{
    public function toOptionArray()
    {
        return array(
            array('value' => '0', 'label'=>'Sandbox'),
            array('value' => '1', 'label'=>'Production'),
        );
    }
}
1
  • How is removing the interface and using traditional array notation going to suddenly make the application see the class? Plenty of other custom source models, in other modules, work the way I have written mine.
    – Mr Pablo
    Dec 11, 2019 at 8:59
0
<?php

namespace Vendor\Module\Model\Config\Source;

use Magento\Framework\Data\OptionSourceInterface;

/**
 * Class Environment implements OptionSourceInterface
 *
 * @package Vendor\Module\Model\Config\Source
 */
class Environment implements OptionSourceInterface
{
    /**
     * @return array
     */
    public function toOptionArray()
    {
        return [
            ['value' => 0, 'label' => __('Sandbox')],
            ['value' => 1, 'label' => __('Production')]
        ];
    }
}

Please check this code, this has to work.

1
  • you just literally copied the question and added comments... May 19 at 4:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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