I was wondering if it is possible to pass parameters to the source model from the system.xml. This is how my system.xml currently looks like:
<category>
<label>Category</label>
<frontend_type>select</frontend_type>
<sort_order>3</sort_order>
<source_model>adminhtml/system_config_source_category</source_model>
<comment>I live</comment>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</category>
It works but I have no empty value at the top of the dropdown because I cant pass the boolean parameter true to the source model. Just to make things easy, this is the source model:
<?php
class Mage_Adminhtml_Model_System_Config_Source_Category
{
public function toOptionArray($addEmpty = true)
{
$tree = Mage::getResourceModel('catalog/category_tree');
$collection = Mage::getResourceModel('catalog/category_collection');
$collection->addAttributeToSelect('name')
->addRootLevelFilter()
->load();
$options = array();
if ($addEmpty) {
$options[] = array(
'label' => Mage::helper('adminhtml')->__('-- Please Select a Category --'),
'value' => ''
);
}
foreach ($collection as $category) {
$options[] = array(
'label' => $category->getName(),
'value' => $category->getId()
);
}
return $options;
}
}
1 Answer 1
I think this is a bug or someone overlooked this:
The call to toOptionArray receives the parameter $fieldType == 'multiselect': https://github.com/OpenMage/magento-mirror/blob/magento-1.9/app/code/core/Mage/Adminhtml/Block/System/Config/Form.php#L463
So the empty option is added only if the type of the field is multiselect. From my point of view it should be the other way around. It should be added for simple selects and not added to multiselects.
Since you cannot edit the core, and you would not want to override the method linked above The solution would be to create your own source model that calls the source model you mentioned and just adds an empty option at the top.
So add this class in one of your modules:
<?php
class [Namespace]_[Module]_Model_System_Config_Source_Category
{
public function toOptionArray()
{
return Mage::getModel('adminhtml/system_config_source_category')->toOptionArray(true);
}
}
then declare your system.xml field like this
....
<source_model>[module]/system_config_source_category</source_model>
....
-
Hmm I came to the same conclusion but I wanted to make sure there's no other way to achieve this. This should be reported somewhere. Thanks for the answer.mpfmon– mpfmon2015年07月27日 14:10:36 +00:00Commented Jul 27, 2015 at 14:10
-
I think the empty option was added intentionally because the ordinary user does not know how to completely deselect a multiselect.mam08ixo– mam08ixo2015年07月27日 14:26:18 +00:00Commented Jul 27, 2015 at 14:26
-
@mam08ixo. I don't think that's the reason. I think it should be added to the simple selects, not the multiple selects.Marius– Marius2015年07月27日 14:27:25 +00:00Commented Jul 27, 2015 at 14:27
-
any improvement on magento2 (as of now 2.4.5) for this issue?M2 Dev– M2 Dev2022年08月09日 14:37:20 +00:00Commented Aug 9, 2022 at 14:37