0

I am new at Magento 2,

UI multi-select component render store views group by websites and stores,

but when I am editing I need saved values of store views of certain record to be preselected in that multi-select.

class StoreViews extends \Magento\Cms\Ui\Component\Listing\Column\Cms\Options
{
 /**
 * @var SegmentFactory $segment
 */
 protected $segmentFactory;
 /**
 * @var Segment $segmentModel
 */
 protected $segmentModel;
 /**
 * @var Http $reguest
 */
 protected $request;
 public function __construct(SystemStore $systemStore, Escaper $escaper, SegmentFactory $segmentFactory, Http $request, Segment $segmentModel)
 {
 parent::__construct($systemStore, $escaper);
 $this->segmentFactory = $segmentFactory;
 $this->segmentModel = $segmentModel;
 $this->request = $request;
 }
 public function toOptionArray()
 {
 $id = $this->request->getParams()['segment_id'];
 $model = $this->segmentModel;
 $segmentFactory = $this->segmentFactory->create();
 $segmentFactory->load($model, $id, 'segment_id');
 $currentValues = json_decode($model->getData()['store_views']);
 return $options;
 }
}

FORM

 <field name="store_views">
 <argument name="data" xsi:type="array">
 <item name="options" xsi:type="object">CodePool\CustomModule\Ui\Component\Form\StoreViews</item>
 <item name="config" xsi:type="array">
 <item name="dataType" xsi:type="string">int</item>
 <item name="label" xsi:type="string" translate="true">Store View</item>
 <item name="formElement" xsi:type="string">multiselect</item>
 <item name="source" xsi:type="string">page</item>
 <item name="dataScope" xsi:type="string">store_views</item>
 <item name="default" xsi:type="string">0</item>
 </item>
 </argument>
 </field>

asked Jan 21, 2019 at 13:46

1 Answer 1

0

try:

<item name="default" xsi:type="string">1,2,3</item>

or with form:

 $fieldset->addField(
 'countries',
 'multiselect',
 [
 'name' => 'countries',
 'label' => __('Select countries'),
 'id' => 'countries',
 'values' => $allCountries,
 'class' => 'status',
 ]
 );
 $model->setData("countries", [1,2,3]); // value in $allCountries not lable
answered Jan 24, 2019 at 10:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.