1

I have been trying to over-ride my customer module in magento 2. I need to Add an icon to 'Sign In' on header link. I have copied

vendor/magento/module-customer/Block/Account/AuthorizationLink.php

and pasted to

app/code/Test/Customer/Account/Block/Account

, and simple changed 'Sign In' text to 'Sign In2'. And then created

app/code/Test/Customer/etc/di.xml

. And I have added the following codes there.

<?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" type="Test\Customer\Block\Account" />
</config>

After that I have run the following commands

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy
php bin/magento cache:clean

And it doesn't seems to be working. Can anyone please help me?

  • could you show Account.php block file of your module? – Sameer Bhayani Oct 16 '17 at 6:50
  • Your Block is at wrong place put it at this location app/code/Vendor/Module/Block/Account Where Account is your Block name & save it with Account.php – Abhishek Tripathi Oct 16 '17 at 7:07
0

2 things:

  • I'm assuming you also have a registration.php, module.xml and performed a bin/magento module:enable Test_Customer
  • You use type="Test\Customer\Block\Account", but your path is Test/Customer/Account/Block/Account, so you should move your class to Test/Customer/Block/Account (and adjust namespace accordingly).

By the way: if you just want to add an icon, a override might be the last thing you want to do. 'rewrites' are meant to be a last resort if there is no other way to achieve your goal (like for example, template overriding, DI or translation files).

In your specific example you're better of by just copying the template file vendor/magento/module-customer/view/frontend/templates/account/link/authorization.phtml to your own theme, since that's the template that's responsible for rendering the link you're trying to add an icon to.

0

Your di.xml file is wrong. Change your code as below

<?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="Test\Customer\Block\Account\AuthorizationLink" />
</config>

Now create

app/code/Test/Customer/Block/Account/AuthorizationLink.php

and put your code in this file

0

Test/Customer/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="Test_Customer" setup_version="1.0.0" schema_version="1.0.0">
    </module>
</config>

Test/Customer/registration.php

<?php

    \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'Test_Customer',
        __DIR__
    );

Test/Customer/Block/Account/AuthorizationLink.php

    <?php

    namespace Test\Customer\Block\Account;

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


        class AuthorizationLink extends \Magento\Framework\View\Element\Html\Link implements SortLinkInterface
    {

        /**
         * 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 In2');
        }

        /**
         * 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 100.2.0
         */
        public function getSortOrder()
        {
            return $this->getData(self::SORT_ORDER);
        }
    }

Test/Customer/etc/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" type="Test\Customer\Block\Account" />
      </config>

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.