1

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 are getting that error because Magento\Store\Ui\Component\Listing\Column\Store is supposed to be used for Listing component. So the easiest way to resolve it is to create your own option source that fetches the stores in your website. check below answer. – fmsthird May 21 at 3:50
1

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>

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.