cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to use custom source_model

Unable to use custom source_model

Hello,

I'm trying to create one custom source_model but getting the error "Class Demo\Test\Model\Config\Source\custom does not exist". 

In system.xml, we have added below code:

<field id="test" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Test</label>
<source_model>Demo\Test\Model\Config\Source\custom</source_model>
</field>

And our file app\code\Demo\Test\Model\Config\Source\Custom.php contain below code:

<?php
namespace Demo\Test\Model\Config\Source;

class custom implements \Magento\Framework\Option\ArrayInterface
{
public function toOptionArray()
{
return [
['value' => 'test', 'label' => __('test')],
['value' => 'demo', 'label' => __('demo')]
];
}
}

Can some one let me know what is wrong with this code.

Thanks!

7 REPLIES 7

Re: Unable to use custom source_model

Hi @supportopeadb2 

 

Please use the below code and let me know in case you face any issues.

<field id="test" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
   <label>Test</label>   <source_model>Demo\Test\Model\Config\Source\Custom</source_model>
</field>

Then create this file Demo\Test\Model\Config\Source\Custom.php

namespace Demo\Test\Model\Config\Source;

class Custom implements \Magento\Framework\Data\OptionSourceInterface
{
 public function toOptionArray()
 {
  return [
    ['value' => 'grid', 'label' => __('Grid Only')],
    ['value' => 'list', 'label' => __('List Only')],
    ['value' => 'grid-list', 'label' => __('Grid (default) / List')],
    ['value' => 'list-grid', 'label' => __('List (default) / Grid')]
  ];
 }
}

Replace custom with Custom and use the modified code.

 

Thank you.

Re: Unable to use custom source_model

Hello,

We have checked it by using your code but still getting the same issue.

Thanks!

Re: Unable to use custom source_model

Hi @supportopeadb2 

 

Can you please try deleting the generated folder and then use the below command to flush the cache.

php bin/magento cache:flush

Also when you are checking on admin please log out first and then again try to check after login.

Let me know in case you face any issues.

 

Thank you.

Re: Unable to use custom source_model

Hello,

Thank you for your quick response. Our issue is resolved now.

Thanks!

Re: Unable to use custom source_model

Hi @supportopeadb2 

 

Good to hear that.

You can mark it as a solution or if you had change something else then please share it here so that anyone facing the same issue can check for the exact solution.

 

Thank you.

Re: Unable to use custom source_model

Hello,

The issue was mismatch of case in directory structure. So we have corrected it and it worked.  

Thanks!

Re: Unable to use custom source_model

Hi @supportopeadb2 

 

Yeah, that's why in my reply I have told you to replace the custom with Custom.