1

I can't figure it out how to add links dynamically to customer navigation.

this is my xml:

<referenceBlock name="customer_account_navigation">
   <block class="Vendor\Module\Block\Custom\Link\" name="block">
       <action method="addCustomLink">
        <!--  HERE DYNAMICALLY NEEDS TO ADD ARGUMENTS e.g label, path etc..-->
       </action>
    </block>
</referenceContainer>

Block:

class Link extends \Magento\Framework\View\Element\Html\Link\Current
{
  public function addCustomLink() 
   {
    $parent = $this->getParentBlock();

            $parent->addLink(
                'Dynamic Label goes here',
                'Dynamic Url goes here',
                'Dynamic title goes here'
         );
   }
}

This is giving me error: Invalid method Magento\Customer\Block\Account\Navigation::addLink .

Anybody have any suggestion how could one do this?

Thanks in advance.

enter image description here

1
  • Prakash Thapa this worked perfectly for me no need anything else in block protected function _toHtml() { return '<li class="nav item"><a href="test">TEST</a></li>'; } Commented Aug 9, 2018 at 13:24

1 Answer 1

2

In app/code/VENDOR/MODULE/view/frontend/layout/customer_account.xml you can add following line:

<referenceBlock name="customer_account_navigation">
            <block class="Vendor\Module\Block\Custom\Link"
                   name="test-customer-account-navigation-link">
                <arguments>
                    <argument name="label" xsi:type="string" translate="true">DYNAMIC TEXT NED TO DISPLAY</argument>
                    <argument name="path" xsi:type="string">DYNAMIC YOUR_PATH_URL(like: customer/account ) </argument>
                    <argument name="sortOrder" xsi:type="number">240</argument>
                </arguments>
            </block>
        </referenceBlock>

Update

Then,app/code/VENDOR/MODULE/Block/Custom/Link.php

namespace Vendor\Module\Block\Custom;

class Link extends \Magento\Customer\Block\Account\Link
{
    public function setPath($path)
    {
        $this->setPath($path);
    }

    public function setLabel($label)
    {
        $this->setLabel($label);
    }
/** 
 * you can directly change href also like this;
 * if you use getHref() then setPath() will be ignored
 *
 * @return string
 */
 public function getHref()
 {
    return 'https://www.google.com/';
 }

}

It should work.

6
  • this is exactly what I Can't have just read the question label path needs to be dynamic. Commented Aug 9, 2018 at 10:54
  • Thanks That's almost perfect but it fall short as the link doesn't have the css classes nav item is there something else needs to do? Commented Aug 9, 2018 at 11:55
  • Sorry I did not understand your question. Thanks Commented Aug 9, 2018 at 11:56
  • Sorry if that wans't clear I've update my question with screen shoot. Commented Aug 9, 2018 at 12:01
  • 1
    Please add your label with $this->setLabel("HERE IS YOUR LABEL"); Commented Aug 13, 2018 at 6:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.