在Magento2 XML类型输入中附加值

mnemlml8  于 2023-01-17  发布在  其他
关注(0)|答案(1)|浏览(135)

我尝试在UI表单中添加xml类型的值,但我发现方法不成功。
例如:这是我的密码

<field name="client_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">ClientId</item>
                    <item name="value" xsi:type="string" translate="true">Module\Core\DataObjects</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">smartbusiness</item>
                    <item name="dataScope" xsi:type="string">client_id</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                        <item name="min_text_length" xsi:type="number">10</item>
                        <item name="max_text_length" xsi:type="number">250</item>
                        <item name="no-whitespace" xsi:type="boolean">true</item>
                    </item>
                </item>
            </argument>
        </field>

在我的自定义类中

<?php

namespace Module\Core\DataObjects;

use Module\Core\Model\SettingsFactory;
use Magento\Framework\App\ObjectManager;

class SettingsData
{
    protected array $setting = [];

    public function __construct()
    {
        $objectManager = ObjectManager::getInstance();
        $modelFactory = $objectManager->get(SettingsFactory::class);
        $collection = $modelFactory->create()->getCollection();
        $collection->getSelect()->limit(1);

        if ($collection->getSize()) {
            $this->setting = $collection->getData()[0];
        }
    }

    public function getData()
    {
        return $this->getClientId();
    }
}

有什么想法吗?我也试过将class ="Name of my class"赋值,但没有成功
我尝试了上面的方法+在class属性中附加类名

ygya80vv

ygya80vv1#

步骤1:-在视图中创建用户界面表单/adminhtml/用户界面组件/sunarc演示类型表单. xml

<?xml version="1.0" encoding="UTF-8"?>

<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/Ui/etc/ui_configuration.xsd">
 <!--main part of the grid-->
    <argument name="data" xsi:type="array">
            <item name="js_config" xsi:type="array">
            <!--Define where to find the data sources-->
                <item name="provider" xsi:type="string">sunarc_demo_type_form.sunarc_demo_type_form_data_source</item>
                <item name="deps" xsi:type="string">sunarc_demo_type_form.sunarc_demo_type_form_data_source</item>
            </item>
            <item name="label" xsi:type="string" translate="true">Add Dummy Data</item>
            <item name="layout" xsi:type="array">
                <item name="type" xsi:type="string">tabs</item>
            </item>
                <!-- <item name="buttons" xsi:type="array">
                <item name="back" xsi:type="string">Sunarc\Enquiry\Block\Adminhtml\Type\Edit\BackButton</item>
                <item name="reset" xsi:type="string">Sunarc\Enquiry\Block\Adminhtml\Type\Edit\ResetButton</item>
                <item name="save" xsi:type="string">Sunarc\Enquiry\Block\Adminhtml\Type\Edit\SaveButton</item> -->
            <!-- </item> -->
    </argument> 
    
    
    <dataSource name="sunarc_demo_type_form_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">Sunarc\Demo\Model\Ui\DataProvider</argument>
            <argument name="name" xsi:type="string">sunarc_demo_type_form_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">entity_id</argument>
            <argument name="requestFieldName" xsi:type="string">id</argument>
          </argument>

        <argument name="data" xsi:type="array">
                <item name="js_config" xsi:type="array">
                    <item name="component" xsi:type="string">Magento_Ui/js/form/provider</item>
                    <item name="submit_url" xsi:type="url" path="*/*/save"/>
                </item>
        </argument>
      
    </dataSource>

    <fieldset name="demo">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Add/Edit Type</item>
            </item>
        </argument>

        <!-- This field represents form id and is hidden -->
        <field name="entity_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="visible" xsi:type="boolean">false</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">type</item>
                </item>
            </argument>
        </field>

    
        <field name="demo_data">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Demo Data</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">type</item>
                </item>
            </argument>
            <settings>
                <validation>
                    <rule name="required-entry" xsi:type="boolean">true</rule>
                </validation>
            </settings>
        </field>

    <!-- IF YOU WANT TO SHOW STATIC VALUE  -->
        <field name="text_example" formElement="input">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="default" xsi:type="string">SAMPLE TEXT HERE</item>
            </item>
        </argument>
        <settings>
            <label translate="true">SAMPLE TEXT</label>
            <visible>true</visible>
            <disabled>false</disabled>
            <elementTmpl>ui/form/element/text</elementTmpl>
        </settings>
    </field>

    </fieldset>

</form>

步骤2:-在Model/UI/DataProvider.php中创建数据源文件

<?php

namespace Sunarc\Demo\Model\Ui;

use Sunarc\Demo\Model\ResourceModel\Demo\CollectionFactory;

class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{

    protected $loadedData;
    /**
     * @param string $name
     * @param string $primaryFieldName
     * @param string $requestFieldName
     * @param CollectionFactory $typeCollectionFactory
     * @param array $meta
     * @param array $data
     */
    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        CollectionFactory $typeCollectionFactory,
        array $meta = [],
        array $data = []
    ) {
        $this->collection = $typeCollectionFactory->create();
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
    }

    /**
     * Get data
     *
     * @return array
     */
    public function getData()
    {

        if (isset($this->loadedData)) {
            return $this->loadedData;
        }

        $items = $this->collection->getItems();

        foreach ($items as $item) {
            $this->loadedData[$item->getData('entity_id')]['demo']['demo_data'] = $item->getData('demo_data');
        }

        return $this->loadedData;
        // return [];
    }
}

Click Here to check the input

相关问题