4

I am having a situation where I need to instantiate a helper object from my custom module in list.phtml file. For this I have two options either I need to overrride productlist.php and pass the helper class in constructor or use object manager. And I am not satisfied from both of the options.. Can anyone suggest me how can I pass this argument in constructor and use it in my phtml file ? I have read a lot about virtual types and type arguments in di.xml Any help in doing this?

3
  • 1
    you can try this $_helper = $this->helper('{your custom module Vendor}\{Custome module name}\Helper\{Class name which is in Helper folder}');
    – kunj
    Jun 22, 2018 at 5:38
  • Thanks, this worked for me $this->helper() is available to use.. Can you also guide me if it is possible to use di.xml in this case?
    – Vivek
    Jun 22, 2018 at 5:41
  • If you want to access helper in PHTML file then di.xml file not require.
    – kunj
    Jun 22, 2018 at 6:03

2 Answers 2

4

You can try using ViewModel. The view model is injected into the block via layout XML.
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>.
You can go through following articles for more details :

1
  • 1
    Perfect.. this is what I am looking for. magento do offer us a lot of options the only thing is we don't know where and how to use them :P
    – Vivek
    Jun 22, 2018 at 6:30
2

Try this:

$_helper = $this->helper('{your custom module Vendor}\{Custome module name}\Helper\{Class name which is in Helper folder}');
1
  • Although this work but I was not looking for this.. Thanks anyway :)
    – Vivek
    Jun 22, 2018 at 6:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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