浏览文章
文章信息
Maegnto2 后台创建UI form 布局layout XML| create a ui form component in adminhtml layout xml
13403
1、xml代码:
<?xml version="1.0" encoding="UTF-8"?> <!-- /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ --> <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd"> <argument name="data" xsi:type="array"> <item name="js_config" xsi:type="array"> <item name="provider" xsi:type="string">upsell_rule_form.rule_form_data_source</item> <item name="deps" xsi:type="string">upsell_rule_form.rule_form_data_source</item> </item> <item name="layout" xsi:type="array"> <item name="type" xsi:type="string">tabs</item> </item> <item name="buttons" xsi:type="array"> <item name="save" xsi:type="array"> <item name="name" xsi:type="string">save</item> <item name="label" xsi:type="string" translate="true">保存 Rule</item> <item name="class" xsi:type="string">primary</item> <item name="url" xsi:type="string">upsell/rule/save</item> </item> <item name="back" xsi:type="array"> <item name="name" xsi:type="string">back</item> <item name="label" xsi:type="string" translate="true">返回</item> <item name="class" xsi:type="string">back</item> <item name="url" xsi:type="string">upsell/rule/listing</item> </item> </item> </argument> <settings> <namespace>upsell_rule_form</namespace> </settings> <dataSource name="rule_form_data_source"> <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> </argument> <settings> <submitUrl path="upsell/rule/save"/> </settings> <dataProvider class="Aiweline\Upsell\Model\ResourceModel\UpsellRule\DataProvider" name="rule_form_data_source"> <settings> <requestFieldName>rule_id</requestFieldName> <primaryFieldName>rule_id</primaryFieldName> </settings> </dataProvider> </dataSource> <fieldset name="general"> <settings> <label translate="true">Rule信息</label> <collapsible>false</collapsible> </settings> <field name="rule_id" formElement="hidden"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="source" xsi:type="string">rule</item> <item name="label" xsi:type="string" translate="true">Rule ID</item> </item> </argument> <settings> <dataType>text</dataType> </settings> </field> <field name="name" formElement="input"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="source" xsi:type="string">rule</item> <item name="label" xsi:type="string" translate="true">Rule名称</item> </item> </argument> <settings> <validation> <rule name="required-entry" xsi:type="boolean">true</rule> </validation> <dataType>text</dataType> </settings> </field> <field name="is_active" formElement="checkbox"> <settings> <label translate="true">启用状态</label> <tooltip> <description translate="true">启用则生效,不启用则不生效!</description> </tooltip> <validation> <rule name="required-entry" xsi:type="boolean">true</rule> </validation> </settings> <formElements> <checkbox> <settings> <prefer>toggle</prefer> <valueMap> <map name="false" xsi:type="string">0</map> <map name="true" xsi:type="string">1</map> </valueMap> </settings> </checkbox> </formElements> </field> <field name="store_code" formElement="select"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="source" xsi:type="string">rule</item> <item name="dataType" xsi:type="string">text</item> <item name="visibleValue" xsi:type="string">1</item> </item> </argument> <settings> <dataType>text</dataType> <label translate="true">选择关联店铺</label> <validation> <rule name="required-entry" xsi:type="boolean">true</rule> </validation> </settings> <formElements> <select> <settings> <options class="Aiweline\Core\Model\M24\Config\Source\Stores"/> </settings> </select> </formElements> </field> <field name="type" formElement="select"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="source" xsi:type="string">rule</item> <item name="visibleValue" xsi:type="string">1</item> </item> </argument> <settings> <validation> <rule name="required-entry" xsi:type="boolean">true</rule> </validation> <dataType>text</dataType> <label translate="true">优惠类型</label> </settings> <formElements> <select> <settings> <options class="Aiweline\Upsell\Model\Config\Source\TypeOption"/> </settings> </select> </formElements> </field> <field name="value" formElement="input"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="source" xsi:type="string">rule</item> <item name="label" xsi:type="string" translate="true">值</item> </item> </argument> <settings> <validation> <rule name="required-entry" xsi:type="boolean">true</rule> <rule name="max_text_length" xsi:type="string">60</rule> </validation> <dataType>text</dataType> </settings> </field> </fieldset> </form>2、DataProvider
提示:这是Magento的xml编译时的数据提供器,这个的作用主要是修改提供器提供的字段信息使用的,很有用,也很重要。
<?php /** * 文件信息 * 作者:邹万才 * 網名:秋风雁飞(可以百度看看) * 網站:www.aiweline.com/bbs.aiweline.com * 工具:PhpStorm * 日期:2020-2-23 * 时间:下午 02:06 * 描述: */ namespace Aiweline\Upsell\Model\ResourceModel\UpsellRule; use Magento\Framework\Exception\NoSuchEntityException; use Aiweline\Upsell\Model\ResourceModel\UpsellRule\CollectionFactory as RuleCollectionFactory; class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider { protected $request; protected $loadedData; protected $storeManager; private \Aiweline\Upsell\Model\UpsellRuleFactory $ruleFactory; public function __construct( RuleCollectionFactory $ruleCollectionFactory, \Magento\Framework\App\RequestInterface $request, \Aiweline\Upsell\Model\UpsellRuleFactory $ruleFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, $name, $primaryFieldName, $requestFieldName, array $meta = [], array $data = [] ) { $this->collection = $ruleCollectionFactory->create(); $this->ruleFactory = $ruleFactory; $this->request = $request; $this->storeManager = $storeManager; parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data); } public function getData() { // 读取ID,检查是更新还是新增 $requestId = $this->request->getParam($this->requestFieldName); if (isset($this->loadedData)) { return $this->loadedData; } $rule = $this->ruleFactory->create(); // 如果是 update 操作 if ($requestId) { $rule->load($requestId); if (!$rule->getId()) { throw NoSuchEntityException::singleField('id', $requestId); } //设置UpsellRule预览图: $baseUrl = $this->storeManager->getStore()->getBaseUrl(); $ruleImage[] = ['url'=>$baseUrl . $rule->getData('rule')]; $rule->setData('rule',$ruleImage); // 设置分类回填 $ruleData = $rule->getData(); $this->loadedData[$requestId]['general'] = $ruleData; } else { // new 操作 $this->loadedData = []; } return $this->loadedData; } }