Magento2 在系统配置中添加自定义条目

2020年6月9日 Edited 2022年12月29日 浏览量 36 4 min read
Magento2 在系统配置中添加自定义条目

在本篇博客中,我们将介绍如何在Stores->Configuration中的现有系统配置中添加自定义条目。

我们已经介绍了如何创建Magento2模块,所以我们将直接进入主题。将Magease/Test从本文中替换为您的模块名称。

 

在模块内的etc/adminhtml/system.xml中添加以下内容:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xs
    <system>
        <tab id="magease_custom_config" translate="label" sortOrder="100">
            <label>Magease Custom Config Page</label>
        </tab>
        <section id="magease_entry_1" translate="label" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1">
            <class>separator-top</class>
            <label>Magease Config SubItem</label>
            <tab>magease_custom_config</tab>
            <resource>Magease_Test::config</resource>
            <group id="general" translate="label" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>General Configuration</label>
                <comment>This is example configuration page.</comment>
                <field id="magease_text" translate="label" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                    <label>Text</label>
                    <comment>This is example comment.</comment>
                </field>
                <field id="magease_yesno" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
                    <label>Is Enabled</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="magease_dropdown_custom" translate="label" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Magease Custom Dropdown</label>
                    <source_model>Magease\Test\Model\Config\Custom</source_model>
                </field>
                <field id="magease_dropdown_dependable" translate="label" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Magease Custom Dependable Dropdown</label>
                    <depends>
                        <field id="*/*/magease_yesno">1</field>
                    </depends>
                    <source_model>Magease\Test\Model\Config\Custom</source_model>
                </field>
                <field id="magease_multiselect" translate="label" type="multiselect" sortOrder="40" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
                    <label>Multiselect</label>
                    <source_model>Magease\Test\Model\Config\Multiselect</source_model>
                </field>
            </group>
        </section>
    </system>
</config>

Text entry

第一个id=“magease_text”的字段是一个简单的文本字段,只需在system.xml中定义它就可以工作,并且不需要任何源模型。

Yesno entry

字段id=“magease_yesno”是来自Magento 1的经典是/否下拉列表。除了在system.xml中定义它之外,不需要额外的工作。

为了让某些自定义区域正常工作,我们必须定义system.xml中声明的源模型,虽然Magento会处理大部分工作,但我们必须为自定义输出覆盖标签或逻辑(如果需要的话)。

Custom dropdown

对于id="magease_dropdown_custom"的自定义下拉菜单,我们将添加自定义源模型Magease\Test\Model\Config\Custom:

<?php
namespace Magease\Test\Model\Config;
 
class Custom implements \Magento\Framework\Option\ArrayInterface
{
    /**
     * @return array
     */
    public function toOptionArray()
    {
        return [
            ['value' => 0, 'label' => __('First')],
            ['value' => 1, 'label' => __('Second')],
            ['value' => 2, 'label' => __('Third')],
            ['value' => 3, 'label' => __('Fourth')]
        ];
    }
}

Depends tag

下一个示例id=“magease_dropdown_dependable”与上一个相同,但因为我们在system.xml中添加了depends标记,所以它将严格依赖于magease_yesno这是system.xml的一部分:

<depends>
     <field id="*/*/magease_yesno">1</field>
</depends>

Custom Multiselect

为了使自定义multiselect id=“magease_multiselect”工作,我们首先必须定义源模型Magease\Test\Model\Config\Multiselect,如下:

<php
namespace Magease\Test\Model\Config;
 
class Multiselect extends \Magento\Captcha\Model\Config\Form\AbstractForm
{
    /**
     * @var string
     */
    protected $_configPath = 'magease/multiselect';
 
    /**
     * Returns options for form multiselect
     *
     * @return array
     */
    public function toOptionArray()
    {
        $optionArray = [];
        $backendConfig = $this->_config->getValue($this->_configPath, 'default');
        if ($backendConfig) {
            foreach ($backendConfig as $formName => $formConfig) {
                if (!empty($formConfig['label'])) {
                    $optionArray[] = ['label' => $formConfig['label'], 'value' => $formName];
                }
            }
        }
        return $optionArray;
    }
}

下一步是定义默认的多选值,在etc/config.xml添加以下表示默认值的条目,同时注意配置树是由前面Magease\Test\Model\ config\Multiselect中定义的受保护变量$_configPath中定义的字符串构成的:

<config>
    <default>
        <magease>
            <multiselect>
                <first_entry>
                    <label>First entry</label>
                </first_entry>
                <second_entry>
                    <label>Second entry</label>
                </second_entry>
            </multiselect>
        </magease>
    </default>
</config>

之后,确保刷新缓存和刷新。

Previous article:
Next article:
Comments
发表评论,留下你的足迹
我们不会公开你的邮箱地址

是否允许我们在发布新内容或者进行促销活动向您发送消息?

Remind me later

Thank you! Please check your email inbox to confirm.

Oops! Notifications are disabled.

© 2014-2023 www.magease.com. All Rights Reserved. 寰云网络 版权所有    鲁ICP备 14014975号-1