Solving Magento 2 Custom Dropdown Attribute Issues
Hello developers! In today’s post, we’ll be diving into a common issue faced by many while working with Magento 2 - custom dropdown attributes not displaying options correctly.
The Scenario
Our user has created a new product dropdown attribute Typ programmatically using the EAV setup factory. However, when they navigate to the attribute page, the options are missing:

The user suspects that the issue lies in how getAllOptions() is implemented in their TypOptionProvider class.
Understanding the Problem
To solve this, let’s first understand what’s happening under the hood. Magento uses a source model to fetch options for dropdown attributes. In our case, the Typ attribute is configured to use BAG\Migration\Setup\Configuration\Source\TypOptionProvider.
The getAllOptions() method in this provider returns a pre-defined array of options:
public function getAllOptions()
{
// ...
if (!$this->_options) {
$this->_options = [
['label' => __('typ1'), 'value' => 1],
['label' => __('typ2'), 'value' => 2],
// ...
];
}
return $this->_options;
}
However, this method doesn’t fetch options from the database. It always returns the same pre-defined array, regardless of any changes made in the Magento backend.
The Solution Approach
To resolve this issue and display newly added options, we need to modify our TypOptionProvider class to fetch options dynamically from the attribute’s option collection:
- First, make sure you have the correct use statements at the top of your file:
use Magento\Framework\Api\AttributeValueConverterInterface;
use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
- Inject the
attributeValueConverterdependency in your constructor:
public function __construct(
AttributeValueConverterInterface $attributeValueConverter,
\Magento\Eav\Model\Config $eavConfig
) {
$this->attributeValueConverter = $attributeValueConverter;
$this->eavConfig = $eavConfig;
}
- Update the
getAllOptions()method to fetch options from the database:
public function getAllOptions()
{
$attribute = $this->eavConfig->getAttribute(\Magento\Catalog\Model\Product::ENTITY, 'Typ');
if ($attribute) {
$optionsCollection = $attribute->getSource()->getAllOptions(false);
return $optionsCollection;
}
return [];
}
- Make sure your TypOptionProvider class is properly registered in your module’s
etc/di.xmlfile:
<type name="Magento\Eav\Model\Entity\Attribute\Source\AbstractSource">
<arguments>
<argument name="data" xsi:type="array">
<item name="options" xsi:type="array">
<item name="Typ" xsi:type="object">BAG\Migration\Setup\Configuration\Source\TypOptionProvider</item>
</item>
</argument>
</arguments>
</type>
Conclusion
With these changes, your custom dropdown attribute should now correctly display options added in the Magento backend. The key takeaway here is that source models determine how an attribute fetches its options.
Remember to always test your changes thoroughly after deployment.
I hope this guide helps you overcome this common Magento 2 attribute issue. If you found this solution helpful, feel free to share it with other developers! And if you’re looking for more in-depth guides and tutorials on Magento, check out Laravel Company.
Happy coding!