Вопрос

The goal is to get a drop down list of stores the site has. The current implementation is attempting to add the following into a ui_component xml file around line 100.

<field name="store_id">
        <argument name="data" xsi:type="array">
            <item name="options" xsi:type="object">Magento\Store\Ui\Component\Listing\Column\Store</item>
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Store</item>
                <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/select</item>
                <item name="dataType" xsi:type="string">text</item>
                <item name="formElement" xsi:type="string">select</item>
            </item>
        </argument>
    </field>

However, when visit the adminhtml section, I get an error message stating that now the type is not an array at line 57. Which happens to be the following:

  <fieldset name="general">
    <argument name="data" xsi:type="array">

Does anyone know the proper way to implement this or have similar examples they could point me to?

Error:

Warning: array_values() expects parameter 1 to be array, object given in WEB_ROOT/vendor/magento/module-ui/Component/Form/Element/AbstractOptionsField.php on line 57

Note: The error goes away when I remove the field store_id.

Это было полезно?

Решение

You should create your own option array that fetches the list of store your website has and then use it as an option in your xml form field.
First, create the option like this in the path:

app/code/Vendor/Module/Model/Config/Source/Stores.php

<?php

namespace Vendor\Module\Model\Config\Source;


class Stores implements \Magento\Framework\Option\ArrayInterface
{
    protected $storeManager;
    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager
    )
    {
        $this->storeManager = $storeManager;
    }
    public function toOptionArray()
    {
        $storeManagerDataList = $this->storeManager->getStores();
        $options = array();

        foreach ($storeManagerDataList as $key => $value) {
            $options[] = ['label' => $value['name'] . ' - ' . $value['code'], 'value' => $key];
        }
        return $options;
    }
}

And then in your xml form you can use it like below:

<field name="store_id">
    <argument name="data" xsi:type="array">
        <item name="options" xsi:type="object">Venodr\Module\Model\Config\Source\Stores</item>
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">Store</item>
            <item name="dataType" xsi:type="string">text</item>
            <item name="formElement" xsi:type="string">select</item>
            <item name="component" xsi:type="string">Magento_Ui/js/form/element/select</item>
            <item name="template" xsi:type="string">ui/form/field</item>
        </item>
    </argument>
</field>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с magento.stackexchange
scroll top