EC-CUBE 3 プラグイン開発でフックポイントの見つけ方のメモ : EC-CUBE 3

Pocket

EC-CUBE3はフックポイントが用意されておりコアの処理へプラグインから介入することができます。
フックポイントも見つけ方のメモです。

フックポイントの見つけ方

基本: $app[‘eccube.event.dispatcher’]で検索しフックポイントを探します。

1. src/Eccube/Event/EccubeEvents.phpから探す

ソースを検索すると例えば下記のコードが見つかります。

// src/Eccube/Controller/Mypage/MypageController.php
$app['eccube.event.dispatcher']->dispatch(EccubeEvents::FRONT_MYPAGE_MYPAGE_LOGIN_INITIALIZE, $event);

EccubeEvents::FRONT_MYPAGE_MYPAGE_LOGIN_INITIALIZEとあるのでsrc/Eccube/Event/EccubeEvents.phpでFRONT_MYPAGE_MYPAGE_LOGIN_INITIALIZEを検索します。

// src/Eccube/Event/EccubeEvents.php
const FRONT_MYPAGE_MYPAGE_LOGIN_INITIALIZE = 'front.mypage.mypage.login.initialize';

上記からfront.mypage.mypage.login.initializeに対応していることがわかります。

2. Application.phpから探す

src/Eccube/Applicationに下記のコードがあります。

// src/Eccube/Application.php
$this->on(\Symfony\Component\HttpKernel\KernelEvents::RESPONSE, function (\Symfony\Component\HttpKernel\Event\FilterResponseEvent $event) use ($app) {
    if (!$event->isMasterRequest()) {
        return;
    }
    $route = $event->getRequest()->attributes->get('_route');
    $app['eccube.event.dispatcher']->dispatch('eccube.event.render.'.$route.'.before', $event);
});

上記のeccube.event.render.’.$route.’.beforeがフックポイントになるので利用可能なrouteを調べます。
routeはコンソールでルーティングを調べることができます。

$ php app/console router:debug

例えば上記コマンドの結果一覧にproduct_detailがあるのでeccube.event.render.product_detail.beforeというフックポイントがあることになります。

商品詳細ページのレンダリング前に介入するにはevent.ymlへ下記を記載します。

eccube.event.render.product_detail.before:
    - [onRenderProductDetailBefore, NORMAL]

またイベントファイル名をconfig.ymlへ記載します。

event: RenderProductDetailBefore

onRenderProductDetailBeforeの実装をRenderProductDetailBefore.phpへ記載します。

コメント

No comments yet.

コメントの投稿

改行と段落タグは自動で挿入されます。
メールアドレスは表示されません。