How to create custom source model for custom attribute type select?

How to create custom source model for custom attribute type select?

July 26, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to create custom source model for custom attribute type select?

Customizing Magento: Creating Your Own Source Model for Select Attribute Types

As an experienced Magento developer, you’ve probably faced situations where you need to create custom attributes with select options that are populated programmatically rather than manually typed in the admin panel. By default, Magento assigns eav/entity_attribute_source_table as the source model for attribute types like “select”, but this can limit your flexibility.

The Challenges of Using the Default Source Model

The main issues you might encounter when using the default source model are:

  1. Limited Auto-Population: You cannot auto-populate the select options with data taken from external sources, such as a database or API calls.
  2. Empty First Option: Even if you specify a “default” or “default_value”, the field still displays an empty option at the top of the dropdown menu.

Creating Your Own Custom Source Model

To overcome these limitations, you can create your own custom source model that inherits from the Magento\Eav\Model\Entity\Attribute\Source\AbstractSource. Here’s a step-by-step guide:

1. Create the New Module

First, create a new module in your Magento installation using the following command:

bin/magento setup:module:create --module-name=Vendor_Name/ModuleName

Replace Vendor_Name with your vendor name and ModuleName with the desired module name.

2. Define Your Source Model

Navigate to the newly created module’s etc/di.xml file. Add the following configuration to define your new source model:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Eav\Model\Entity\Attribute\Source\AbstractSource">
        <arguments>
            <argument name="sourceModel" xsi:type="array">
                <item name="Vendor_Name_ModuleName" xsi:type="string">Vendor_Name\ModuleName\Model\Source\CustomSelect</item>
            </argument>
        </arguments>
    </type>
</config>

3. Implement the Custom Source Model

Create a new file in Vendor_Name/ModuleName/Model/Source/CustomSelect.php. In this file, implement your custom source model:

namespace Vendor_Name\ModuleName\Model\Source;

use Magento\Framework\Data\OptionSourceInterface;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Magento\Framework\App\ResourceConnection;

class CustomSelect implements OptionSourceInterface
{
    private $connection;
    private $collection;

    public function __construct(ResourceConnection $resource)
    {
        $this->connection = $resource->getConnection('core_read');
    }

    public function getAllOptions()
    {
        $result = [];

        // Query your options from the database or external API here
        $sql = "SELECT option_id, option_name FROM my_table";
        $options = $this->connection->fetchAll($sql);

        foreach ($options as $option) {
            $result[] = [
                'value' => $option['option_id'],
                'label' => $option['option_name']
            ];
        }

        return $result;
    }
}

In the getAllOptions method, you can fetch your data from a database table or an external API using the Magento resource connection.

4. Assign Your Custom Source Model to the Attribute

When creating or editing attributes programmatically, make sure to assign your custom source model:

$attribute = $installer->createAttribute()
    ->setAttributeCode('my_attribute')
    ->setBackendType('int')
    ->setFrontendInput('select')
    ->setSortOrder(10)
    ->setIsUserDefined(true)
    ->setDefaultValue(null)
    ->setIsVisible(true)
    ->setIsSearchable(true)
    ->setIsFilterable(true)
    ->setIsComparable(false)
    ->setUsedInProductListing(true)
    ->setApplyTo('simple');

$attribute->save();

$installer->addAttributeToSet(
    $entityTypeId,
    $attributeGroupId,
    null,
    'my_attribute'
);

Conclusion

Creating a custom source model for select attribute types in Magento allows you to auto-populate the options programmatically and eliminates the empty first option issue. By following this guide, you can now have full control over your attribute select options and fetch them from any data source.

Remember that proper testing is essential when making changes to core functionality. Always ensure that your customizations are well-documented and follow Magento’s best practices for a smooth integration with other modules and updates.

For more advanced techniques in extending Magento, consider exploring our comprehensive guide at LaravelCompany.com.

Database API Extensions Admin

Our network