Magento 2 – _prepareLayout() Declared in Template Instead of AbstractBlock

magento2

  • Possible dependency on implementation details. Usage of 'Magento\Framework\View\Element\AbstractBlock::_prepareLayout' should be declared instead of 'Magento\Framework\View\Element\Template::_prepareLayout'

  • This is the error that I get, may I please get a possible solution to this.

  • my class is as follows :

class AddressBook extends Template
{
  • then there are bunch of declarations there then for my __construct() I have the following
    /**
     * @param Context $context
     * @param Session $customerSession
     */
    public function __construct(
        Template\Context $context, 
        Session $customerSession, 
        LoggerInterface $logger, 
        CollectionFactory $countries,
        AddressRepositoryInterface $addressRepository, 
        B2BManInterface $companyRepository,
        AddressRepository $addressRepository2,
        Http $http)
    {
        $this->customerSession = $customerSession;
        $this->addressRepository = $addressRepository;
        $this->companyRepository = $companyRepository;
        $this->logger = $logger;
        $this->countries = $countries;
        $this->admin = false;
        parent::__construct($context);
        $this->addressRepository2 = $addressRepository2;
        $this->http = $http;
    }

from my method that I am overriding which is _prepareLayout() I have this :

    {
        parent::_prepareLayout();
        $this->pageConfig->getTitle()->set(__('Company Address AddressBook'));
        $shouldSetShowPage = !strpos($_SERVER['REQUEST_URI'], 'company/users/create/')?true:false;
        if ($this->getAddressCollection()) {
            try {
                $pager = $this->getLayout()->createBlock(
                    'Magento\Theme\Block\Html\Pager',
                    'company.address.book.pager'
                )->setAvailableLimit([5 => 5, 10 => 10, 15 => 15, 20 => 20])
                    ->setShowPerPage($shouldSetShowPage)->setCollection(
                        $this->getAddressCollection()
                    );
            } catch (LocalizedException $e) {
                $this->logger->critical($e->getMessage());
            }
            $this->setChild('pager', $pager);
            $this->getAddressCollection()->load();
        }
        return $this;
    }
  • is there a possible way to call Magento\Framework\View\Element\AbstractBlock::_prepareLayout() rather than Magento\Framework\View\Element\Template::_prepareLayout.

Thank you in advance.

Best Answer

Yes, you can call Magento\Framework\View\Element\AbstractBlock::_prepareLayout() by: \Magento\Framework\View\Element\AbstractBlock::_prepareLayout();

But it is not a good idea because if the Magento\Framework\View\Element\Template class declares _prepareLayout method in the future, your code won't be working as expected.

Looks like you faced this error when running the Upgrade Compatibility Tool? If yes, you can ignore this error because magento core (codebase) implements the same way in many files, that is call parent::_prepareLayout() from the class that extends Magento\Framework\View\Element\Template, eg https://github.com/magento/magento2/blob/2.4.5/app/code/Magento/Customer/Block/Address/Grid.php#L70

Btw, you shouldn't use PHP superglobals ($_SERVER) to safely access values. https://devdocs.magento.com/guides/v2.4/ext-best-practices/extension-coding/security-performance-data-bp.html#use-wrappers-instead-of-superglobal-variables

Related Topic