Magento2 コアファイルの継承方法
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
]
開始行:
[[Magentoめも]]
#contents
*Magento2 コアファイルの継承方法 [#f9d8948c]
Magento2にすでにある機能を継承して動きを変えたい場合があ...
その場合は、モジュールを作成し、etc/di.xml ファイルにpref...
*Modelの継承 [#v7d9c4ad]
vendor\magento\module-catalog\Model\Product.php を継承し...
app/code/Yassujp/HelloWorld/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-inst...
<preference for="Magento\Catalog\Model\Product" type...
</config>
継承したいメソッドをコピーして書き換えます。
app\code\Yassujp\HelloWorld\Model\Product.php
<?php
namespace Yassujp\HelloWorld\Model;
class Product extends \Magento\Catalog\Model\Product
{
public function getName()
{
return $this->_getData(self::NAME).' Local';
}
}
#ref(model.png)
商品名に Local が付加されていることが確認できます。
*Blockの継承 [#aebc7d30]
vendor\magento\module-catalog\Block\Product\View.php を継...
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-inst...
<preference for="Magento\Catalog\Block\Product\View"...
</config>
継承したいメソッドをコピーして書き換えます
app\code\Yassujp\HelloWorld\Block\Product\View.php
<?php
namespace Yassujp\HelloWorld\Block\Product;
class View extends \Magento\Catalog\Block\Product\View
{
public function getProduct()
{
echo 'Local Block';
if (!$this->_coreRegistry->registry('product') &&...
$product = $this->productRepository->getById...
$this->_coreRegistry->register('product', $p...
}
return $this->_coreRegistry->registry('product');
}
}
商品詳細ページの左上に Local Block と表示されていることが...
*Controllerの継承 [#g0ea5270]
vendor\magento\module-catalog\Controller\Product\View.php...
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-inst...
<preference for="Magento\Catalog\Controller\Product\...
</config>
app\code\Yassujp\HelloWorld\Controller\Product\View.php
<?php
namespace Yassujp\HelloWorld\Controller\Product;
class View extends \Magento\Catalog\Controller\Product\V...
{
public function execute()
{
echo 'I am in Local Controller';
$categoryId = (int) $this->getRequest()->getPara...
$productId = (int) $this->getRequest()->getParam...
$specifyOptions = $this->getRequest()->getParam(...
if ($this->getRequest()->isPost() && $this->getR...
$product = $this->_initProduct();
if (!$product) {
return $this->noProductRedirect();
}
if ($specifyOptions) {
$notice = $product->getTypeInstance()->g...
$this->messageManager->addNotice($notice);
}
if ($this->getRequest()->isAjax()) {
$this->getResponse()->representJson(
$this->_objectManager->get('Magento\...
'backUrl' => $this->_redirect->g...
])
);
return;
}
$resultRedirect = $this->resultRedirectFacto...
$resultRedirect->setRefererOrBaseUrl();
return $resultRedirect;
}
$params = new \Magento\Framework\DataObject();
$params->setCategoryId($categoryId);
$params->setSpecifyOptions($specifyOptions);
try {
$page = $this->resultPageFactory->create();
$this->viewHelper->prepareAndRender($page, $...
return $page;
} catch (\Magento\Framework\Exception\NoSuchEnti...
return $this->noProductRedirect();
} catch (\Exception $e) {
$this->_objectManager->get('Psr\Log\LoggerIn...
$resultForward = $this->resultForwardFactory...
$resultForward->forward('noroute');
return $resultForward;
}
}
}
商品詳細ページ左上に I am in Local Controller と表示され...
参考
-http://magento.stackexchange.com/questions/86497/how-to-...
-http://magento.stackexchange.com/questions/113511/how-to...
*AbstractControllerの書き換え [#y5d02377]
上記のextendsではどうやっても動作しませんでした。
以下のように記述することで書き換えできました。
app/etc/NonComposerComponentRegistration.php
$pathList[] = dirname(__DIR__) . '/etc/ClassReplacer.php';
app/etc/ClassReplacer.php
<?php
class ClassReplacer
{
public function includeReplacedFiles($src)
{
try {
$replacedFiles = $this->listDir($src, false,...
foreach ($replacedFiles as $replacedFile) {
include_once $src . $replacedFile;
}
} catch (Exception $e) {
return;
}
}
protected function listDir($dir, $prependDir = false...
{
if (!is_dir($dir)) {
return array();
}
$currPath = $prependDir ? $dir : $currPath;
$currPath = $currPath !== '' ? rtrim($currPath, ...
$files = array();
foreach (scandir($dir) as $file) {
if (in_array($file, array('.', '..'))) {
continue;
}
$entity = $currPath . $file;
if ($recursive && is_dir("$dir/$file")) {
$files = array_merge($files, $this->list...
continue;
}
if ($entityRegexp && !preg_match($entityRege...
$files[] = $entity;
}
return $files;
}
}
$replace = new ClassReplacer();
$replace->includeReplacedFiles(dirname(__DIR__) . '/code...
以下のファイルの動作を変更したい場合、~
vendor\magento\module-sales\Controller\AbstractController...
以下へコピー~
app\code\Magento\Sales\Controller\AbstractController\Reor...
// return $resultRedirect->setPath('checkout/cart');
return $resultRedirect->setPath('checkout');
これで一つカートの画面を飛ばすことができました。
参考~
http://magento.stackexchange.com/questions/108001/overrid...
終了行:
[[Magentoめも]]
#contents
*Magento2 コアファイルの継承方法 [#f9d8948c]
Magento2にすでにある機能を継承して動きを変えたい場合があ...
その場合は、モジュールを作成し、etc/di.xml ファイルにpref...
*Modelの継承 [#v7d9c4ad]
vendor\magento\module-catalog\Model\Product.php を継承し...
app/code/Yassujp/HelloWorld/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-inst...
<preference for="Magento\Catalog\Model\Product" type...
</config>
継承したいメソッドをコピーして書き換えます。
app\code\Yassujp\HelloWorld\Model\Product.php
<?php
namespace Yassujp\HelloWorld\Model;
class Product extends \Magento\Catalog\Model\Product
{
public function getName()
{
return $this->_getData(self::NAME).' Local';
}
}
#ref(model.png)
商品名に Local が付加されていることが確認できます。
*Blockの継承 [#aebc7d30]
vendor\magento\module-catalog\Block\Product\View.php を継...
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-inst...
<preference for="Magento\Catalog\Block\Product\View"...
</config>
継承したいメソッドをコピーして書き換えます
app\code\Yassujp\HelloWorld\Block\Product\View.php
<?php
namespace Yassujp\HelloWorld\Block\Product;
class View extends \Magento\Catalog\Block\Product\View
{
public function getProduct()
{
echo 'Local Block';
if (!$this->_coreRegistry->registry('product') &&...
$product = $this->productRepository->getById...
$this->_coreRegistry->register('product', $p...
}
return $this->_coreRegistry->registry('product');
}
}
商品詳細ページの左上に Local Block と表示されていることが...
*Controllerの継承 [#g0ea5270]
vendor\magento\module-catalog\Controller\Product\View.php...
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-inst...
<preference for="Magento\Catalog\Controller\Product\...
</config>
app\code\Yassujp\HelloWorld\Controller\Product\View.php
<?php
namespace Yassujp\HelloWorld\Controller\Product;
class View extends \Magento\Catalog\Controller\Product\V...
{
public function execute()
{
echo 'I am in Local Controller';
$categoryId = (int) $this->getRequest()->getPara...
$productId = (int) $this->getRequest()->getParam...
$specifyOptions = $this->getRequest()->getParam(...
if ($this->getRequest()->isPost() && $this->getR...
$product = $this->_initProduct();
if (!$product) {
return $this->noProductRedirect();
}
if ($specifyOptions) {
$notice = $product->getTypeInstance()->g...
$this->messageManager->addNotice($notice);
}
if ($this->getRequest()->isAjax()) {
$this->getResponse()->representJson(
$this->_objectManager->get('Magento\...
'backUrl' => $this->_redirect->g...
])
);
return;
}
$resultRedirect = $this->resultRedirectFacto...
$resultRedirect->setRefererOrBaseUrl();
return $resultRedirect;
}
$params = new \Magento\Framework\DataObject();
$params->setCategoryId($categoryId);
$params->setSpecifyOptions($specifyOptions);
try {
$page = $this->resultPageFactory->create();
$this->viewHelper->prepareAndRender($page, $...
return $page;
} catch (\Magento\Framework\Exception\NoSuchEnti...
return $this->noProductRedirect();
} catch (\Exception $e) {
$this->_objectManager->get('Psr\Log\LoggerIn...
$resultForward = $this->resultForwardFactory...
$resultForward->forward('noroute');
return $resultForward;
}
}
}
商品詳細ページ左上に I am in Local Controller と表示され...
参考
-http://magento.stackexchange.com/questions/86497/how-to-...
-http://magento.stackexchange.com/questions/113511/how-to...
*AbstractControllerの書き換え [#y5d02377]
上記のextendsではどうやっても動作しませんでした。
以下のように記述することで書き換えできました。
app/etc/NonComposerComponentRegistration.php
$pathList[] = dirname(__DIR__) . '/etc/ClassReplacer.php';
app/etc/ClassReplacer.php
<?php
class ClassReplacer
{
public function includeReplacedFiles($src)
{
try {
$replacedFiles = $this->listDir($src, false,...
foreach ($replacedFiles as $replacedFile) {
include_once $src . $replacedFile;
}
} catch (Exception $e) {
return;
}
}
protected function listDir($dir, $prependDir = false...
{
if (!is_dir($dir)) {
return array();
}
$currPath = $prependDir ? $dir : $currPath;
$currPath = $currPath !== '' ? rtrim($currPath, ...
$files = array();
foreach (scandir($dir) as $file) {
if (in_array($file, array('.', '..'))) {
continue;
}
$entity = $currPath . $file;
if ($recursive && is_dir("$dir/$file")) {
$files = array_merge($files, $this->list...
continue;
}
if ($entityRegexp && !preg_match($entityRege...
$files[] = $entity;
}
return $files;
}
}
$replace = new ClassReplacer();
$replace->includeReplacedFiles(dirname(__DIR__) . '/code...
以下のファイルの動作を変更したい場合、~
vendor\magento\module-sales\Controller\AbstractController...
以下へコピー~
app\code\Magento\Sales\Controller\AbstractController\Reor...
// return $resultRedirect->setPath('checkout/cart');
return $resultRedirect->setPath('checkout');
これで一つカートの画面を飛ばすことができました。
参考~
http://magento.stackexchange.com/questions/108001/overrid...
ページ名: