# EC-CUBE 4.2 プラグイン 開発
Ec-cube v4.2.1 を利用して商品ページに NEW タグ表示する独自プラグインを開発します。
プラグイン開発においての注意点
- 開発中に画面上で開発中プラグインのアンインストール(削除)をしない
- 開発中にアンインストールすると app/Plugin 配下のソースがすべてシステム的に削除されてしまいます。
eccube:plugin:uninstall --forceコマンド使わない- プラグインをアンインストールしてしまうので、使わないほうがいいです。
# プラグイン生成コマンド
bin/console e:p:g
`bin/console e:p:g` とは
bin/console eccube:plugin:generateの略でプラグインを開発するためのコマンドラインツールです。
このコマンドを実行することで、プラグインの雛形を作成することができます。
プラグインインストール eccube:plugin:install
bin/console e:p:i –code=[プラグイン名]
プラグインの有効化 eccube:plugin:enable
bin/console e:p:e –code=[プラグイン名]
プラグインの無効化 eccube:plugin:disable
bin/console e:p:d –code=[プラグイン名]
デフォルトで作成されるディレクトリとファイル一覧
- Controller
- Entity
- Event.php
- Form
- Nav.php
- Repository
- Resource
- TwigBlock.php
- composer.json
# プラグインのディレクトリ構造
プロジェクト名 ProductNewTag にします。
app/Plugin/ProductNewTag/
├── Command
├── Common
│ └── Nav.php
├── Controller
├── Doctrine
│ └── Query
├── Entity
├── EventListener
│ └── EventListener.php
├── Form
│ ├── Extension
│ └── Type
├── PluginManager.php
├── Repository
├── Resource
│ ├── assets
│ ├── config
│ │ └── services.yaml
│ ├── locale
│ └── template
└── composer.json
設定ファイルであるcomposer.jsonは必須
services.yaml はコンテナの定義保存しています。
# composer.json
{
"name": "ec-cube/productnewtag",
"version": "1.0.0",
"description": "新着商品タグ表示プラグイン",
"type": "eccube-plugin",
"require": {
"ec-cube/plugin-installer": "~0.0.6"
},
"autoload": {
"psr-4": {
"Plugin\\ProductNewTag\\": "app"
}
},
"extra": {
"code": "ProductNewTag"
}
}
| 項目 | 説明 | メモ |
|---|---|---|
| name | パッケージ名 | ec-cube/[プラグインコード] を記述 |
| version | バージョン | php のバージョンフォーマットに合わせ |
| description | プラグイン名称 | |
| type | パッケージタイプ | eccube-plugin固定 |
| require | 依存パッケージ | ec-cube/plugin-installer": "~0.0.6" などを記述 |
| extra | 付属情報 | "code": "[プラグインコード]" を記述 |
# プラグインのファイル
app/Plugin/ProductNewTag/ProductNewTag.phpプラグインのエントリーポイントapp/Plugin/ProductNewTag/Service/NewTagService.php商品名に New タグを追加する処理を実装するサービス
ProductNewTag.php
<?php
namespace Plugin\ProductNewTag;
use Eccube\Plugin\AbstractPluginManager;
use Plugin\ProductNewTag\Service\NewTagService;
class ProductNewTag extends AbstractPluginManager
{
public function enable($app)
{
parent::enable($app);
$this->container->get('event_dispatcher')->addListener(
'ProductClass\ProductListFilterEvent',
[$this->container->get(NewTagService::class), 'addNewTagToProductName']
);
}
public function disable($app)
{
$this->container->get('event_dispatcher')->removeListener(
'ProductClass\ProductListFilterEvent',
[$this->container->get(NewTagService::class), 'addNewTagToProductName']
);
parent::disable($app);
}
}
NewTagService.php
<?php
namespace Plugin\ProductNewTag\Service;
use Eccube\Event\ProductClass\ProductListFilterEvent;
class NewTagService
{
public function addNewTagToProductName(ProductListFilterEvent $event)
{
$productList = $event->getList();
foreach ($productList as &$product) {
$product['name'] = '[New] ' . $product['name'];
}
$event->setList($productList);
}
}
# プラグインを有効化
php bin/console plugin:register
php bin/console plugin:install ProductNewTag
php bin/console plugin:enable ProductNewTag
# エラー
Warning: Declaration of Plugin\ProductNewTag\ProductNewTag::enable($app) should be compatible with Eccube\Plugin\AbstractPluginManager::enable(array $meta, Symfony\Component\DependencyInjection\ContainerInterface $container) in /var/www/html/app/config/eccube/services.yaml (which is loaded in resource "/var/www/html/app/config/eccube/services.yaml").
# 開発コマンド
# データベースごと全テーブルを削除
bin/console doctrine:schema:drop --force --full-database
# データベース、テーブルを作成
bin/console doctrine:schema:create
# 任意のプラグインをインストール・有効化
bin/console e:p:i --code=Holiday
bin/console e:p:e --code=Holiday
# キャッシュクリア
bin/console cache:clear --no-warmup