0

How to add custom link in customer account navigation?

I am looking for code to show form upon clicking custom link on left navigation.

Can we submit form using custom link?

5

If you want to do sort the link to then follow. Create default.xml in following directory

app/code/SpaceName/ModuleName/view/frontend/layout

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>   

    <referenceBlock name="customer_account_navigation">
                <block class="Magento\Customer\Block\Account\SortLinkInterface" name="test-link">
                    <arguments>
                        <argument name="path" xsi:type="string">test/controller/action</argument>
                        <argument name="label" xsi:type="string">Test Link</argument>
                         <argument name="sortOrder" xsi:type="number">150</argument>
                    </arguments>
                </block>
    </referenceBlock>

  </body>
</page>

Reference

| improve this answer | |
  • Hello Qaisar, How to load content upon clicking link, i can see Test link now in Account navigation. I am trying to load js data tables data from db on clicking it. How that can be done pls? – jafar pinjar Aug 2 '18 at 7:11
  • @jafar you have to add your module code in question so i can help further. – Qaisar Satti Aug 2 '18 at 7:16
  • @Quisar , Which code is need to add ? I used the above code in my default.xml file and i can see link on account navigation now. How can i bring html on right side after clicking the link? – jafar pinjar Aug 2 '18 at 7:18
  • @jafar follow this link to create page. blog.qaisarsatti.com/magento_2/… in layout file just use this handle <update handle="customer_account"/> – Qaisar Satti Aug 2 '18 at 7:22
  • hello Qaisar, where can i use this code <update handle="customer_account"/>? I created the layout file in my module it is loading html content, but it is not on right hand side. how to bring it? – jafar pinjar Aug 2 '18 at 8:49
3

Create a layout file customer_account.xml inside Vendor\Module\view\frontend\layout and put below code.

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="sidebar.main">
                <referenceBlock name="customer_account_navigation">
                    <block class="Vendor\Module\Block\Customer\Link" name="new-customer-link">
                        <arguments>
                            <argument name="label" xsi:type="string" translate="true">new Customer Link</argument>
                            <argument name="path" xsi:type="string">frontname/controller/action</argument>
                            <argument name="custom_form_action" xsi:type="string">kanhaiya5590.com</argument>
                            <argument name="sortOrder" xsi:type="number">110</argument>
                        </arguments>
                    </block>
                </referenceBlock>
        </referenceContainer>
    </body>
</page>

As per question need form to be trigger on click of link or button

namespace Vendor\Module\Block\Customer;

use Magento\Customer\Model\Session as CustomerSession;
use Magento\Framework\App\DefaultPathInterface;
use Magento\Framework\View\Element\Html\Link\Current;
use Magento\Framework\View\Element\Template\Context;

class Link extends Current
{   
    protected $_customerSession;


    public function __construct(
        Context $context,
        DefaultPathInterface $defaultPath,
        CustomerSession $customerSession,
        array $data = []
    )
    {
        $this->_customerSession = $customerSession;       
        parent::__construct($context, $defaultPath, $data);
    }

    protected function _toHtml()
    {
        $html = null;
        if ($this->_customerSession->isLoggedIn()) {
            $html = '<li class="nav item '.($this->isCurrent().?"current": "").'">';
                $html .= '<form action="'.$this->getCustomFormAction().'">'; 
                $html .= '<button>'.$this->escapeHtml((string)new \Magento\Framework\Phrase($this->getLabel())).'</button>';
                $html .= '</form>'; 
            $html .= '</li>';
        }
        return $html;
    }

}

Note : custom_form_action as per your need you can set

if still more help needed please let us know.

| improve this answer | |
  • I am looking for code to add link on leftnav, After clicking a link a form should open in right side. from there i am trying to submit. – jafar pinjar Aug 2 '18 at 7:02
  • ok best to set a modal popup on click and then set you form data in popup – Kanhaiya lal Aug 2 '18 at 7:16
  • Yes, my total requirement is upon clicking link on leftnav, i need to load data table on right side with edit and delete each row, how can load the data after clicking link. please suggest me – jafar pinjar Aug 2 '18 at 7:17
  • as per your reply you want to load data or form in right side area then best approch to use controller only, if your requirement is to show your data in any page of customer account secion then try to implement modal popup – Kanhaiya lal Aug 2 '18 at 7:19
  • 1
    if you satisfied with my share answer or with approach please mark the answer is valid answer.so it will help other those who looking same logic – Kanhaiya lal Aug 2 '18 at 7:23

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.