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;
}
  • then why do you create blocks and not models, or helpers, or anything else that's not a model, block or helper? – Marius Nov 15 '16 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? – Bartosz Kubicki Nov 15 '16 at 15:32
  • you should offer an example to make it easier to understand. – Marius Nov 15 '16 at 15:33
  • I've updated my question. – Bartosz Kubicki Nov 15 '16 at 15:38
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.

  • 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? – Bartosz Kubicki Nov 15 '16 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? – tjons Nov 18 '16 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? – Bartosz Kubicki Nov 18 '16 at 14:43
  • It's probably the right thing to do. In my mind, a block shouldn't need another block... – tjons Nov 18 '16 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? – Bartosz Kubicki Nov 18 '16 at 14:56

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.