【EC-CUBE3】プラグインを有効化したときにdtb_page_layoutにページ情報を登録する方法です。
プラグインを有効化したときにdtb_page_layoutにページ情報を登録するためには以下のようなマイグレーションファイルを用意する必要があります。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace DoctrineMigrations; | |
use Doctrine\DBAL\Migrations\AbstractMigration; | |
use Doctrine\DBAL\Schema\Schema; | |
use Eccube\Application; | |
use Eccube\Entity\PageLayout; | |
use Eccube\Entity\Master\DeviceType; | |
/** | |
* Auto-generated Migration: Please modify to your needs! | |
*/ | |
class Version20170312115644 extends AbstractMigration | |
{ | |
const PAGE_NAME = 'ページ名'; | |
const ROUTE_NAME = 'plg_route_name'; | |
public function __construct() | |
{ | |
$this->app = Application::getInstance(); | |
} | |
/** | |
* @param Schema $schema | |
*/ | |
public function up(Schema $schema) | |
{ | |
$this->createPageLayout(); | |
} | |
/** | |
* @param Schema $schema | |
*/ | |
public function down(Schema $schema) | |
{ | |
$this->deletePageLayout(); | |
} | |
/** | |
* dtb_page_layoutにページ情報を登録 | |
*/ | |
protected function createPageLayout() | |
{ | |
$em = $this->app['orm.em']; | |
$DeviceType = $this->app['eccube.repository.master.device_type'] | |
->find(DeviceType::DEVICE_TYPE_PC); | |
$PageLayout = $this->findPageLayout($DeviceType, self::ROUTE_NAME); | |
if($PageLayout === false) { | |
$PageLayout = new PageLayout(); | |
$PageLayout->setDeviceType($DeviceType); | |
$PageLayout->setName(self::PAGE_NAME); | |
$PageLayout->setUrl(self::ROUTE_NAME); | |
$PageLayout->setEditFlg(PageLayout::EDIT_FLG_DEFAULT); | |
$em->persist($PageLayout); | |
$em->flush(); | |
} else { | |
throw new \Exception(sprintf("%sはすでに登録されています。", self::ROUTE_NAME)); | |
} | |
} | |
/** | |
* dtb_page_layoutからページ情報を削除 | |
*/ | |
protected function deletePageLayout() | |
{ | |
$em = $this->app['orm.em']; | |
$DeviceType = $this->app['eccube.repository.master.device_type'] | |
->find(DeviceType::DEVICE_TYPE_PC); | |
$PageLayout = $this->findPageLayout($DeviceType, self::ROUTE_NAME); | |
if($PageLayout instanceof PageLayout) { | |
$em->remove($PageLayout); | |
$em->flush(); | |
} | |
} | |
/** | |
* ルーティングからページ情報を取得 | |
* | |
* @param type $DeviceType | |
* @param type $url ルーティング | |
* @return boolean | |
*/ | |
protected function findPageLayout($DeviceType, $url) | |
{ | |
try{ | |
$em = $this->app['orm.em']; | |
$PageLayout = $em->getRepository('Eccube\Entity\PageLayout'); | |
$PageLayout = $PageLayout->createQueryBuilder("pl") | |
->where('pl.DeviceType = :DeviceType AND pl.url = :url') | |
->setParameter("DeviceType", $DeviceType) | |
->setParameter("url", $url) | |
->getQuery() | |
->getSingleResult(); | |
return $PageLayout; | |
} catch (\Exception $e) { | |
return false; | |
} | |
} | |
} |
upメソッドでdtb_page_layoutテーブルにページ情報を登録、downメソッドでページ情報を削除する処理が書かれています。
次にPluginManager.phpに以下のように設定すれば、プラグイン有効時にdtb_page_layoutテーブルにページ情報を登録され、プラグインを削除したときにページ情報が削除されます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* This file is part of the SamplePlugin | |
* | |
* Copyright (C) 2017 サンプルプラグイン | |
* | |
* For the full copyright and license information, please view the LICENSE | |
* file that was distributed with this source code. | |
*/ | |
namespace Plugin\SamplePlugin; | |
use Eccube\Application; | |
use Eccube\Plugin\AbstractPluginManager; | |
class PluginManager extends AbstractPluginManager | |
{ | |
/** | |
* プラグインインストール時の処理 | |
* | |
* @param $config | |
* @param Application $app | |
* @throws \Exception | |
*/ | |
public function install($config, Application $app) | |
{ | |
} | |
/** | |
* プラグイン削除時の処理 | |
* | |
* @param $config | |
* @param Application $app | |
*/ | |
public function uninstall($config, Application $app) | |
{ | |
$this->migrationSchema($app, __DIR__.'/Resource/doctrine/migration', $config['code'], 0); | |
} | |
/** | |
* プラグイン有効時の処理 | |
* | |
* @param $config | |
* @param Application $app | |
* @throws \Exception | |
*/ | |
public function enable($config, Application $app) | |
{ | |
$this->migrationSchema($app, __DIR__.'/Resource/doctrine/migration', $config['code']); | |
} | |
/** | |
* プラグイン無効時の処理 | |
* | |
* @param $config | |
* @param Application $app | |
* @throws \Exception | |
*/ | |
public function disable($config, Application $app) | |
{ | |
} | |
/** | |
* プラグイン更新時の処理 | |
* | |
* @param $config | |
* @param Application $app | |
* @throws \Exception | |
*/ | |
public function update($config, Application $app) | |
{ | |
$this->migrationSchema($app, __DIR__.'/Resource/doctrine/migration', $config['code']); | |
} | |
} |