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":




<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd"> <preference for="Magento\Customer\Block\Account\AuthorizationLink" type="Sample\Mycode\Block\Account\AuthorizationLink" /> </config>




<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Sample_Mycode" setup_version="0.0.1"/>




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()
        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.



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.