2

I have a template and should write some logic. I have made a view model and I'm calling it from a template. Should I call block methods from the view model? And if so, how can I do it?

  • Yes you can use View model – Aditya Shah Dec 28 '18 at 17:29
  • You just add an additional XML argument to the Block class - either when creating the block using <block> or when referring to the block using <referenceBlock>. – Aditya Shah Dec 28 '18 at 17:29
  • TheKitMurkit, if any answer helps you then you mark accepted to those answer, so it will helpful for future readers, you can mark accept answer by clicking (✓) next to answer. – Aditya Shah Jan 17 at 6:25
  • @AdityaShah unfortunately, these answers weren't useful – TheKitMurkit Jan 17 at 9:12
0

This might look like the following:

<block class="Vendor\Module\Block\Dummy" name="dummy">
    <arguments>
        <argument name="view_model" xsi:type="object">Vendor\Module\ViewModel\Dummy</argument>
    </arguments>
</block>

The newly created class could be used as follows:

namespace Yireo\Example\ViewModel;

class Dummy implements \Magento\Framework\View\Element\Block\ArgumentInterface
{
    public function __construct()
    {
    }
}

In this case, the constructor is still empty. But because the Object Manager is used to instantiate this class, you can modify the constructor to inject new dependencies as you are used to in other classes. You can apply all of the coolness of Magento DI here.

Finally, you can insert the new ViewModel class in your existing PHTML template as follows:

$viewModel = $block->getViewModel();

Reference

  • Thank you, but this information is already available. The question was "Should I call block methods FROM the view model?" – TheKitMurkit Dec 28 '18 at 18:08
  • Yes you can,you can use all block methods in ViewModels in Magento 2.2 – Aditya Shah Dec 28 '18 at 18:15
  • In MVVM parlance, a Magento block object is the View Model. The block object will do any programmatic reading of the CRUD models, request object, external systems, etc. The View is the phtml template file. It only interacts with the ViewModel (the block object). – Aditya Shah Dec 28 '18 at 18:17
0

In MVC Magento, you haven't to call directly the model in your phtml, block is made for this.

So you call your model in block then you call the block in your view (phtml). $block->getSomeFunction();

Good luck.

0

to call a function in your block:

<?php echo $block->getMyData(); ?>

to call from your viewModel:

layout definition example below

<block template="Mbs_PunchHole::resultapi.phtml">
    <arguments>
       <argument name="viewModel" xsi:type="object">Mbs\HolePunch\Block\APIResultViewModel</argument>
       </arguments>
 </block>

template code example below:

<?php $viewModel = $block->getData('viewModel');
echo $viewModel->getMyData(); 

I have a public repo showing an example with ViewModel code: please take a look at https://bitbucket.org/magstaging/punchhole

Using Block like Magento 1 practice works and ViewModel is an option to decouple logic from your block and the template.

ViewModel reduces use of inheritance in your code because ViewModel does not inherit anything

--> basically, if you take over a block, you also inherit from the block and that is not ideal to enhance scalability of your code; in practical terms it is always more maintenance to add/maintain your custom dependency injections when you customise the block

--> the ViewModel implements ArgumentInterface and the latter is an empty interface (just here for the sake of structuring Magento code I understand)

--> An advantage to use ViewModel is that the ViewModel will have a lot less dependencies than the same function in a block.

Now, where there is a real challenge is that most of the Magento 2 code is not using ViewModel and it means it is often makes us writing more code to use ViewModel. So, it may seem ViewModel is a way to improve code maintenance overtime and short term using block is having some benefits to benefit from block property handy like $this->layout, $this->scopeConfig and be consistent with Magento core code

  • Yes, but can I call block from it's view model? – TheKitMurkit Jan 2 at 10:49
  • 1
    you can by using block as parameter of the function. However, it does seem to be not what viewModel is for. You'd have to try and see. Usually, it is when writing tests that it appears obvious what's best.. – Herve Tribouilloy Jan 2 at 10:53
0

In the ViewModel you don’t have direct access to the methods of the block, unless you add the block as a dependency in the constructor of the ViewModel. I’m not sure however if that would be a good practice. Depending on the case, you could always extend the block instead of using a ViewModel if you need to access methods of the original block.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.