Labo

EC-CUBE3 分室

【3.0.13】管理画面の既存フォームへの項目追加

2017年01月29日 / 投稿者名:chiharu


今回は管理画面の既存フォームに項目を加えるための実験です。
実際には以前の【3-0-13】データベースへの入出力にて以下の既存フォームに対して項目の追加を行ったことはります。
 管理画面 > 設定 > 基本情報設定 > ショップマスター > 基本情報編集
 
しかし、該当の部分はちょっと特殊な動きをしているため簡単でしたが、通常のフォームは一工夫いるようです。
そのため、その一工夫に当たる部分を確認してみたいと思います。
 
なお、今回も基本はプラグインジェネレータでのベース作成を行います。
とりあえず受注管理画面を触って見たいのでとりあえずは以下のような感じです。

1---Entry confirmation
2[+]Plugin Name:  ナックウェブテスト008
3[+]Plugin Code:  NakwebTest008
4[+]Version:  0.0.1
5[+]Author:  NAKWEB
6[+]Old version support:  No
7[+]Site events:
8  eccube.event.route.admin_order_page.request
9  eccube.event.route.admin_order_page.controller
10  eccube.event.route.admin_order_page.response
11  eccube.event.route.admin_order_page.exception
12  eccube.event.route.admin_order_page.terminate
13[+]Hook points:
14  admin.order.edit.index.initialize
15  admin.order.edit.index.progress
16  admin.order.edit.index.complete
17 
18[confirm] Do you want to proceed? [y/n] :

今回はイベントを複数追加してみましたが想像よりは便利にはなりませんでした(笑)
それでは挑戦してみます。
 
 ・
 ・
 ・
 
とりあえず出来ました。
今回は既存フォームに twig の挿入を行う事をメインにしているため、データの登録処理などは省いています(笑)
 
■プラグインファイル
 event.yml
 NakwebTest008Event.php
 ServiceProvider/NakwebTest008ServiceProvider.php
 Event/OrderLegacy.php
 Resource/template/admin/nakwebtest008.twig
 
 ▼event.yml

1Admin/Order/edit.twig:
2    - [onRenderAdminOrderEdit, NORMAL]
3 
4eccube.event.route.admin_order_page.request:
5    - [onRouteAdminOrderPageRequest, NORMAL]
6eccube.event.route.admin_order_page.controller:
7    - [onRouteAdminOrderPageController, NORMAL]
8eccube.event.route.admin_order_page.response:
9    - [onRouteAdminOrderPageResponse, NORMAL]
10eccube.event.route.admin_order_page.exception:
11    - [onRouteAdminOrderPageException, NORMAL]
12eccube.event.route.admin_order_page.terminate:
13    - [onRouteAdminOrderPageTerminate, NORMAL]

 イベント情報の登録です。
 twig ファイルに関してはプラグインジェネレーターでは表示されないのか、または指定が間違えているようですね。
 とりあえず twig に関する記述を追加してみました。
 
 ▼NakwebTest008Event.php

1<?php
2 
3/*
4 * This file is part of the NakwebTest008
5 *
6 * Copyright (C) 2017 ナックウェブテスト008
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11 
12namespace Plugin\NakwebTest008;
13 
14use Eccube\Application;
15use Eccube\Event\EventArgs;
16use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
17use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
18use Symfony\Component\HttpKernel\Event\GetResponseEvent;
19use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
20use Symfony\Component\HttpKernel\Event\PostResponseEvent;
21 
22class NakwebTest008Event
23{
24 
25    /** @var  \Eccube\Application $app */
26    private $app;
27 
28    public function __construct(Application $app)
29    {
30        $this->app = $app;
31    }
32 
33    public function onRouteAdminOrderPageRequest(EventArgs $event)
34    {
35    }
36 
37    public function onRouteAdminOrderPageController(EventArgs $event)
38    {
39    }
40 
41    public function onRouteAdminOrderPageResponse(EventArgs $event)
42    {
43    }
44 
45    public function onRouteAdminOrderPageException(EventArgs $event)
46    {
47    }
48 
49    public function onRouteAdminOrderPageTerminate(EventArgs $event)
50    {
51    }
52 
53    public function onAdminOrderEditIndexInitialize(EventArgs $event)
54    {
55    }
56 
57    public function onAdminOrderEditIndexProgress(EventArgs $event)
58    {
59    }
60 
61    public function onAdminOrderEditIndexComplete(EventArgs $event)
62    {
63    }
64 
65 
66    public function onRenderAdminOrderEdit($event)
67    {
68        $nakwebTest008Event = $this->app['eccube.plugin.nakwebtest008.event.order_legacy'];
69        $nakwebTest008Event->changeTwig($event);
70 
71    }
72 
73 
74}

 こちらもイベント情報の登録です。
 特には問題はないかなと思います。
 
 ▼ServiceProvider/NakwebTest008ServiceProvider.php

1<?php
2 
3/*
4 * This file is part of the NakwebTest008
5 *
6 * Copyright (C) 2017 ナックウェブテスト008
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11 
12namespace Plugin\NakwebTest008\ServiceProvider;
13 
14use Eccube\Application;
15use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
16use Monolog\Handler\FingersCrossedHandler;
17use Monolog\Handler\RotatingFileHandler;
18use Monolog\Logger;
19use Plugin\NakwebTest008\Event\OrderLegacy;
20use Plugin\NakwebTest008\Form\Type\NakwebTest008ConfigType;
21use Silex\Application as BaseApplication;
22use Silex\ServiceProviderInterface;
23use Symfony\Component\Yaml\Yaml;
24 
25class NakwebTest008ServiceProvider implements ServiceProviderInterface
26{
27 
28    public function register(BaseApplication $app)
29    {
30        // プラグイン用設定画面
31        $app->match('/'.$app['config']['admin_route'].'/plugin/NakwebTest008/config', 'Plugin\NakwebTest008\Controller\ConfigController::index')->bind('plugin_NakwebTest008_config');
32 
33        // 独自コントローラ
34        $app->match('/plugin/[lower_code]/hello', 'Plugin\NakwebTest008\Controller\NakwebTest008Controller::index')->bind('plugin_NakwebTest008_hello');
35 
36 
37        // Order legacy event
38        $app['eccube.plugin.nakwebtest008.event.order_legacy'] = $app->share(function () use ($app) {
39            return new OrderLegacy($app);
40        });
41 
42 
43        // Form
44        $app['form.types'] = $app->share($app->extend('form.types', function ($types) use ($app) {
45            $types[] = new NakwebTest008ConfigType();
46 
47            return $types;
48        }));
49 
50        // Repository
51 
52        // Service
53 
54        // メッセージ登録
55        // $file = __DIR__ . '/../Resource/locale/message.' . $app['locale'] . '.yml';
56        // $app['translator']->addResource('yaml', $file, $app['locale']);
57 
58        // load config
59        // プラグイン独自の定数はconfig.ymlの「const」パラメータに対して定義し、$app['[lower_code]config']['定数名']で利用可能
60        // if (isset($app['config']['NakwebTest008']['const'])) {
61        //     $config = $app['config'];
62        //     $app['[lower_code]config'] = $app->share(function () use ($config) {
63        //         return $config['NakwebTest008']['const'];
64        //     });
65        // }
66 
67        // ログファイル設定
68        $app['monolog.logger.[lower_code]'] = $app->share(function ($app) {
69 
70            $logger = new $app['monolog.logger.class']('[lower_code]');
71 
72            $filename = $app['config']['root_dir'].'/app/log/[lower_code].log';
73            $RotateHandler = new RotatingFileHandler($filename, $app['config']['log']['max_files'], Logger::INFO);
74            $RotateHandler->setFilenameFormat(
75                '[lower_code]_{date}',
76                'Y-m-d'
77            );
78 
79            $logger->pushHandler(
80                new FingersCrossedHandler(
81                    $RotateHandler,
82                    new ErrorLevelActivationStrategy(Logger::ERROR),
83                    0,
84                    true,
85                    true,
86                    Logger::INFO
87                )
88            );
89 
90            return $logger;
91        });
92 
93    }
94 
95    public function boot(BaseApplication $app)
96    {
97    }
98 
99}

 こちらも twig を変更する際の記述を追加しています。
 
 ▼Event/OrderLegacy.php

1<?php
2 
3/*
4 * This file is part of the Maker plugin
5 *
6 * Copyright (C) 2016 LOCKON CO.,LTD. All Rights Reserved.
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11 
12namespace Plugin\NakwebTest008\Event;
13 
14use Eccube\Application;
15use Eccube\Common\Constant;
16use Symfony\Component\HttpFoundation\RedirectResponse;
17use Symfony\Component\HttpFoundation\Request;
18use Symfony\Component\HttpFoundation\Response;
19use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
20 
21class OrderLegacy
22{
23 
24    /**
25     * @var Application
26     */
27    protected $app;
28 
29    /**
30     * AbstractEvent constructor.
31     * @param \Silex\Application $app
32     */
33    public function __construct($app)
34    {
35        $this->app = $app;
36    }
37 
38 
39    public function changeTwig($event)
40    {
41 
42        $parts = $this->app->renderView(
43            'NakwebTest008/Resource/template/admin/nakwebtest008.twig'
44        );
45 
46        $baseHtml = $event->getSource();
47        $search   = '/(<div id="customer_info_box"  class="box accordion">)/';
48        $newHtml  = $parts . '<div id="customer_info_box"  class="box accordion">';
49        $dispHtml = preg_replace($search, $newHtml, $baseHtml);
50 
51        $event->setSource($dispHtml);
52 
53        return;
54 
55    }
56 
57}

 今回のメイン部分です。
 基本的に twig の変更に関しては preg_replace などの置換処理が多用されるようです。
 システムの修正やテンプレートの修正に非常に弱い作りで無いのかと思い、少し不安になります。
 
 ▼Resource/template/admin/nakwebtest008.twig

1<div class="box accordion form-horizontal">
2    <div class="box-header toggle">
3        <h3 class="box-title">テスト追加項目<svg class="cb cb-angle-down icon_down"> <use xlink:href="#cb-angle-down" /></svg></h3>
4    </div><!-- /.box-header -->
5    <div class="box-body accpanel">
6        <input type="texterea" value="ここに表示">
7    </div>
8</div>

 今回は項目の追加を目標としたため、フォームの中身については関与しません。
 テンプレートへの追加する項目のみ記載している形ですね。
 
 
 
今回の作業を行った結果の意見としては以下のような形です。
 プラグインを作成する場合はともかく、クライアントからのカスタマイズ依頼に関しては
 調整したテンプレートを使用するほうがよっぽど早いと思われる。
 
汎用的に作るプラグインであればテンプレートへの差し込み処理は絶対に必要になります。
しかし、一般的なカスタマイズを行う場合にはコストパフォーマンス的に考えるとありえない気がします。
まぁ、その際にはテンプレートを直接変更する手段が残っているので問題は無いのですが・・・
こんな感想を持つのは私だけなのでしょうか。
 
また、表示内容の変更方法は複数あるようですね。
今回の書き方以外にも色々な記載方法が見受けられます。
もう少しリファレンスが纏まっていると非常に助かる気がします。
 
 
しかし、今後公開のプラグインを挿入する際にプラグインを作るにしても導入するにしても ID / class の名称など非常に気を付ける必要があります。
慣れていないこともあり、一概には言えませんが 2系の時よりも気を使いますね。
・・・当面は注意が必要ですね。
 
ということで、今回はここまでです。
 

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

* Copy This Password *

* Type Or Paste Password Here *

*

コメント欄にコードを挿入したい場合は、[php][/php] を使ってください。