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>

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
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.