EC-CUBE3系プラグイン作成入門


EC-CUBE3のマイナーバージョンが上がりプラグインの作成が可能になったので、ちょっとずつプラグイン作成についてまとめていきたいと思います。

今回は、現状で最新となるEC-CUBE3.0.7を使っていきます。
インストール方法は以前まとめていますので、またインストールされたことことがない方はそちらをご覧ください。
インストーラを使ってEC-CUBE 3のインストール

また、公式サイトにプラグイン作成のマニュアルがあります。
規約など詳細に書かれてありますので、事前に目を通しておくと良いでしょう。
ドキュメントはこちら

それでは、実際にプラグインを作っていきます。
入門といえば、”HelloWorld”ですよね。TOPページに表示してみます。

以前のバージョンと同じように必須となるファイルがあります。

HelloWorld/
├── PluginManager.php
└── config.yml

config.yml

プラグイン基本情報を記載

name: HelloWorld #プラグイン名
code: HelloWorld #プラグインコード
version: 0.0.1 #プラグインバージョン

PluginManager.php

プラグインのインストール時や、更新時などの処理を記載

/*
 * This file is part of EC-CUBE
 *
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
 * http://www.lockon.co.jp/
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Plugin\HelloWorld;

use Eccube\Plugin\AbstractPluginManager;

class PluginManager extends AbstractPluginManager
{

    /**
     * インストール時
     */
    public function install($config, $app)
    {
    }

    /**
     * アンインストール時
     */
    public function uninstall($config, $app)
    {
    }

    /**
     * 有効時
     */
    public function enable($config, $app)
    {
    }

    /**
     * 無効時
     */
    public function disable($config, $app)
    {
    }

    /**
     * アップデート時
     */
    public function update($config, $app)
    {
    }
}

次に、イベントを追加していきます。
赤字が追加するファイルです。

HelloWorld/
├── PluginManager.php
├── config.yml
├── HelloWorldEvent.php
└── event.yml

config.ymlにeventを追記

event: HelloWorldEvent

この場合[HelloWorldEvent.php]ファイルが読み込まれます。

event.yml

利用するフックポイントを定義

【フォーマット】
{HookPointName}:
- [{MethodName}, {Priority}]
- [{MethodName}, {Priority}]

フックポイントには、以下の命名規則によって命名されています。
・ アプリケーション全体のフックポイント
    eccube.event.app.{before | after}
・ コントローラーイベントのフックポイント
    eccube.event.controller.{ルーティング名}.{before | after | finish}
・ レンダーイベントのフックポイント
    eccube.event.render.{ルーティング名}.before

フックポイントは大量にありますが
app/console router:debug
で確認することができます。

今回はTOPページに"HelloWorld"を表示するので
eccube.event.render.homepage.before:
    - [homepageBefore, NORMAL]
を指定します。

HelloWorldEvent.php(EventName.php)

フックポイントに対する処理を記述していきます。

/*
 * This file is part of EC-CUBE
 *
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
 * http://www.lockon.co.jp/
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Plugin\HelloWorld;

use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;

class HelloWorldEvent
{
    /**
     * @var \Eccube\Application
     */
    private $app;

    public function __construct($app)
    {
        $this->app = $app;
    }

    public function homepageBefore(FilterResponseEvent $event)
    {
        $request = $event->getRequest();
        $response = $event->getResponse();

        $addContent = 'Hello World!';

        // 書き換えhtmlの初期化
        $html = $response->getContent();

        // 書き換え処理ここから
        $crawler = new Crawler($html);
        $oldElement = $crawler->filter('.item_gallery');
        $oldHtml = $oldElement->html();
        $newHtml = $oldHtml . $addContent;
        $html = $crawler->html();
        $html = str_replace($oldHtml, $newHtml, $html);
        // 書き換え処理ここまで

        $response->setContent($html);
        $event->setResponse($response);
    }
}

以上の処理でなんとか任意の場所に”HelloWorld”を表示することができました。
次回は、もう少しふかいところまで追求してみたいと思います。


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>