今回は管理画面の既存フォームに項目を加えるための実験です。
実際には以前の【3-0-13】データベースへの入出力にて以下の既存フォームに対して項目の追加を行ったことはります。
管理画面 > 設定 > 基本情報設定 > ショップマスター > 基本情報編集
しかし、該当の部分はちょっと特殊な動きをしているため簡単でしたが、通常のフォームは一工夫いるようです。
そのため、その一工夫に当たる部分を確認してみたいと思います。
なお、今回も基本はプラグインジェネレータでのベース作成を行います。
とりあえず受注管理画面を触って見たいのでとりあえずは以下のような感じです。
---Entry confirmation [+]Plugin Name: ナックウェブテスト008 [+]Plugin Code: NakwebTest008 [+]Version: 0.0.1 [+]Author: NAKWEB [+]Old version support: No [+]Site events: eccube.event.route.admin_order_page.request eccube.event.route.admin_order_page.controller eccube.event.route.admin_order_page.response eccube.event.route.admin_order_page.exception eccube.event.route.admin_order_page.terminate [+]Hook points: admin.order.edit.index.initialize admin.order.edit.index.progress admin.order.edit.index.complete [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
Admin/Order/edit.twig:
- [onRenderAdminOrderEdit, NORMAL]
eccube.event.route.admin_order_page.request:
- [onRouteAdminOrderPageRequest, NORMAL]
eccube.event.route.admin_order_page.controller:
- [onRouteAdminOrderPageController, NORMAL]
eccube.event.route.admin_order_page.response:
- [onRouteAdminOrderPageResponse, NORMAL]
eccube.event.route.admin_order_page.exception:
- [onRouteAdminOrderPageException, NORMAL]
eccube.event.route.admin_order_page.terminate:
- [onRouteAdminOrderPageTerminate, NORMAL]
イベント情報の登録です。
twig ファイルに関してはプラグインジェネレーターでは表示されないのか、または指定が間違えているようですね。
とりあえず twig に関する記述を追加してみました。
▼NakwebTest008Event.php
<?php
/*
* This file is part of the NakwebTest008
*
* Copyright (C) 2017 ナックウェブテスト008
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\NakwebTest008;
use Eccube\Application;
use Eccube\Event\EventArgs;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
class NakwebTest008Event
{
/** @var \Eccube\Application $app */
private $app;
public function __construct(Application $app)
{
$this->app = $app;
}
public function onRouteAdminOrderPageRequest(EventArgs $event)
{
}
public function onRouteAdminOrderPageController(EventArgs $event)
{
}
public function onRouteAdminOrderPageResponse(EventArgs $event)
{
}
public function onRouteAdminOrderPageException(EventArgs $event)
{
}
public function onRouteAdminOrderPageTerminate(EventArgs $event)
{
}
public function onAdminOrderEditIndexInitialize(EventArgs $event)
{
}
public function onAdminOrderEditIndexProgress(EventArgs $event)
{
}
public function onAdminOrderEditIndexComplete(EventArgs $event)
{
}
public function onRenderAdminOrderEdit($event)
{
$nakwebTest008Event = $this->app['eccube.plugin.nakwebtest008.event.order_legacy'];
$nakwebTest008Event->changeTwig($event);
}
}
こちらもイベント情報の登録です。
特には問題はないかなと思います。
▼ServiceProvider/NakwebTest008ServiceProvider.php
<?php
/*
* This file is part of the NakwebTest008
*
* Copyright (C) 2017 ナックウェブテスト008
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\NakwebTest008\ServiceProvider;
use Eccube\Application;
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
use Monolog\Handler\FingersCrossedHandler;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
use Plugin\NakwebTest008\Event\OrderLegacy;
use Plugin\NakwebTest008\Form\Type\NakwebTest008ConfigType;
use Silex\Application as BaseApplication;
use Silex\ServiceProviderInterface;
use Symfony\Component\Yaml\Yaml;
class NakwebTest008ServiceProvider implements ServiceProviderInterface
{
public function register(BaseApplication $app)
{
// プラグイン用設定画面
$app->match('/'.$app['config']['admin_route'].'/plugin/NakwebTest008/config', 'Plugin\NakwebTest008\Controller\ConfigController::index')->bind('plugin_NakwebTest008_config');
// 独自コントローラ
$app->match('/plugin/[lower_code]/hello', 'Plugin\NakwebTest008\Controller\NakwebTest008Controller::index')->bind('plugin_NakwebTest008_hello');
// Order legacy event
$app['eccube.plugin.nakwebtest008.event.order_legacy'] = $app->share(function () use ($app) {
return new OrderLegacy($app);
});
// Form
$app['form.types'] = $app->share($app->extend('form.types', function ($types) use ($app) {
$types[] = new NakwebTest008ConfigType();
return $types;
}));
// Repository
// Service
// メッセージ登録
// $file = __DIR__ . '/../Resource/locale/message.' . $app['locale'] . '.yml';
// $app['translator']->addResource('yaml', $file, $app['locale']);
// load config
// プラグイン独自の定数はconfig.ymlの「const」パラメータに対して定義し、$app['[lower_code]config']['定数名']で利用可能
// if (isset($app['config']['NakwebTest008']['const'])) {
// $config = $app['config'];
// $app['[lower_code]config'] = $app->share(function () use ($config) {
// return $config['NakwebTest008']['const'];
// });
// }
// ログファイル設定
$app['monolog.logger.[lower_code]'] = $app->share(function ($app) {
$logger = new $app['monolog.logger.class']('[lower_code]');
$filename = $app['config']['root_dir'].'/app/log/[lower_code].log';
$RotateHandler = new RotatingFileHandler($filename, $app['config']['log']['max_files'], Logger::INFO);
$RotateHandler->setFilenameFormat(
'[lower_code]_{date}',
'Y-m-d'
);
$logger->pushHandler(
new FingersCrossedHandler(
$RotateHandler,
new ErrorLevelActivationStrategy(Logger::ERROR),
0,
true,
true,
Logger::INFO
)
);
return $logger;
});
}
public function boot(BaseApplication $app)
{
}
}
こちらも twig を変更する際の記述を追加しています。
▼Event/OrderLegacy.php
<?php
/*
* This file is part of the Maker plugin
*
* Copyright (C) 2016 LOCKON CO.,LTD. All Rights Reserved.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\NakwebTest008\Event;
use Eccube\Application;
use Eccube\Common\Constant;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
class OrderLegacy
{
/**
* @var Application
*/
protected $app;
/**
* AbstractEvent constructor.
* @param \Silex\Application $app
*/
public function __construct($app)
{
$this->app = $app;
}
public function changeTwig($event)
{
$parts = $this->app->renderView(
'NakwebTest008/Resource/template/admin/nakwebtest008.twig'
);
$baseHtml = $event->getSource();
$search = '/(<div id="customer_info_box" class="box accordion">)/';
$newHtml = $parts . '<div id="customer_info_box" class="box accordion">';
$dispHtml = preg_replace($search, $newHtml, $baseHtml);
$event->setSource($dispHtml);
return;
}
}
今回のメイン部分です。
基本的に twig の変更に関しては preg_replace などの置換処理が多用されるようです。
システムの修正やテンプレートの修正に非常に弱い作りで無いのかと思い、少し不安になります。
▼Resource/template/admin/nakwebtest008.twig
<div class="box accordion form-horizontal">
<div class="box-header toggle">
<h3 class="box-title">テスト追加項目<svg class="cb cb-angle-down icon_down"> <use xlink:href="#cb-angle-down" /></svg></h3>
</div><!-- /.box-header -->
<div class="box-body accpanel">
<input type="texterea" value="ここに表示">
</div>
</div>
今回は項目の追加を目標としたため、フォームの中身については関与しません。
テンプレートへの追加する項目のみ記載している形ですね。
今回の作業を行った結果の意見としては以下のような形です。
プラグインを作成する場合はともかく、クライアントからのカスタマイズ依頼に関しては
調整したテンプレートを使用するほうがよっぽど早いと思われる。
汎用的に作るプラグインであればテンプレートへの差し込み処理は絶対に必要になります。
しかし、一般的なカスタマイズを行う場合にはコストパフォーマンス的に考えるとありえない気がします。
まぁ、その際にはテンプレートを直接変更する手段が残っているので問題は無いのですが・・・
こんな感想を持つのは私だけなのでしょうか。
また、表示内容の変更方法は複数あるようですね。
今回の書き方以外にも色々な記載方法が見受けられます。
もう少しリファレンスが纏まっていると非常に助かる気がします。
しかし、今後公開のプラグインを挿入する際にプラグインを作るにしても導入するにしても ID / class の名称など非常に気を付ける必要があります。
慣れていないこともあり、一概には言えませんが 2系の時よりも気を使いますね。
・・・当面は注意が必要ですね。
ということで、今回はここまでです。











