当前位置: 技术文章>> 如何在Magento 2中添加动态系统配置字段

文章标题:如何在Magento 2中添加动态系统配置字段
  • 文章分类: Magento
  • 10990 阅读
系统学习magento二次开发,推荐小册:《Magento中文全栈二次开发 》

本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。


如何在Magento 2中添加动态系统配置字段:

步骤 1:在以下路径中创建一个示例系统.xml:

app/code/Vendor/Extension/etc/adminhtml/system.xml

然后,添加以下代码:

<?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>

步骤2:为此,请在以下路径中创建di.xml:

app\code\Vendor\Extension\etc\adminhtml\di.xml

现在,添加以下代码:

<?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>

步骤3: 接下来,在插件中创建一个名为 Data.php 文件的插件:

app\code\Vendor\Extension\Plugin\Config\Field\Data.php

并将以下代码添加到文件中:

<?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')
            ],
        ];
    }
}

您可以从管理面板检查输出。

结语:

这样,您可以在Magento 2中添加动态系统配置字段。


推荐文章