0

So i am trying to override the module-customer -> Block -> Account -> AuthorizationLink.php

What i did is:

I made a folder Customer in:

app -> code -> Theme -> Customer

in the Customer Folder there is 2 more folders: etc and Block/Account

in the etc folder there is xml called di

di.xml:-

 <?xml version="1.0"?>

 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Customer\Block\Account\AuthorizationLink" type="AXAStudios\Customer\Block\AuthorizationLink" />
</config>

and in my Block/Account i have a file called: AuthorizationLink.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace AXAStudios\Customer\Block\Account;

use Magento\Customer\Model\Context;
use Magento\Customer\Block\Account\SortLinkInterface;

/**
 * Customer authorization link
 *
 * @api
 * @SuppressWarnings(PHPMD.DepthOfInheritance)
 * @since 100.0.2
 */
class AuthorizationLink extends \Magento\Customer\Block\Account\AuthorizationLink
{
    /**
     * Customer session
     *
     * @var \Magento\Framework\App\Http\Context
     */
    protected $httpContext;

    /**
     * @var \Magento\Customer\Model\Url
     */
    protected $_customerUrl;

    /**
     * @var \Magento\Framework\Data\Helper\PostHelper
     */
    protected $_postDataHelper;

    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Framework\App\Http\Context $httpContext
     * @param \Magento\Customer\Model\Url $customerUrl
     * @param \Magento\Framework\Data\Helper\PostHelper $postDataHelper
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\App\Http\Context $httpContext,
        \Magento\Customer\Model\Url $customerUrl,
        \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->httpContext = $httpContext;
        $this->_customerUrl = $customerUrl;
        $this->_postDataHelper = $postDataHelper;
    }

    /**
     * @return string
     */
    public function getHref()
    {
        return $this->isLoggedIn()
            ? $this->_customerUrl->getLogoutUrl()
            : $this->_customerUrl->getLoginUrl();
    }

    /**
     * @return string
     */
    public function getLabel()
    {
        return $this->isLoggedIn() ? __('Sign Out') : __('Sign In');
    }

     /**
     * @return string
     */
    public function getLabelOrder() {
        return __('My Orders');
    }

    /**
     * Retrieve params for post request
     *
     * @return string
     */
    public function getPostParams()
    {
        return $this->_postDataHelper->getPostData($this->getHref());
    }

    /**
     * Is logged in
     *
     * @return bool
     */
    public function isLoggedIn()
    {
        return $this->httpContext->getValue(Context::CONTEXT_AUTH);
    }

    /**
     * {@inheritdoc}
     * @since 101.0.0
     */
    public function getSortOrder()
    {
        return $this->getData(self::SORT_ORDER);
    }
}

But whenever i upload it to the server and flush cache there is nothing that changed.

so any help?

Thanks.

  • Did you try generating the classes by setup:di:compile? – Mathanagopal S Mar 21 at 13:53
  • You are saying that you create a folder structure of app/code/Theme/Customer but in the preference, you are specifying the type to be AXAStudios\Customer\Block\AuthorizationLink. What is the name of the module that you are creating? Please check it and provide the correct implementations in the question. – Mathanagopal S Mar 21 at 13:58
  • @MathanagopalS in app\code i've made a folder Customer in the Customer folder there is 2 more folders: Block and etc. In the Block file i have a file that i want to override from the vendor and that is the: AuthorizationLink.php from the module-customer. In the etc i have 2 files di.xml and module.xml. But i have no idea to do this and if i am working in the right folder – TehGaz7 Mar 23 at 11:29
  • @MathanagopalS Thanks, i placed this in mijn app -> code. Is this the right way or do i have to place this in my app -> design -> frontend -> AXAStudios -> axadesign -> my current theme – TehGaz7 Mar 25 at 9:41
0

Check this out - https://github.com/Mathanagopal97/Override-block-not-working.

It seems that for overriding a block, we have to override the combination of block.php, block's xml and block's template file.

| improve this answer | |
  • Thankyou!, Got 1 more questions can i also add my own functions to the block i override? because i only apply's to functions that already exist as an example i want to make a function called: getLabelOrder where it returns 'My Orders' as an example – TehGaz7 Mar 27 at 10:02
  • Yes, you can add your own methods if you want. But, then you have to call that either in the template file of yours or in some other method that you are overriding. The concept is simple and all basic rules of inheritance will apply here. – Mathanagopal S Mar 27 at 10:11
  • Alright thanks. I did but it did not work like i searched up or tried on my manners maybe if you can add this to your github repository quick. Man you saved my life hahah thankyou! – TehGaz7 Mar 27 at 10:26
  • Haha, I have added the code to my repository. Check it out. – Mathanagopal S Mar 27 at 10:44

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.