Frage

I am having problems with dynamic display store views which are grouped by their website and having selected values if I am editing existing data.

What I achieved, displaying store views, and saving them to the database as an array of ids in the TEXT column.

I am new at Magento, so excuse me if this question is not very clear.

class StoreViews implements \Magento\Framework\Data\OptionSourceInterface
{
    protected $storeManager;

    protected $options = [];


    public function __construct(\Magento\Store\Model\StoreManagerInterface $storeManager)
    {
        $this->storeManager = $storeManager;
    }


    public function toOptionArray()
    {
        $collection = $this->storeManager->getStores();
        foreach ($collection as $item){
            $this->options[] = array('label' => $item['code'], 'value' => (int)$item['store_id']);
        }

        return $this->options;
    }
}

Here is UI COMPONENT

<field name="store_views">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">Module\SubscriberSegment\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_id</item>
                    <item name="default" xsi:type="string">0</item>
                </item>
            </argument>
</field>

ADDING PICTURE FOR BETTER EXPLAINING THE PROBLEM

enter image description here

This multi-select store needs to be grouped by their website, and in case of a saved record, I need to multi-selected values that are saved to be selected

War es hilfreich?

Lösung

You can get the store views grouped by website by using Magento\Cms\Ui\Component\Listing\Column\Cms\Options as a source model to store_views ui component instead of >Module\SubscriberSegment\Ui\Component\Form\StoreViews.
To get values selected you need that your data to be an array for the element store_views.
So, in the class that retrieves the data, do this:

Let's say $data is what you got from the db.

if (isset($data['store_views'])) {
    $data['store_views'] = is_array($data['store_views']) ? $data['store_views'] : explode(',', $data['store_views']);
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit magento.stackexchange
scroll top