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>
-
Did you get a solution for this, magento really makes it hard for developers, one would be really out of options to use itRichard Muvirimi– Richard Muvirimi2022年05月19日 04:21:10 +00:00Commented May 19, 2022 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– Mr Pablo2022年05月20日 13:12:17 +00:00Commented May 20, 2022 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 outdatedRichard Muvirimi– Richard Muvirimi2022年05月20日 14:35:51 +00:00Commented May 20, 2022 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 :PMr Pablo– Mr Pablo2022年05月20日 15:22:01 +00:00Commented May 20, 2022 at 15:22
2 Answers 2
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'),
);
}
}
answered Dec 11, 2019 at 6:17
Ronak Rathod
6,58020 silver badges46 bronze badges
-
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– Mr Pablo2019年12月11日 08:59:46 +00:00Commented Dec 11, 2019 at 8:59
<?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.
-
you just literally copied the question and added comments...Richard Muvirimi– Richard Muvirimi2022年05月19日 04:21:55 +00:00Commented May 19, 2022 at 4:21
default