Magento2 エクステンションの作成 その5
Rev.5を表示中。最新版はこちら。
1. 概要
ここでは、新規登録ページと編集ページの表示を実装する(保存処理は次回)。
2. テンプレートの作成
まず、編集ページのlayoutファイルを作成する。layoutファイルの命名規則はMagento2 エクステンションの作成 その2でも説明したが、
./view/{area}/layout/{route_id}_{controller_name}_{action_name}.xml
のようなルールになっている。編集ページのURLはtopic/posts/edit/xxxxなので、view/adminhtml/layout/topic_posts_edit.xmlというレイアウトファイルを作成する。
view/adminhtml/layout/topic_posts_edit.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<title>
Posts
</title>
</head>
<body>
<referenceContainer name="content">
<!-- -view/adminhtml/ui_component/topic_edit_form.xmlを参照 -->
<uiComponent name="topic_edit_form"/>
</referenceContainer>
</body>
</page>
編集ページでは入力フォームを表示するが、これもGridと同じようにUI componentsを使って表示する。上記のレイアウトファイルでは、<uiComponent name="topic_edit_form"/>として、view/adminhtml/layout/topic_posts_edit.xmlを読み込んでいる。
次にview/adminhtml/layout/topic_posts_edit.xmlを作成して、フォームの内容を定義する。
view/adminhtml/ui_component/topic_edit_form.xml
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<argument name="data" xsi:type="array">
<item name="js_config" xsi:type="array">
<item name="provider" xsi:type="string">topic_edit_form.topic_edit_form_data_source</item>
</item>
<item name="label" xsi:type="string" translate="true">Topic Information</item>
<item name="template" xsi:type="string">templates/form/collapsible</item>
</argument>
<settings>
<!-- 保存ボタン、戻るボタンの定義 -->
<buttons>
<button name="save" class="BitHive\Topic\Block\Adminhtml\Edit\SaveButton"/>
<button name="back" class="BitHive\Topic\Block\Adminhtml\Edit\BackButton"/>
</buttons>
<namespace>topic_edit_form</namespace>
<dataScope>data</dataScope>
<deps>
<dep>topic_edit_form.topic_edit_form_data_source</dep>
</deps>
</settings>
<!-- Modelにデータを読み込むためのDataProviderの定義 -->
<dataSource name="topic_edit_form_data_source" component="Magento_Ui/js/form/provider">
<settings>
<submitUrl path="topic/posts/save"/>
</settings>
<dataProvider class="BitHive\Topic\Model\DataProvider" name="topic_edit_form_data_source">
<settings>
<requestFieldName>id</requestFieldName>
<primaryFieldName>post_id</primaryFieldName>
</settings>
</dataProvider>
</dataSource>
<!-- フォームに表示する要素の定義 -->
<fieldset name="post_details">
<settings>
<collapsible>false</collapsible>
<label translate="true">Post Details</label>
</settings>
<field name="message" formElement="input">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">posts</item>
</item>
</argument>
<settings>
<validation>
<rule name="required-entry" xsi:type="boolean">true</rule>
<rule name="max_text_length" xsi:type="number">255</rule>
</validation>
<dataType>text</dataType>
<dataScope>message</dataScope>
<label translate="true">Message</label>
</settings>
</field>
<field name="date" formElement="input">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">posts</item>
</item>
</argument>
<settings>
<dataType>datetime</dataType>
<dataScope>date</dataScope>
<label translate="true">Date</label>
</settings>
</field>
</fieldset>
</form>
3. コントローラーの作成
2.で作成したテンプレートを表示するコントローラーと関連する以下のファイルを作成する。
(1) Controller/Adminhtml/Posts/Edit.php
編集ページ用のコントローラー。
(2) Controller/Adminhtml/Posts/NewAction.php
新規登録ページ用のコントローラー。
(3) Model/DataProvider.php
Modelにデータを読み込むためのDataProvider。topic_edit_form.xmlの<dataProvider>で指定したクラス。
(4) Helper/Timezone.php
Timezoneの変換を行うヘルパークラス。
(5) Topic/Block/Adminhtml/Edit/GenericButton.php,SaveButton.php,BackButton.php
topic_edit_form.xmlで定義した保存ボタン、戻るボタンを実装するクラス。
まずは編集用ページのコントローラーであるController/Adminhtml/Posts/Edit.phpを作成する。
Controller/Adminhtml/Posts/Edit.php
<?php
namespace BitHive\Topic\Controller\Adminhtml\Posts;
class Edit extends \Magento\Backend\App\Action
{
protected $pageFactory;
protected $postFactory;
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $pageFactory,
\BitHive\Topic\Model\PostFactory $postFactory)
{
$this->pageFactory = $pageFactory;
$this->postFactory = $postFactory;
return parent::__construct($context);
}
public function execute()
{
$postId = $this->getRequest()->getParam('id');
$model = $this->postFactory->create();
if ($postId) {
$model->load($postId);
if (!$model->getId()) {
$this->messageManager->addError(__('This post no longer exists.'));
$this->_redirect('*/*/');
return;
}
}
$resultPage = $this->pageFactory->create();
$resultPage->getConfig()->getTitle()->prepend($postId ? __('Edit') : __('New'));
return $resultPage;
}
}
idパラメータからPostモデルを読み込みテンプレートを表示しているだけ。このコントローラーは新規登録時も使用するため、post_idが未指定のケースもある。post_idが未指定ならモデルの読み込みは行わずに、そのままテンプレートを表示する。
なお、モデルを生成しているPostFactoryはModel名+Factoryで自動生成されるクラスなので実装する必要はない。
次に新規登録ページ用のコントローラーController/Adminhtml/Posts/NewAction.phpを作成する。
Controller/Adminhtml/Posts/NewAction.php
<?php
namespace BitHive\Topic\Controller\Adminhtml\Posts;
class NewAction extends \Magento\Backend\App\Action
{
protected $resultForwardFactory;
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory)
{
$this->resultForwardFactory = $resultForwardFactory;
return parent::__construct($context);
}
public function execute()
{
$resultForward = $this->resultForwardFactory->create();
return $resultForward->forward('edit');
}
}
これは、ResultForwardで'edit'に転送しているので、先程作成したEdit.phpを使用することになる。
編集中
