Hướng dẫn tạo system.xml và sử dụng Helper trong Magento 2

Ở bài viết trước mình đã hướng dẫn các bạn cách tạo ra menu cho module, hôm nay mình sẽ hướng dẫn các bạn cách tạo system.xml và sử dụng Helper trong Magento 2.

Với system.xml thì nó giúp các bạn có thể config được các chức năng của module, vd: Bật/tắt module v.v

Còn đối với Helper nó chứa các phương thức giúp chúng ta có thể thực hiện các tác vụ trên các đối tượng và biến trong Magento.

Tạo system.xml

Các bạn tạo file system.xml theo đường dẫn app/code/Magetop/Helloworld/etc/adminhtml/ với đoạn code sau:

<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
 * system
 *
 * @copyright Copyright © 2020 MAGETOP. All rights reserved.
 * @author    [email protected]
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <tab id="magetop" translate="label" sortOrder="10">
            <label>Magetop</label>
        </tab>
        <section id="helloworld" translate="label" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Configuration Helloworld</label>
            <tab>magetop</tab>
            <resource>Magetop_Helloworld::system_config</resource>
            <group id="setting" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>General Setting</label>
                <field id="enable" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                    <label>Enable</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="text" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="0" showInStore="0">
                    <label>Text</label>
                    <comment>Nhập đoạn text sẽ được hiển thị</comment>
                </field>
            </group>
        </section>
    </system>
</config>

Các bạn cần lưu ý đặt id section trùng với action=”adminhtml/system_config/edit/section/helloworld” trong mục menu mà mình đã hướng dẫn ở bài trước để khi chúng ta click vào menu nó sẽ truy cập đến đúng phần section config. Sau khi lưu code lại các bạn tiến hành clean cache.

Giờ chúng ta vào admin chọn Module -> Module Configuration.

Module Configuration

Nó đã đưa chúng ta đến đúng system vừa tạo ở trên.

cấu tạo system

Hình ảnh trên mình đã chỉ ra các thuộc tính trong đoạn code của system.xml để các bạn dễ hình dung hơn.

Tạo Helper

Để lấy dữ liệu từ Field Text rồi hiển thị nó nên 1 trang mới chúng ta sẽ sử dụng Helper

Tạo file app/code/Magetop/Helloworld/Helper/Data.php với đoạn code:

<?php
namespace Magetop\Helloworld\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Store\Model\ScopeInterface;

class Data extends AbstractHelper
{
    public function getText($text){
        return $this->scopeConfig->getValue('helloworld/setting/'.$text, ScopeInterface::SCOPE_STORE);
    }
}

Ở đây chúng ta đã tạo ra 1 function có tên là getText truyền vào $text để lấy giá trị từ field Text với đường dẫn <id section>/<id group>/<id field>.

Tiếp theo tạo file Config.php trong app/code/Magetop/Helloworld/Controller/Index với đoạn code:

<?php
namespace Magetop\Helloworld\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magetop\Helloworld\Helper\Data;

class Config extends Action
{

    protected $helper;

    public function __construct(Context $context, Data $helper)
    {
        $this->helper = $helper;
        parent::__construct($context);
    }


    public function execute()
    {
        echo $this->helper->getText('text');
    }
}

Tạo xong các bạn clean cache, điền text vào field và lưu lại.

điền field text

Sau đó truy cập vào đường dẫn domain/routes/index/config.

hiển thị text

Dòng chữ mình ghi trong field đã được hiển thị ra 1 trang.

Vậy là mình đã hướng dẫn xong cách tạo system.xml và sử dụng Helper trong Magento 2.

Cảm ơn các bạn đã đọc bài viết.

4.5 2 votes
Article Rating

Callula Huy

Callula is the Marketing Executive at Magetop. With more than 5 years of copywriting under her belt, Callula is into creating valuable content that is straight to the point. Life student. Workaholic. Foreign languages and traveling aficionado.

Leave a Reply or put your Question here

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x