1

Because of lack of knowledge, I currently have many parts using objectManager in my project.But I'm not sure whether I should create a module and create many functions for these various parts running by objectManager. For example, in app/design/frontend/CustomTheme/default/Magento_Catalog/templates/product/list.phtml, I'm using 4 different results just using objectManager.

$_product = $objectManager->create('Magento\Catalog\Model\Product')->load($_item->getId());

$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');

$cartlist = $objectManager->create('\Magento\Checkout\Model\Cart')->getQuote(); 

$context = $ObjectManager->get('Magento\Framework\App\Http\Context');

If I create a module and a Block, is it okay to add 4 different functions in just one Block? In other files, I am going to use this Block class to remove the parts running by objectManager.

4 Answers 4

1

You can create one common module. In that common module, you can create one common block OR you can create Module specific Block like one for Product specific and another for Cart specific.

Also if the same type of functions are coming use in multiple PHTML files in different location. Instead of creating functions in Block, Create one Helper Class for all your common functions to improve code.

Hope this help !!

1

If I have many different parts using objectManager in many different phtml, which is better you think? One common Block or One common Helper class? Thanks.

1
  • Please remove this part "If I have many different ..." and update your post because it is not an answer
    – PЯINCƎ
    Feb 26, 2020 at 8:56
1

It depend of what you want to do in your phtml, if you need that 4 classes in your phtml, you need to create 4 functions in the block associated with that phtml, then your get them like this $block->getMyFunction1(), $block->getMyFunction2() etc...

If you just need one function that require the other classes, so you have to juste create one function then you get it in your phtml like the above example.

Between create that functions in your block or helper, personally I prefer to create them directly in the block, this avoids importing them from helper and injecting them into block because it's not good to get functions direclty from helper into phtml, all functions added in phtml, must come from block.

Hope that this will help you.

2
  • Thanks. Is it okay to call the function in phtml like this? $_product = $block->getLayout()->createBlock('Vendor\ObjectManagerBridge\Block\Catalog\Product\ListProduct')->getProductById($_item->getId());
    – John
    Feb 26, 2020 at 9:38
  • 1
    If your phtml is not associated to that block, yes you can do it like this
    – PЯINCƎ
    Feb 26, 2020 at 9:44
0

No need to create new module. You can use the view models for that. Check the given link to know When to use? & How to write? : https://devdocs.magento.com/guides/v2.3/extension-dev-guide/view-models.html

Hope this helps!

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.