سؤال

I have a form like this. This is used to save a new custom admin. But I have a problem with showing a value in the store_id field while editing. form for editing

While showing this form the store field must be selected. The name of this field is store_id but I have to show the name of the store instead. so I need to make a class for getting the store's name. I can get all the names of the stores and use them for the dropdown list. But after seeing the detail when editing, the store_id field is empty and doesn't even show the store_id or the store_name. However, the store_id can be saved to the database.

This is the part for the store_id field at admin_form.xml

<field name="store_id">
    <argument name="data" xsi:type="array">
        <item name="options" xsi:type="object">Module\Vendor\Model\Select\Source\Store</item>
        <item name="config" xsi:type="array">
            <item name="componentType" xsi:type="string">field</item>
            <item name="formElement" xsi:type="string">select</item>
            <item name="label" xsi:type="string">Store</item>
            <item name="visible" xsi:type="boolean">true</item>
            <item name="dataType" xsi:type="string">text</item>
            <item name="source" xsi:type="string">Customer</item>
            <item name="dataScope" xsi:type="string">store_id</item>
        </item>
    </argument>
</field>

and this is the class for showing the name of the store

<?php

class Store extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{

    /**
     * @var \Vendor\Module\Model\CustomFactory
     */
    private $customFactory;

    /**
     * @param \Magento\Customer\Model\CustomerFactory $customerFactory
     */
    public function __construct(\Module\Vendor\Model\ResourceModel\Store\CollectionFactory $customFactory)
    {
        $this->customFactory = $customFactory;
    }

    /**
     * Get all options
     *
     * @return array
     */
    public function getAllOptions()
    {
        $customCollection = $this->customFactory->create();

        $customCollection->addFieldToFilter('status', 'Active');

        $customCollection->setOrder('store_name', 'ASC');

        $this->_options = [['label' => 'Please select', 'value' => '']];

        foreach ($customCollection as $custom) {
            $this->_options[] = ['label' => $custom->getData('store_name'), 'value' => $custom->getData('id')];
        }
        return $this->_options;
    }

     
}

How can I make this field is selected based on the store_id and the selected field shows the store's name?

هل كانت مفيدة؟

المحلول

The problem is because store_ids are not from same table as your custom model, so it doesn't get loaded, you need to set in manually, in either _afterLoad of your model or DataProvider class to set it in array with same array index as the field name in your form and you're good to go.

نصائح أخرى

The problem is in your Store class, it must be implemented from Magento\Framework\Data\OptionSourceInterface.

Then replace the getAllOptions function with toOptionArray, the content of the new function function is

public function toOptionArray(): array
    {
        $result = [];

        $customCollection = $this->customFactory->create();
        $customCollection->addFieldToFilter('status', 'Active');
        $customCollection->setOrder('store_name', 'ASC');

        $result[] = [
            'value' => '',
            'label' => 'Please select'
        ];
        
        foreach ($customCollection as $custom) {
            $result[] = [
                'value' => $custom->getData('id'),
                'label' => $custom->getData('store_name')
            ];
        }


        return $result;
    }

And you must make sure the column name in your database and the field name in your UI component file is the same store_id .

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top