Please try this code.
NOTE: You can change class name from Test
to anything.
app/code/Company/Module/etc/adminhtml/system.xml
<field id="customer_list" translate="label" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Select Customer</label>
<source_model>Company\Module\Model\Test</source_model>
</field>
app/code/Company/Module/Model/Test.php
<?php
namespace Company\Module\Model;
use Magento\Framework\Option\ArrayInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
class Test implements ArrayInterface
{
/**
* @var CustomerRepositoryInterface
* */
protected $customerRepository;
/**
* @var SearchCriteriaBuilder
*/
protected $searchCriteriaBuilder;
/**
* @param CustomerRepositoryInterface $customerRepository
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* */
public function __construct(
CustomerRepositoryInterface $customerRepository,
SearchCriteriaBuilder $searchCriteriaBuilder
)
{
$this->customerRepository = $customerRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
}
/*
* 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()
{
$searchCriteria = $this->searchCriteriaBuilder->create();
$customers = $this->customerRepository->getList($searchCriteria)->getItems();
$customersList = array();
foreach ($customers as $customer)
{
$customersList[$customer->getId()] = __($customer->getFirstname() . ' ' . $customer->getLastname());
}
return $customersList;
}
}