cancel
Showing results for 
Search instead for 
Did you mean: 

Error: Overriding a block in Magento 2

Error: Overriding a block in Magento 2

I want to override some lines in the "vendor/magento/module-customer/Block/Account/Magento\Customer\Block\Account\AuthorizationLink.php".

 

So I created a extension structure "app/code/Sample/Mycode":

 

/etc/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\AuthorizationLink" type="Sample\Mycode\Block\Account\AuthorizationLink" /> </config>

/etc/module.xml

 

 

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Sample_Mycode" setup_version="0.0.1"/>
</config>

/Block/Account/AuthorizationLink.php

 

<?php

namespace Sample\Mycode\Block;
use Magento\Customer\Model\Context;
class AuthorizationLink extends \Magento\Customer\Block\Account\AuthorizationLink
{
    public function getLabel()
    {
        return $this->isLoggedIn() ? __('Hello World') : __('Hello World 2');
    }
    protected function _toHtml()
    {
        $this->setModuleName($this->extractModuleName('Magento\Customer\Block\Account\AuthorizationLink'));
        return parent::_toHtml();
    }
}

 

 

Unfortunately I got an error on my request. The logs are less revealing.

ICan anyone see my error? I appreciate any help.

 

1 REPLY 1

Re: Error: Overriding a block in Magento 2

If you need to modify only "some lines" then use plugins, not preferences.

With preferences you will break (or will be broken by) other weak extensions which are trying to set a preference for the same core class.