Domanda

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?

È stato utile?

Soluzione

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

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top