0

I often create blocks containing PHP methods, not binded with any template by design. They are sometimes used only in one place, sometimes throughout an app. So far, I was making them inherit from \Magento\Framework\View\Element\Template, but I am not sure if it is the best way. Maybe there is some other abstract block class most suitable for such a 'php-only' blocks?

example - this one is used on product pages in order to check if provided product is in cart already (it has no template assigned):

namespace [Vendor]\[ModuleName]\Block\Catalog\Product;
use \Magento\Catalog\Api\Data\ProductInterface;
use \Magento\Checkout\Model\Session;
use \Magento\Framework\View\Element\Template;
use \Magento\Framework\View\Element\Template\Context;
class IsInCart extends Template
{
private $checkoutSession;
public function __construct(
 Session $checkoutSession,
 Context $context,
 array $data = []
)
{
 parent::__construct($context, $data);
 $this->checkoutSession = $checkoutSession;
}
public function isInCart(ProductInterface $product)
{
 $productId = $product->getId();
 $cartItems = $this->checkoutSession->getQuote()->getAllVisibleItems();
 $itemsIds = array();
 foreach ($cartItems as $cartItem) {
 $itemsIds[] = $cartItem->getProduct()->getId();
 }
 return in_array($productId, $itemsIds);
}
public function countItemsInCart()
{
 $cartItems = $this->checkoutSession->getQuote()->getAllVisibleItems();
 $cartItemsCount = count($cartItems);
 return $cartItemsCount;
}
asked Nov 15, 2016 at 15:20
4
  • then why do you create blocks and not models, or helpers, or anything else that's not a model, block or helper? Commented Nov 15, 2016 at 15:25
  • For example - block checks if product is already bought and returns a value to render. I understand that is part of view layer. What is more blocks can be easily added by layout xml. Is it incorrect? Commented Nov 15, 2016 at 15:32
  • you should offer an example to make it easier to understand. Commented Nov 15, 2016 at 15:33
  • I've updated my question. Commented Nov 15, 2016 at 15:38

1 Answer 1

2

The right block to extend in the case of a block that does not render anything is Magento\Framework\View\Element\AbstractBlock. However, blocks that do not render anything are somewhat useless, and you would probably be better served to move that functionality into another block, or a model or helper file.

answered Nov 15, 2016 at 20:50
5
  • Interesting point - +1 for AbstractBlock. Models in my opinion, should not be used directly inside phtml. Results of my block's methods are displayed inside phtml, but my block doesn't have any own template to do it ( because it would be one string f.e. or results are used to make 'ifs' in templates, and are not displayed at all). Throught magento articles, helpers are discouraged to use, as not fitting anywhere. So in my case - wouln't be the best - if I want show on frontend f.e. if product is in cart/wishlist etc. to use block? Commented Nov 15, 2016 at 22:17
  • @lord_of_strings Thanks for accepting. Absolutely, you shouldn't use models inside phtml files. I'm not quite sure though, what you mean, as blocks should access models - so perhaps you refactor this logic into a model and then access it in your other blocks? Commented Nov 18, 2016 at 14:32
  • I am not sure - actually such a block is quite lightweight. Do you think it is really neccessary to refractor it into model and make block logic even more thin? Commented Nov 18, 2016 at 14:43
  • It's probably the right thing to do. In my mind, a block shouldn't need another block... Commented Nov 18, 2016 at 14:45
  • This is really understandable point when you build completely new module with new entity. But the way I used block - it is good way to add some functionality/display some data etc. without adding any preference/plugin on any model class. That way I 'add' more methods to model without touching them. They can be easly plug in another templates, not modyfing nativa Magento classes. If I would like to add such a methods I would have to make a preference on native blocks or models. What do you think? Commented Nov 18, 2016 at 14:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.