タイトルの通り、EC-CUBE4のデフォルト機能のイベントについて解説します。商品編集時に商品の説明文が空白だった場合のデフォルトの文を登録する処理を追加します。
実装
「src/Eccube/Controller/Admin/Product/ProductController」の商品編集完了時のイベントに対して、設定をします。
「app/Customize/Event/Admin」ディレクトリを作成しましょう。
「app/Customize/Event/Admin」ディレクトリに「ProductEvent.php」ファイルを作成しましょう。
ソースコードは以下となります。
<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Customize\Event\Admin;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductEvent implements EventSubscriberInterface
{
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
/**
* @return array The event names to listen to
*/
public static function getSubscribedEvents()
{
return [
EccubeEvents::ADMIN_PRODUCT_EDIT_COMPLETE => 'comp'
];
}
/**
* 商品登録完了時の処理
*/
public function comp(EventArgs $event)
{
$auguments = $event->getArguments();
$Product = $auguments['Product'];
if(!$Product->getDescriptionDetail()) {
$Product->setDescriptionDetail('テスト説明');
$this->entityManager->flush($Product);
}
}
}
解説
結果
商品説明を空白で登録すると、下記のように「テスト説明」が登録されていれば動作問題なしです。

以上です。
スポンサーリンク
スポンサーリンク