cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2.1.2 trying to get a link to show up on My Account page

SOLVED

Magento 2.1.2 trying to get a link to show up on My Account page

I followed https://webkul.com/blog/account-navigation-link-magento2/ and yet I can't get my  link to a custom page in My Account (I'd like it underneath Wishlist)  to show up. I've cleared every cache, any idea as to why it might not show up?

12 REPLIES

Re: Magento 2.1.2 trying to get a link to show up on My Account page

Are you adding into theme or module?

 

If in module make sure you have installed that module.

 

If you are adding into theme make sure theme type is not virtual.

Re: Magento 2.1.2 trying to get a link to show up on My Account page

Adding it in module. The module is installed. It still doesn't show up.

Re: Magento 2.1.2 trying to get a link to show up on My Account page

@zuzikaiser Please show your code file of a module.

If Issue Solved, Click Kudos/Accept As solutions.

Re: Magento 2.1.2 trying to get a link to show up on My Account page

app/code/[Vendor]/[ModuleName]/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="[Vendor]_[ModuleName]" setup_version="0.0.1" active="true"/>
</config>

 

app/code/[Vendor]/[ModuleName]/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    '[Vendor]_[ModuleName]',
    __DIR__
);

app/code/[Vendor]/[ModuleName]/etc/frontend/routes.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="[modulename]" frontName="[modulename]">
            <module name="[Vendor]_[ModuleName]"/>
        </route>
    </router>
</config>

app/code/[Vendor]/[ModuleName]/Controller/Index/Index.php

<?php

namespace [Vendor]\[ModuleName]\Controller\Index;

use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Action\Context;

class Index extends \Magento\Framework\App\Action\Action
{

    protected $_resultPageFactory;

    public function __construct(Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory)
    {
        $this->_resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    public function execute()
    {

        $resultPage = $this->_resultPageFactory->create();

        return $resultPage;

   }
}

app/code/[Vendor]/[ModuleName]/view/frontend/layout/[modulename]_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd" layout="2columns-left">
    <update handle="customer_account"/>
      <referenceBlock name="page.main.title">
        <action method="setPageTitle">
            <argument translate="true" name="title" xsi:type="string">[My Title]</argument>
        </action>
      </referenceBlock>
         <referenceContainer name="page.messages">
            <block class="Magento\Framework\View\Element\Messages" name="messages" as="messages"/>
        </referenceContainer>
        <referenceContainer name="content">
            <block class="[Vendor]\[ModuleName]\Block\MyBlock" name="[modulename]_[modulename]_index.view" template="[Vendor]_[ModuleName]::index.phtml"/>
        </referenceContainer>
</page>

app/code/[Vendor]/[ModuleName]/Block/MyBlock.php

<?php
namespace [Vendor]\[ModuleName]\Block;

class MyBlock extends \Magen
    public $_storeManto\Framework\View\Element\Template
{ager;
    /**
     * Construct
     *
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param array $data
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context, 
        \Magento\Store\Model\StoreManagerInterface $storeManager
    )
    {
        $this->_storeManager=$storeManager;
        parent::__construct($context);
    }

    public function _prepareLayout()
    {
        $this->pageConfig->getTitle()->set(__('My Block'));

        return parent::_prepareLayout();
    }
}

app/code/[Vendor]/[ModuleName]/view/frontend/templates/index.phtml

Hello World

app/code/[Vendor]/[ModuleName]/view/frontend/layout/customer_account.xml

<?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>
        <referenceBlock name="customer_account_navigation">
            <block class="Magento\Framework\View\Element\Html\Link\Current" name="customer_account_my_link">
                <arguments>
                    <argument name="path" xsi:type="string">[modulename]/index/index</argument>
                    <argument name="label" xsi:type="string">My Link</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

Re: Magento 2.1.2 trying to get a link to show up on My Account page

@zuzikaiser First of all, Your block file extends structure is missing. may be you have to miss extends class in Block php of above code.

 

 

class MyBlock extends \Magen
    public $_storeManto\Framework\View\Element\Template
{ager;
    

You need to define proper extends class for Block file.

 

Second Point,

In your customer_account.xml file,

Just pass path as your frontname in your case, [modulename]

<?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>
        <referenceBlock name="customer_account_navigation">
            <block class="Magento\Framework\View\Element\Html\Link\Current" name="customer-account-navigation-my-link">
                <arguments>
                    <argument name="path" xsi:type="string">[modulename]</argument>
                    <argument name="label" xsi:type="string">My Link</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

Third Point,

In your, 

app/code/[Vendor]/[ModuleName]/view/frontend/layout/[modulename]_index_index.xml file You  missed Body tag.

 

<?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" layout="2columns-left">
	<update handle="customer_account"/>
   <body>
      <referenceBlock name="page.main.title">
        <action method="setPageTitle">
            <argument translate="true" name="title" xsi:type="string">[My Title]</argument>
        </action>
      </referenceBlock>
         <referenceContainer name="page.messages">
            <block class="Magento\Framework\View\Element\Messages" name="messages" as="messages"/>
        </referenceContainer>
        <referenceContainer name="content">
            <block class="[Vendor]\[ModuleName]\Block\MyBlock" name="[modulename]_[modulename]_index.view" template="[Vendor]_[ModuleName]::index.phtml"/>
        </referenceContainer>
  </body>
</page>

Remove var folder and clear cache.

 

Click Accept As Solution, Answer is useful. Consider Kudos if helpful.

If Issue Solved, Click Kudos/Accept As solutions.

Re: Magento 2.1.2 trying to get a link to show up on My Account page

Thank you for answering. 

The Block class got messed up in copying. It's actually:

class MyBlock extends \Magento\Framework\View\Element\Template
{
    public $_storeManager;

For the customer_account.xml path I have:

 <argument name="path" xsi:type="string">[modulename]/index/index</argument>

Is that correct?

I added the body tag back in, I had it before and had taken it out.

 

I did: 

 rm -r var/generation/*

 rm -r var/di/*

 rm -r var/cache/*

 rm -r var/page_cache/*

 rm -r var/view_preprocessed/*

and

bin/magento cache:flush

 

Is that what you meant? Sorry, I'm a total beginner with magento.

 

Then I refreshed, the link still doesn't show.

Re: Magento 2.1.2 trying to get a link to show up on My Account page

@zuzikaiser Can you show your module files repeat? So if any hint is possible then i will look up it.

If Issue Solved, Click Kudos/Accept As solutions.

Re: Magento 2.1.2 trying to get a link to show up on My Account page

app/code/[Vendor]/[ModuleName]/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="[Vendor]_[ModuleName]" setup_version="0.0.1" active="true"/>
</config>

app/code/[Vendor]/[ModuleName]/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    '[Vendor]_[ModuleName]',
    __DIR__
);

app/code/[Vendor]/[ModuleName]/etc/frontend/routes.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="[modulename]" frontName="[modulename]">
            <module name="[Vendor]_[ModuleName]"/>
        </route>
    </router>
</config>

app/code/[Vendor]/[ModuleName]/Controller/Index/Index.php

<?php

namespace [Vendor]\[ModuleName]\Controller\Index;

use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Action\Context;

class Index extends \Magento\Framework\App\Action\Action
{

    protected $_resultPageFactory;

    public function __construct(Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory)
    {
        $this->_resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    public function execute()
    {

        $resultPage = $this->_resultPageFactory->create();

        return $resultPage;

   }
}

app/code/[Vendor]/[ModuleName]/view/frontend/layout/[modulename]_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd" layout="2columns-left">
    <update handle="customer_account"/>
	 <body>
      <referenceBlock name="page.main.title">
        <action method="setPageTitle">
            <argument translate="true" name="title" xsi:type="string">[My Title]</argument>
        </action>
      </referenceBlock>
         <referenceContainer name="page.messages">
            <block class="Magento\Framework\View\Element\Messages" name="messages" as="messages"/>
        </referenceContainer>
        <referenceContainer name="content">
            <block class="[Vendor]\[ModuleName]\Block\MyBlock" name="[modulename]_index_index.view" template="[Vendor]_[ModuleName]::index.phtml"/>
        </referenceContainer>
	</body>
</page>

app/code/[Vendor]/[ModuleName]/Block/MyBlock.php

<?php
namespace [Vendor]\[ModuleName]\Block;

class MyBlock extends \Magento\Framework\View\Element\Template
{
    public $_storeManager;
    /**
     * Construct
     *
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param array $data
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context, 
        \Magento\Store\Model\StoreManagerInterface $storeManager
    )
    {
        $this->_storeManager=$storeManager;
        parent::__construct($context);
    }

    public function _prepareLayout()
    {
        $this->pageConfig->getTitle()->set(__('My Block'));

        return parent::_prepareLayout();
    }
}

app/code/[Vendor]/[ModuleName]/view/frontend/templates/index.phtml

Hello World

app/code/[Vendor]/[ModuleName]/view/frontend/layout/customer_account.xml

<?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>
        <referenceBlock name="customer_account_navigation">
            <block class="Magento\Framework\View\Element\Html\Link\Current" name="customer_account_my_link">
                <arguments>
                    <argument name="path" xsi:type="string">[modulename]/index/index</argument>
                    <argument name="label" xsi:type="string">My Link</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

Re: Magento 2.1.2 trying to get a link to show up on My Account page

@zuzikaiser You have to just keep below code in your customer_account.xml file,

 

 

<?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>
        <referenceBlock name="customer_account_navigation">
            <block class="Magento\Framework\View\Element\Html\Link\Current" name="customer_account_my_link" after="customer-account-navigation-wish-list-link">
                <arguments>
                    <argument name="path" xsi:type="string">[modulename]</argument>
                    <argument name="label" xsi:type="string">My Link</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

 

 

Remove index/index from path in above code.

Run command, 

php bin/magento setup:upgrade

I have just try your module in my system and its working.

 

If response was helpful to you,consider giving kudos to the post.
If issue solved, click Accept as Solution.

 

If Issue Solved, Click Kudos/Accept As solutions.