2

I'm playing overriding the blocks in magento 2, extended block constructor is calling but inside method is not working?

my code is

di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <preference for="Magento\Customer\Block\Account\Dashboard\Info" type="Learning\Customers\Block\Info" />
</config>

Info.php

  <?php

namespace Learning\Customers\Block;

use Magento\Framework\View\Element\Template;

class Info extends \Magento\Customer\Block\Account\Dashboard\Info
{

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
        \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
        \Magento\Customer\Helper\View $helperView,
        array $data = []
    ) {

        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/Info.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info($this->getName());

        parent::__construct($context,$currentCustomer,$subscriberFactory,$helperView, $data);
    }

    public function getName()
    {
        return 'Magento 2';
    }

}

Any suggestions?

Thanks.

  • what do you mean by "not working"? what happens when you try it? – Marius Aug 4 '15 at 14:22
  • @Marius, constructor() is calling, but getName() method is not overriding i.e., not displaying Magento 2 – Manoj Kumar Aug 4 '15 at 14:24
  • @Marius any help on this – Manoj Kumar Aug 5 '15 at 5:10
4

add below method to Info.php

protected function _toHtml()
    {
        $this->setModuleName($this->extractModuleName('Magento\Customer\Block\Account\Dashboard\Info'));
        return parent::_toHtml();
    }

see reference Link How to Overriding block in Magento 2

0

Overriding Block is done but issue is when i get current category id using registry in magento 2 showing error

There has been an error processing your request.
Object DOMDocument should be created

Any one know how can i get current category id in Toolbar block.

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.