1
  • 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.

1 Answer 1

0

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

2
  • Thank you very much, that makes sense, we are doing an upgrade of one of our sites and I am responsible for the changes that need be to move from 2.4.2-p1 to 2.4.4 and I only get the errors from my team lead and he confirmed that he got this error from an Upgrade Compatibility Tool
    – Mabonito
    Oct 10, 2022 at 9:42
  • You're welcome.
    – Tu Van
    Oct 10, 2022 at 9:55

Your Answer

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

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