
system.xml は、Magento 2 System Configuration で設定フィールドを作成するために使用する設定ファイルです。モジュールに管理者が設定する必要のある設定がある場合に必要となります。このファイルがどのように見えるかは、Store -> Setting -> Configuration で確認できます。
ステップ1:System.xmlの作成
magento 2 のシステム設定ページは、論理的にいくつかの部分に分かれています。Tabs, Sections, Groups, Fields です。このことを理解するために、この画像をご覧ください。

それでは、シンプルなモジュールHello Worldのためのシンプルな構成を作り始めましょう。system.xmlはモジュールのetc/adminhtmlフォルダにあり、ベンダー “Mageplaza “のための新しいタブ、モジュール “Hello World “のための新しいセクション、いくつかのシンプルなフィールドを含むグループを作成します:enable moduleとtext。
ファイル:app/code/Mageplaza/HelloWorld/etc/adminhtml/system.xml
<?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="mageplaza" translate="label" sortOrder="10">
<label>Mageplaza</label>
</tab>
<section id="helloworld" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
<class>separator-top</class>
<label>Hello World</label>
<tab>mageplaza</tab>
<resource>Mageplaza_HelloWorld::helloworld_config</resource>
<group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
<label>General Configuration</label>
<field id="enable" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Module Enable</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="display_text" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Display Text</label>
<comment>This text will display on the frontend.</comment>
</field>
</group>
</section>
</system>
</config>
このコードでは、タブ、セクション、グループ、フィールドの作成方法を確認できます。それぞれの要素について、さらに詳しくご紹介します。
- Tab要素は、多くのセクションといくつかの主要な属性と子を持つことができます。
- Id属性は、このタブを識別するためのものです。
- sortOrder 属性は、このタブの位置を定義します。
- Translate 属性は、どのタイトルを翻訳する必要があるかを Magento に知らせます。
- Label 要素の子は、タブのタイトルとして表示されるテキストです。
- Section 要素は Tab 要素のように id、sortOrder、translate 属性を持ちます。他にもいくつかの属性(showInDefault、showInWebsite、showInStore)があり、この要素が各スコープに表示されるかどうかを決定します。スコープはここで変更できます。

セクションは、多くのグループやその他の子要素を持つことができます:
- Class:この値は、この要素のクラスとして追加されます。この要素をメイクアップしたい場合は、この値を指定する必要があります。
- Label: この要素のテキストタイトルです。
- Tab: これはタブ ID です。このタブ要素は、このセクションがどのタブに属しているかを Magento に知らせます。このセクションはそのタブの下に配置されます。
- Resource: 管理者ユーザーがこの設定にアクセスするために必要な ACL ルールを定義します。
- Group: この要素はセクションと同じように、多くのフィールドといくつかの属性を持ちます。
- Fields: このページのメインパスです。設定したいデータが保存されます。この要素では、type属性に注目します。この属性は、要素が表示される際の方法を定義します。この例では、selectとtextの2つのタイプのフィールドを作成します。それぞれのタイプで、フィールドの子要素を定義し、思い通りに動作するようにします。
例えば、select/multiselectタイプでは、子要素source_modelを定義する必要があります。
ステップ2:デフォルト値の設定
作成後のsystem.xml内の各フィールドには値がありません。これらのフィールドを呼び出すと、「null」という結果が返ってきます。そこで、このモジュールでは、フィールドにデフォルト値を設定する必要があり、configにアクセスせずにその値を呼び出し、値を設定して保存します。このデフォルト値は、etcフォルダにあるconfig.xmlに保存されます。それでは、この簡単な設定のために、config.xmlを作成してみましょう。
ファイル:app/code/Mageplaza/HelloWorld/etc/config.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<helloworld>
<general>
<enable>1</enable>
<display_text>Hello World</display_text>
</general>
</helloworld>
</default>
</config>
要素にフィールドへのパスを入れることで、そのフィールドに値のデフォルトを設定することができます。フォーマットは:
<default>
<section>
<group>
<field>{value}</field>
</group>
</section>
</default>
ステップ3:Magentoのキャッシュをフラッシュすること
それでは、キャッシュを更新して、結果をご覧ください:

なお、最初に「Error 404 Page Not Found」が表示された場合は、一度ログアウトしてから再度ログインすると直ります。
ステップ4:コンフィグレーションからの値の取得
まず、値を保存してキャッシュをフラッシュし、データベースから保存した値を取得しましょう。
system.xmlでは、enableとdisplay_textの2つのフィールドを追加しています。そのため、パスは次のようになります。
- helloworld/general/enable
- helloworld/general/display_text
4.1 簡単な呼び出し
$this->scopeConfig->getValue('helloworld/general/enable', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
$this->scopeConfig->getValue('helloworld/general/display_text', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
4.2 ヘルパーファイルの作成(標準)
作成ファイル:app/code/Mageplaza/HelloWorld/Helper/Data.php
<?php
namespace Mageplaza\HelloWorld\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Store\Model\ScopeInterface;
class Data extends AbstractHelper
{
const XML_PATH_HELLOWORLD = 'helloworld/';
public function getConfigValue($field, $storeId = null)
{
return $this->scopeConfig->getValue(
$field, ScopeInterface::SCOPE_STORE, $storeId
);
}
public function getGeneralConfig($code, $storeId = null)
{
return $this->getConfigValue(self::XML_PATH_HELLOWORLD .'general/'. $code, $storeId);
}
}
<?php
namespace Mageplaza\HelloWorld\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Store\Model\ScopeInterface;
class Data extends AbstractHelper
{
const XML_PATH_HELLOWORLD = 'helloworld/';
public function getConfigValue($field, $storeId = null)
{
return $this->scopeConfig->getValue(
$field, ScopeInterface::SCOPE_STORE, $storeId
);
}
public function getGeneralConfig($code, $storeId = null)
{
return $this->getConfigValue(self::XML_PATH_HELLOWORLD .'general/'. $code, $storeId);
}
}
では、それをコントローラで実現してみましょう。
ファイル:app/code/Mageplaza/HelloWorld/Controller/Index/Config.php
<?php
namespace Mageplaza\HelloWorld\Controller\Index;
class Config extends \Magento\Framework\App\Action\Action
{
protected $helperData;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Mageplaza\HelloWorld\Helper\Data $helperData
)
{
$this->helperData = $helperData;
return parent::__construct($context);
}
public function execute()
{
// TODO: Implement execute() method.
echo $this->helperData->getGeneralConfig('enable');
echo $this->helperData->getGeneralConfig('display_text');
exit();
}
}
php bin/magento cache:cleanを実行してキャッシュをクリアし、結果を確認してください。
