1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

EC-CUBE4 管理画面に任意の情報を表示する方法

Last updated at Posted at 2021-01-30

はじめに

管理画面に任意の情報を表示する方法です。
Entity を拡張した場合は、 "Entity からフォームを自動生成する " が利用できます。

部分的にテンプレートを差し込む方法

この方法は、オリジナルの Twig テンプレートファイルを修正することなく、任意の表示を任意の場所に追加できます。
※ 恐らく、一般的な EC-CUBE プラグインが使っている方法と同じです。

差し込み用の Twig ファイルを用意

今回は、メインエリアの一番下に表示されるサンプルです。
表示位置を変更する場合は、 jQuery のセレクタ .c-contentsArea__cols .c-contentsArea__primaryCol .c-primaryCol の部分を任意のセレクトにすることで、表示位置が変わります。

app/template/admin/Product/product__snippet.twig
<div class="card rounded border-0 mb-4" id="js-your-snipet">
    HELLO {{ someVar }}
</div>
<script>
    (function () {
        $('.c-contentsArea__cols .c-contentsArea__primaryCol .c-primaryCol').append($('#js-your-snipet'));
    })();
</script>

※ ファイル名に制約はありません。

対象の Twig ファイルを監視して、差し込み用テンプレートを追加

app/Customize/EventListener/AdminProductTwigEventListener.php
<?php

declare(strict_types=1);

namespace Customize\EventListener;

use Eccube\Event\TemplateEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class AdminProductTwigEventListener implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            '@admin/Product/product.twig' => 'handle'
        ];
    }

    public function handle(TemplateEvent $event): void
    {
        $event->setParameter('someVar', 'abc');
        $event->addSnippet('@admin/Product/product__snippet.twig');
    }
}

完成 :raised_hands_tone2:

スクリーンショット 2021-01-30 15.12.22.png

独自のテンプレートを利用する方法

"テンプレートの読み出し順序" を利用して、 YOUR_PROJECT/app/template/admin 以下にファイルを設置します。

例えば、管理画面の商品詳細画面の場合、オリジナルのテンプレートは YOUR_PROJECT/src/Eccube/Resource/template/admin/Product/product.twig です。

この場合、独自テンプレートを YOUR_PROJECT/app/template/admin/Product/product.twig に設置すると優先的に読み込まれます。

:warning: 注意点
この方法は、EC-CUBE 本体のバージョンアップや他の EC-CUBE プラグインによる拡張が適用されない可能性が高いです。
EC-CUBE バージョンアップや該当ファイルを監視するプラグインを使う場合は注意が必要です。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?