Magento2: Сохраняйте таблицу конфигурации с несколькими полями


Как я могу получить в Store -> Configuration многополевые таблицы, значения которых сериализуются?

Author: St3phan, 2017-10-25

1 answers

Решение можно найти в модуле по этой ссылке на Github.

Ниже вы можете увидеть, как создается это требование:

Необходимо создать следующие VendorName/SysConfigTable папки в /app/code/.

Вы должны создать файл registration.php в /app/code/VendorName/SysConfigTable

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'VendorName_SysConfigTable',
    __DIR__
);

Вы должны создать файл module.xml в /app/code/VendorName/SysConfigTable/etc

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="VendorName_SysConfigTable" setup_version="0.0.1" />
</config>

Вы должны создать файл composer.json в /app/code/VendorName/SysConfigTable

{
    "name": "vendorname/sysconfigtable",
    "description": "",
    "require": {
        "php": "~5.5.0|~5.6.0|~7.0.0",
        
        "magento/magento-composer-installer": "*"
    },
    "suggest": {

    },
    "type": "magento2-module",
    "version": "0.0.1",
    "license": [

    ],
    "autoload": {
        "files": [
            "registration.php"
        ],
        "psr-4": {
            "VendorName\\SysConfigTable\\": ""
        }
    },
    "extra": {
        "map": [
            [
                "*",
                "VendorName/SysConfigTable"
            ]
        ]
    }
}

Вы должны создать файл system.xml в /app/code/VendorName/SysConfigTable/etc/adminhtml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <tab id="vendorname_general" translate="label" sortOrder="0">
            <label>VendorName</label>
        </tab>
        <section id="vendorname_general_section" translate="label" type="text" sortOrder="0" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Your Multiple fields</label>
            <tab>vendorname_general</tab>
            <resource>VendorName_Test::vendorname_test_config</resource>
            <group id="general" translate="label"  sortOrder="0" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>General</label>
                <field id="active" translate="label" sortOrder="0" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Your Fields</label>
                    <frontend_model>VendorName\SysConfigTable\Block\System\Config\Form\Field\Fields</frontend_model>
                    <backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
                </field>
            </group>
        </section>
    </system>
</config>

Вы должны создать файл Fields.php в /app/code/VendorName\SysConfigTable\Block\System\Config\Form\Field

<?php   
namespace VendorName\SysConfigTable\Block\System\Config\Form\Field;

use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;

/**
 * Class Active
 *
 * @package VendorName\SysConfigTable\Block\System\Config\Form\Field
 */
class Fields extends AbstractFieldArray {

    /**
     * @var bool
     */
    protected $_addAfter = TRUE;

    /**
     * @var
     */
    protected $_addButtonLabel;

    /**
     * Construct
     */
    protected function _construct() {
        parent::_construct();
        $this->_addButtonLabel = __('Add');
    }

    /**
     * Prepare to render the columns
     */
    protected function _prepareToRender() {
        $this->addColumn('field_1', ['label' => __('Field 1')]);
        $this->addColumn('field_2', ['label' => __('Field 2')]);
        $this->addColumn('field_3', ['label' => __('Field 3')]);
        $this->addColumn('field_4', ['label' => __('Field 4')]);
        $this->_addAfter       = FALSE;
        $this->_addButtonLabel = __('Add');
    }
}
 12
Author: St3phan, 2020-06-15 08:30:17