On the Product edit form in the Admin area, I have added a multiselect field by overwriting the modifyMeta()
method on MagentoCatalogUiDataProviderProductFormModifierAbstractModifier
as so:
<?php
namespace DotancohenFoobarUiDataProviderProductFormModifier;
use MagentoCatalogUiDataProviderProductFormModifierAbstractModifier;
use MagentoUiComponentFormElementInput;
use MagentoUiComponentFormElementSelect;
use MagentoUiComponentFormFieldset;
use MagentoUiComponentFormElementDataTypeText;
use MagentoUiComponentFormField;
use MagentoUiComponentFormElementMultiSelect;
class Features extends AbstractModifier {
public function modifyMeta(array $meta)
{
$meta_extend = [
'dc-product-foobar' => [
'arguments' => [
'data' => [
'config' => [
'label' => __('Foobar'),
'collapsible' => true,
'componentType' => Fieldset::NAME,
'dataScope' => 'data.dc-product-foobar',
'sortOrder' => 10,
],
],
],
'children' => [
'testmultiselect' => [
'arguments' => [
'data' => [
'config' => [
'label' => __('Multiselect Element'),
'componentType' => Field::NAME,
'formElement' => MultiSelect::NAME,
'dataScope' => 'testmultiselect',
'dataType' => Text::NAME,
'sortOrder' => 10,
'options' => [
['value' => '0', 'label' => __('One')],
['value' => '1', 'label' => __('Two')],
['value' => '2', 'label' => __('Three')],
['value' => '3', 'label' => __('Four')],
],
],
],
],
],
],
],
];
$meta = array_replace_recursive($meta, $meta_extend);
return $meta;
}
}
This code does properly add the Multiselect field. How can I now set specific values as being selected? The data is coming from an outside process, not the database, so I need to set the values here in PHP.
I’ve tried the obvious adding of elements 'selected' => 'selected'
and 'selected' => true
to the relevant values’ arrays, like so:
'options' => [
['value' => '0', 'label' => __('One'), 'selected' => 'selected'],
['value' => '1', 'label' => __('Two'), 'selected' => true],
['value' => '2', 'label' => __('Three')],
['value' => '3', 'label' => __('Four')],
],
Neither approach worked. Then I tried to add some JavaScript that might select the elements, as suggested here:
// Javascript on the fieldset itself
'config' => [
'after_element_html' => '<script>require([], function(){console.log("Hello, fieldset!");});</script>',
'label' => __('Features'),
'collapsible' => true,
'componentType' => Fieldset::NAME,
'dataScope' => 'data.dc-product-foobar',
'sortOrder' => 10,
],
// Javascript on the Multiselect element
'config' => [
'after_element_html' => '<script>require([], function(){console.log("Hello, Multiselect!");});</script>',
'label' => __('Multiselect Element'),
'componentType' => Field::NAME,
'formElement' => MultiSelect::NAME,
'dataScope' => 'testmultiselect',
'dataType' => Text::NAME,
'sortOrder' => 10,
'options' => [
['value' => '0', 'label' => __('One')],
['value' => '1', 'label' => __('Two')],
['value' => '2', 'label' => __('Three')],
['value' => '3', 'label' => __('Four')],
],
],
However the JavaScript is not being invoked. I do see that that the two after_element_html
values are appearing in some escaped JSON string in the HTML, but the <script>
tag itself is not in the DOM.
How can I either assert that specific options’ values are to be selected in the PHP array, or how can I get that JavaScript invoked so that I could select them using JavaScript?