I am creating a standard Magento widget. In the options tab for the widget I have a field. This field is for filters in the form of key:'value'
pairs. I would like the user to be able to dynamically create as many filters as they need.
So, ideally the user clicks a button 'Add new Filter' and a new field is generated below the original field.
I have an idea that I can use a custom <source_model>
, but I am not sure what javascript methods or Mage classes already handle dynamic buttons such as this. I'm thinking there has to be an easy way.
My widget.xml file looks like this:
<?xml version="1.0"?>
<widgets>
<namespace_module type="module/links">
<name>Widget Name</name>
<description>Display a gizmo</description>
<parameters>
<filters>
<label>Filters</label>
<visible>1</visible>
<required>0</required>
<type>text</type>
<source_model>module/filters</source_model>
</filters>
</parameters>
</namespace_module>
</widgets>
Then I have app/code/community/Namespace/Module/Model/Filters.php
which looks like:
class Namespace_Module_Model_Filters {
public function toOptionArray() {
return array(
array('value' => 'val', 'val' => '1'),
array('value' => 'val2', 'val2' => '2'),
array('value' => 'val3', 'val3' => '3'),
);
}
// Can I do anything here to generate dynamic fields?
}
Perhaps this is not the correct way to go about this? Is it possible to create a custom <type>
and generate the buttons there? Maybe I just need more coffee ["])
UPDATE: I'm now looking at the <type>multiline</type>
parameter, to see if I can use that. However, I think I'm still going to need to create a custom multiline. The reason is that Varien_Data_Form_Element_Multiline
has the line count hardcoded:
public function __construct($attributes=array())
{
parent::__construct($attributes);
$this->setType('text');
$this->setLineCount(2); // hardcoded
}
Best Answer
I've not got much experience with widgets, but if this works like any other admin panel form you could make use of
Mage_Adminhtml_Block_System_Config_Form_Field_Array_Abstract
and store your filters as a serialised array?Have a look at
Mage_CatalogInventory_Block_Adminhtml_Form_Field_Minsaleqty
for an implementation of it.It should allow you to add dynamic rows and store them all under one serialised attribute, kind of like how this picture shows.