Last active
January 7, 2022 16:03
-
-
Save 0-Sony/48b3f4e17d1bc548f0ae7406f2da9620 to your computer and use it in GitHub Desktop.
Magento2: Add ViewModel by using plugin
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 | |
declare(strict_types=1); | |
namespace MyNamespace\MyModule\Plugin; | |
use Vendor\Module\Block\Any\Block as AnyBlock; | |
/** | |
* Class AddViewModelToBlock | |
*/ | |
class AddViewModelToBlock | |
{ | |
/** | |
* Description $myViewModel | |
* | |
* @var MyViewModel $myViewModel | |
*/ | |
protected MyViewModel $myViewModel; | |
/** | |
* AddViewModelToBlock constructor | |
* | |
* @param MyViewModel $myViewModel | |
*/ | |
public function __construct( | |
MyViewModel $myViewModel | |
) { | |
$this->myViewModel = $myViewModel; | |
} | |
/** | |
* Description beforeToHtml function | |
* | |
* @param AnyBlock $subject | |
* | |
* @return void | |
*/ | |
public function beforeToHtml(AnyBlock $subject): void | |
{ | |
$subject->setData('view_model', $this->myViewModel); | |
} | |
} |
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
<?xml version="1.0"?> | |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | |
<type name="Vendor\Module\Block\Any\Block"> | |
<plugin name="my_custom_plugin_name" type="MyNamespace\MyModule\Plugin\AddViewModelToBlock"/> | |
</type> | |
</config> |
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 | |
declare(strict_types=1); | |
namespace MyNamespace\MyModule\ViewModel; | |
use Magento\Framework\View\Element\Block\ArgumentInterface; | |
/** | |
* Class MyViewModel | |
* | |
* @package MyNamespace\MyModule\ViewModel | |
*/ | |
class MyViewModel implements ArgumentInterface | |
{ | |
/** | |
* Description doSomething function | |
* | |
* @return string | |
*/ | |
public function doSomething(): string | |
{ | |
return 'something'; | |
} | |
} |
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 | |
// Vendor\Module\Block\Any\Block::template.phtml | |
/** @var Vendor\Module\Block\Any\Block $block */ | |
/** @var \MyNamespace\MyModule\ViewModel\MyViewModel $viewModel */ | |
$viewModel = $block->getData('view_model'); | |
echo $viewModel->doSomething(); //return 'something' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment