I have created a custom module in magento2. And created ui component form to save title and sort_order value, now I want to add brands as multi-select field in my module's form.
How to add a multi-select field for brands attribute options?
magento-2.1magento2multiselect-attributeuicomponent
I have created a custom module in magento2. And created ui component form to save title and sort_order value, now I want to add brands as multi-select field in my module's form.
How to add a multi-select field for brands attribute options?
add this before$model->setData($postData);
$postData['customer_group_id']=implode(',',$postData['customer_group_id']);
$model->setData($postData);
Okay finally I've resolved this by myself here is the full code of my module...
registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_Module',
__DIR__
);
etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Vendor_Module" setup_version="1.0.0"></module>
</config>
Setup/InstallData.php
<?php
namespace Vendor\Module\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
private $eavSetupFactory;
public function __construct(
EavSetupFactory $eavSetupFactory
)
{
$this->eavSetupFactory = $eavSetupFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
"testone",
[
'group' => "",
'label' => "Test One",
'is_html_allowed_on_front' => true,
'default' => '1',
'note' => '',
'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
'visible' => true,
'required' => false,
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => true,
'visible_in_advanced_search' => false,
'unique' => false,
"frontend_class" => "",
"used_in_product_listing" => true,
"input" => "select",
"type" => "varchar",
"source" => "Vendor\Module\Model\Config\Source\TimeSetup",
'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend'
]
);
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
"testtwo",
[
'group' => "",
'label' => "Test Two",
'is_html_allowed_on_front' => true,
'default' => '1',
'note' => '',
'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,
'visible' => true,
'required' => false,
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => true,
'visible_in_advanced_search' => false,
'unique' => false,
"frontend_class" => "",
"used_in_product_listing" => true,
"input" => "select",
"type" => "varchar",
"source" => "Vendor\Module\Model\Config\Source\TimeSetup",
'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend'
]
);
}
}
Model\Config\Source\TimeSetup.php
<?php
namespace Vendor\Module\Model\Config\Source;
class TimeSetup extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
protected $_optionsData;
/**
* getAllOptions
*
* @return array
*/
public function getAllOptions()
{
if ($this->_options === null) {
$this->_options = [
['value' => 'one', 'label' => __('one')],
['value' => 'two', 'label' => __('two')]
];
}
return $this->_options;
}
final public function toOptionArray()
{
return array(
array('value' => 'one', 'label' => __('one')),
array('value' => 'two', 'label' => __('two'))
);
}
}
view/adminhtml/ui_component/product_form.xml
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="testingproduct">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Testing Group</item>
<item name="provider" xsi:type="string">product</item>
<item name="dataScope" xsi:type="string">data.product</item>
<item name="sortOrder" xsi:type="number">2</item>
<item name="collapsible" xsi:type="boolean">true</item>
<item name="opened" xsi:type="boolean">false</item>
<item name="ns" xsi:type="string">product_form</item>
</item>
</argument>
<container name="testing_group">
<argument name="data" xsi:type="array">
<item name="type" xsi:type="string">group</item>
<item name="config" xsi:type="array">
<item name="formElement" xsi:type="string">container</item>
<item name="component" xsi:type="string">Magento_Ui/js/form/components/group</item>
<item name="label" xsi:type="string" translate="true">Testing Group</item>
<item name="required" xsi:type="boolean">false</item>
<item name="sortOrder" xsi:type="number">220</item>
<item name="breakLine" xsi:type="boolean">false</item>
<item name="scopeLabel" xsi:type="string">[STORE VIEW]</item>
</item>
</argument>
<field name="testone">
<argument name="data" xsi:type="array">
<item name="options" xsi:type="object">Vendor\Module\Model\Config\Source\TimeSetup</item>
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Testing Row</item>
<item name="sortOrder" xsi:type="number">230</item>
<item name="dataType" xsi:type="string">string</item>
<item name="formElement" xsi:type="string">select</item>
</item>
</argument>
</field>
<field name="testtwo">
<argument name="data" xsi:type="array">
<item name="options" xsi:type="object">Vendor\Module\Model\Config\Source\TimeSetup</item>
<item name="config" xsi:type="array">
<item name="sortOrder" xsi:type="number">240</item>
<item name="dataType" xsi:type="string">string</item>
<item name="formElement" xsi:type="string">select</item>
</item>
</argument>
</field>
</container>
</fieldset>
</form>
Using above code, Data saving properly, Only issues I'm facing now is that These attributes are appearing in General section as well in my created custom section which is "Testing Group" so as of now I've hide them from General section using CSS.
Best Answer
Add new field in UI form
Now create
Myvalues.php
to provide multiselect options at