<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发
》</a></h5>
<div class="image-container">
<p>
<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">
<img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发">
</a>
</p>
</div>
<div class="text-container" style="font-size:14px; color:#888">
<p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p>
</div>
<hr><p>如何在Magento 2中添加动态系统配置字段:</p><p>步骤 1:在以下路径中创建一个示例系统.xml:</p><p>app/code/Vendor/Extension/etc/adminhtml/system.xml</p><p>然后,添加以下代码:</p><pre class="brush:bash;toolbar:false"><?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../Magento/Backend/etc/system_file.xsd">
<system>
<tab id="vendortab" translate="label" sortOrder="100">
<label>Vendor TAB</label>
</tab>
<section id="sectionid" translate="label" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<class>separator-top</class>
<label>General Config</label>
<tab>vendortab</tab>
<resource>Vendor_Extension::config</resource>
<group id="group" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
<label>General Configuration</label>
<field id="field" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1"
showInStore="1">
<label>Field</label>
</field>
</group>
</section>
</system>
</config></pre><p>步骤2:为此,请在以下路径中创建di.xml:</p><p>app\code\Vendor\Extension\etc\adminhtml\di.xml</p><p>现在,添加以下代码:</p><pre class="brush:bash;toolbar:false"><?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Config\Model\Config\Structure\Data">
<plugin name="vendor_dynamic" type="Vendor\Extension\Plugin\Config\Field\Data"/>
</type>
</config><span style="font-weight: 400;"> </span></pre><p>步骤3: 接下来,在插件中创建一个名为 Data.php 文件的插件:</p><p>app\code\Vendor\Extension\Plugin\Config\Field\Data.php</p><p>并将以下代码添加到文件中:</p><pre class="brush:bash;toolbar:false"><?php
namespace Vendor\Extension\Plugin\Config\Field;
use Magento\Config\Model\Config\Structure\Data as StructureData;
use Magento\Framework\Module\ModuleListInterface;
class Data
{
public function __construct(ModuleListInterface $moduleList)
{
$this->_moduleList = $moduleList;
}
public function beforeMerge(StructureData $object, array $config)
{
$moduleList = $this->_moduleList->getNames();
foreach ($moduleList as $name)
{
if (strpos($name, 'Vendor_Extension') === false)
{
continue;
}
$this->moduleslist[] = $name;
}
if (!isset($config['config']['system']))
{
return [$config];
}
$sections = $config['config']['system']['sections'];
foreach ($sections as $sectionId => $section)
{
if (isset($section['tab']) && ($section['tab'] === 'vendor') && ($section['id'] !== 'vendor'))
{
foreach ($this->moduleslist as $moduleName)
{
if ($section['id'] !== 'sectionid')
{
continue;
}
$dynamicGroups = $this->getGroups($moduleName, $section['id']);
if (!empty($dynamicGroups))
{
$config['config']['system']['sections'][$sectionId]['children'] = $dynamicGroups + $section['children'];
}
break;
}
}
}
return [$config];
}
protected function getGroups($moduleName, $sectionName)
{
$defaultFieldOptions = [
'type' => 'text',
'showInDefault' => '1',
'showInWebsite' => '1',
'showInStore' => '1',
'sortOrder' => 1,
'module_name' => $moduleName,
'validate' => 'required-entry',
'_elementType' => 'field',
'path' => $sectionName . '/module'
];
$fields = [];
foreach ($this->getNewField() as $id => $option)
{
$fields[$id] = array_merge($defaultFieldOptions, ['id' => $id], $option);
}
return [
'module' => [
'id' => 'module',
'label' => __('Dynamic Section'),
'showInDefault' => '1',
'showInWebsite' => '0',
'showInStore' => '0',
'_elementType' => 'group',
'path' => $sectionName,
'children' => $fields
]
];
}
protected function getNewField()
{
return [
'name1' => [
'label' => __('Dynamic Field'),
'frontend_class' => 'vendor_dynamic_class',
'show' => 1,
'tooltip' => __('Dynamic Field Tooltip'),
'comment' => __('Dynamic Field Comment')
],
];
}
}</pre><p>您可以从管理面板检查输出。</p><p>结语:</p><p>这样,您可以在Magento 2中添加动态系统配置字段。</p><p><br/></p>