ブログ B L O G

ECCUBE 4 プラグインの作り方

# はじめに 今回はEC-CUBE4のプラグインを作成するにあたって 実用面から、順を追って解説しています。 ※参考文献がある場合には、そのリンクを記載していますのでご確認ください。 ## プラグインインストール ### 必須ファイル プラグインインストールには以下のファイルが必要です。 プラグインコードを予め決めた上で、 プラグイン内クラスのnamespaceへ[Plugin\プラグインコード]の記載が必要になります。 **プラグインディレクトリ/composer.json** ```json { "name": "ec-cube/PluginCode", "version": "0.0.0", "description": "PluginCode", "type": "eccube-plugin", "require": { "ec-cube/plugin-installer": "~0.0.6" }, "extra": { "code": "PluginCode" } } ※プラグインコードを「PluginCode」として記載しています。 ``` (参考: http://doc4.ec-cube.net/plugin_spec#composerjson) ## インストール ユーザー独自プラグインをインストールする場合は以下の2通りの方法があります。 1. プラグインファイルをtar.gzで圧縮して管理画面からアップロードする方法 2. [app/Plugin/プラグインコード]のディレクトリを作成しcomposer.jsonをディレクトリ内に保存後 ```bin/console eccube:plugin:install --code=プラグインコード``` のコマンドによりプラグインをインストールする方法。 **補足** eccubeからサポートされたプラグインに関するコマンド: eccube:plugin:disable eccube:plugin:enable eccube:plugin:generate eccube:plugin:install eccube:plugin:schema-update eccube:plugin:uninstall eccube:plugin:update ## プラグインインストール、有効、無効、削除、アップロード動作追加 プラグインインストール、有効、無効、削除、アップロードそれぞれの場合に、独自の動作を追加したい場合にはPluginManagerの追加が必要です。 ***プラグインディレクトリ/PluginManager.php*** ```php get('doctrine.orm.entity_manager'); /** @var \Eccube\Repository\Master\DeviceTypeRepository $deviceTypeRepository */ $deviceTypeRepository = $container->get(DeviceTypeRepository::class); //PCタイプを取得する $pcDevice = $deviceTypeRepository->find(DeviceType::DEVICE_TYPE_PC); $plguinLayout = new Layout(); $plguinLayout->setDeviceType($pcDevice); $plguinLayout->setName("プラグイン作成レイアウト"); $entityManager->persist($plguinLayout); $entityManager->flush(); } } ``` ## プラグインインストール時のデータベース操作について ### テーブルカラムの追加 既存テーブルにカラムを追加したい場合は [プラグインディレクトリ/Entity/{拡張DBクラス名}Trait.php]ファイルの追加が必要です。 **例** dtb_pageカラム追加 ***プラグインディレクトリ/Entity/PageTrait.php*** ```php css = $css; return $this; } /** * @return string */ public function getCss() { return $this->css; } } ``` **拡張** プラグインが有効化されるときに[Eccube\Entity\Page]に対してapp/proxy/entity/Page.phpクラスが自動作成されます。 Eccube\Entity\Pageを利用するときに [src/Eccube/Entity/Page.php]の代わりに[app/proxy/entity/Page.php]を呼び出します。 ### テーブル追加 新規テーブル作成時に [プラグインディレクトリ/Entity/{DBクラス名}.php]のファイル追加が必要になります。 **例** ***プラグインディレクトリ/Entity/History.php*** ```php id; } /** * @return \Eccube\Entity\Order */ public function getOrder() { return $this->Order; } /** * @param \Eccube\Entity\Order $Order * * @return History */ public function setOrder(\Eccube\Entity\Order $Order) { $this->Order = $Order; return $this; } /** * @return string */ public function getData() { return $this->data; } /** * @param $data * @return $this */ public function setData($data) { $this->data = $data; return $this; } } ``` ***プラグインディレクトリ/Repository/HistoryRepository.php*** ```php context = $context; } /** * @param GetResponseEvent $event */ public function onKernelRequest(GetResponseEvent $event) { if ($this->context->isAdmin()) {//管理画面判定 printf('is admin'); } if ($this->context->isFront()) {//フロント判定 printf('is front'); } $user = $this->context->getCurrentUser();//ログインのユーザー (管理画面の場合はメンバー、フロント画面の場合はカスタマー) if ($user) { if ($user instanceof Member) { printf('member name: %s
', $user->getName()); } else if ($user instanceof Customer) { printf('customer name: %s %s
', $user->getName01(), $user->getName02()); } } $request = $event->getRequest(); /** @var \Symfony\Component\HttpFoundation\ParameterBag $attributes */ $attributes = $request->attributes; printf('route name: %s
', $attributes->get('_route')); //ルーティング名を取得 $attributes->set関数により切り替えも可能です。 printf('route params: %s
', print_r($attributes->get('_route_params', []), 1)); //ルーティングのパラメータ取得 printf('controller: %s
', $attributes->get('_controller')); //実行予定のコンソールを取得する $attributes->setにより設定も可能です。 printf('template: %s
', $attributes->get('_template')); //実行予定のテンプレートを取得する $attributes->setにより設定も可能です。 } /** * @return array */ public static function getSubscribedEvents() { return [ KernelEvents::REQUEST => ['onKernelRequest', 1], //ここは優先順位を1に設定する ]; } } ``` **追記** http Event Listenerを利用する場合は優先順位により実行結果が変わることがあるので、 複数のEvent Listenerの参照が必要でしょう。 詳細はeccubeの[src/Eccube/EventListener]内のファイルを参照するようにしてください。 ## eccubeのイベント (参考: https://github.com/EC-CUBE/sample-payment-plugin#%E3%82%A4%E3%83%99%E3%83%B3%E3%83%88%E3%81%AE%E8%BF%BD%E5%8A%A0) ***プラグインディレクトリ/PluginCodeEvent.php*** ```php 'onAdminContentPageEdit', //defaultテンプレートファイルイベント 'Product/list.twig' => 'onProductList', //ECCUBEイベント,eccubeイベントによりform,パラメータなどの取得と修正が可能です。 //利用可能なイベントはsrc/Eccube/Event/EccubeEvents.phpまで参考ください EccubeEvents::ADMIN_CONTENT_PAGE_EDIT_INITIALIZE => 'adminContentPageEditInitialize', ]; } public function onAdminContentPageEdit(TemplateEvent $event) { $event->setParameter('pluginCodeTest', 'test'); $event->setSource('{{ pluginCodeTest }} 
' . $event->getSource()); } public function onProductList(TemplateEvent $event) { } public function adminContentPageEditInitialize(EventArgs $eventArgs) { } } ``` **追記** 可能なイベント一覧 https://github.com/EC-CUBE/ec-cube/blob/4.0/src/Eccube/Event/EccubeEvents.php ## ページの追加 (参考: https://github.com/EC-CUBE/sample-payment-plugin#%E3%83%AB%E3%83%BC%E3%83%86%E3%82%A3%E3%83%B3%E3%82%B0%E3%81%AE%E8%BF%BD%E5%8A%A0) **例** ***プラグインディレクトリ/Controller/MyController.php*** ```php redirectToRoute('homepage', []); } ``` 3. テンプレートを動的にする ```php /** * テストページ. * * @Route("/test_page", name="test_page") */ public function test() { return $this->render('@PluginCode/test_page.twig', []); } ``` 4. JSON方式で返信 ```php /** * テストページ. * * @Route("/test_page", name="test_page") */ public function test() { return $this->json(['test'=>'data']); } ``` ## formの追加 (参考: https://github.com/EC-CUBE/sample-payment-plugin#formtype%E6%8B%A1%E5%BC%B5) ## バッチ追加 プラグインにバッチクラスを追加することによって [bin/console xxxx] でバッチをコマンド実行することが可能です。 **例** ***プラグインディレクトリ/PluginCodeCommand.php*** ```php setDescription('test command') ->addOption('timeout', null, InputOption::VALUE_REQUIRED, '', 30) ->addOption('limit', null, InputOption::VALUE_REQUIRED, '', 0); } /** * @param InputInterface $input * @param OutputInterface $output * * @return int|null|void */ protected function execute(InputInterface $input, OutputInterface $output) { //テキスト出力する $output->writeln("test"); $output->writeln(sprintf("timeout: %s", $input->getOption('timeout'))); $output->writeln(sprintf("limit: %s", $input->getOption('limit'))); } } ``` [bin/console plugin_code:test --timeout=10 --limit=20]のコマンドで呼び出せます。 ## 翻訳ファイル 今回、EC-CUBE4では多言語化の機能がありますが もちろんプラグインも翻訳ファイルを追加することによって多言語化が可能です。 **例** ***プラグインディレクトリ/Resource/locale/messages.ja.yaml*** ``` PluginCode.test1: test1 PluginCode.test2: test2 ``` ## 管理画面ナビの拡張 (参考: https://github.com/EC-CUBE/sample-payment-plugin#%E7%AE%A1%E7%90%86%E7%94%BB%E9%9D%A2%E3%83%8A%E3%83%93%E3%81%AE%E6%8B%A1%E5%BC%B5)

お問合せ

サンデイシステムズへのお問合わせは、下記よりコンタクトフォームをご利用下さい。
担当の者より、追ってご連絡させていただきます。

GO TO PAGE TOP