Adding a Dropdown Field in Magento Admin Panel
Are you looking to customize your Magento admin panel by adding an extra dropdown field under System > Configuration > Catalog > Catalog? You’re not alone.
The Magento admin configuration provides a lot of flexibility, allowing you to extend its functionality using modules. Let’s walk through how you can add a new dropdown option under the “Product Listing Sort by” setting.
Creating the Module
First, create a new module directory in your app/code folder following this structure: Company/Module. Replace Company with your vendor name and Module with the unique identifier for your extension.
- /app
- /code
- Company
- Module
- etc
- view
- adminhtml
- template
Defining the Configuration
Next, you need to define your system configuration settings. Create a new XML file at Company/Module/etc/adminhtml/system.xml.
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="catalog" translate="label" sortOrder="100">
<label>Catalog</label>
<tab>general</tab>
<resource>Magento_Catalog::config_catalog</resource>
<group id="product_listing_sortby" sortOrder="25" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Product Listing Sort by</label>
<field type="select" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="0">
<label>The New Option</label>
<source_model>Company\Module\Model\Config\Source\NewOption</source_model>
</field>
</group>
</section>
</system>
</config>
Notice how we’ve added a new field called “The New Option” inside the existing group product_listing_sortby. We’re using a source model to populate this dropdown’s options.
Creating the Source Model
Now let’s create the source model. In your Company/Module/Model/Config folder, make a new file called Source/NewOption.php.
<?php
namespace Company\Module\Model\Config;
class NewOption implements \Magento\Framework\Option\ArrayInterface
{
public function toOptionArray()
{
return [
['value' => 'new_option', 'label' => 'The New Option'],
];
}
}
This model will provide the options for your new dropdown field. In this case, we’re adding a single option called “The New Option”.
Refreshing Your Configuration
To see your changes in action:
- Log out of Magento.
- Clear your cache using
bin/magento cache:clean. - Log back into your admin panel.
You should now see your new dropdown field titled “The New Option” under System > Configuration > Catalog > Catalog > Product Listing Sort by.
Conclusion
Adding custom dropdown fields to the Magento admin configuration is a powerful way to extend its functionality and make it more tailored to your specific needs. By following these steps, you can create a module that adds an additional option for sorting product listings.
Remember, when working with Magento modules, always ensure you’re using version control systems like Git to keep track of changes and avoid conflicts. If you find yourself needing more advanced customizations or help with module development, don’t hesitate to reach out to the experts at Laravel Company.
Happy coding!