4

I already add a new link "Demo link" in the Customer Account page, but I still do not know how to add a page to that link enter image description here

I just learned about Magento 2, can you give detail instructions on each step for me

I also created the file index.phtml to control that link but it does not load the page as I did enter image description here

I want it to display like this, sidebar and content beside enter image description here

But when I click on "Demo link" it does not Extend Customer page, exactly **menu sidebar*

1

I will also show how you add/show your custom page when you click on the newly added custom link in the customer account sidebar :

  1. Create your layout app/code/YourNamespace/YourModule/view/frontend/layout/customer_account.xml :
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" 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-demo-link">
            <arguments>
                <argument name="path" xsi:type="string">yourmodule/yourdemolink/index</argument>
                <argument name="label" xsi:type="string">Your Demo Link</argument>
            </arguments>
        </block>
    </referenceBlock>
    </body>
</page>


In the above XML code, you can see that I have defined my custom menu path as:
yourmodule = my module name
yourdemolink = my module’s controller name
index = my controller’s action

2.Create your module’s layout XML file : app/code/YourNamespace/YourModule/view/frontend/layout/yourmodule_yourdemolink_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="customer_account"/>
    <head>
        <title>
            Your Demo Link
        </title>
    </head>
    <body>
        <referenceContainer name="content">
            <block class="YourNamespace\YourModule\Block\YourBlock" name="yourmodule.demolink.index" template="YourNamespace_YourModule::yourdemolink/index.phtml" cacheable="false" />
        </referenceContainer>
    </body>
</page>
  1. Create Controller class file : app/code/YourNamespace/YourModule/Controller/YourDemoLink/Index.php
<?php
namespace YourNamespace\YourModule\Controller\YourDemoLink;

class Index extends \Magento\Framework\App\Action\Action
{
        /**
         * @var \Magento\Framework\View\Result\PageFactory
         */
        protected $resultPageFactory;

        /**
         * @param \Magento\Framework\App\Action\Context $context
         * @param \Magento\Framework\View\Result\PageFactory resultPageFactory
         */
        public function __construct(
            \Magento\Framework\App\Action\Context $context,
            \Magento\Framework\View\Result\PageFactory $resultPageFactory
        )
        {
            $this->resultPageFactory = $resultPageFactory;
            parent::__construct($context);
        }
    /**
     * Default customer account page
     *
     * @return void
     */
    public function execute()
    {
        return $this->resultPageFactory->create();
    }
}
?>
  1. Create Block class file : app/code/YourNamespace/YourModule/Block/YourBlock.php
<?php
namespace YourNamespace\YourModule\Block;
class YourBlock extends \Magento\Framework\View\Element\Template
{        
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        array $data = []
    )
    {        
        parent::__construct($context, $data);
    }

    public function getHelloWorld()
    {
        return 'Hello World';
    }

}
?>
  1. Create Template file : app/code/YourNamespace/YourModule/view/frontend/templates/demolink/index.phtml
<h2>
    <?php echo $block->getHelloWorld(); ?>
</h2>

echo 'My First Link Page';

Hope this help you.

| improve this answer | |
  • Thank for your comment – Kite Hany Jun 17 '19 at 9:58
0

Create a module with name STech_Link by following below steps:

Step 1: Create registration.php under

app/code/STech/Link/registration.php

<?php

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

Step 2: Create module.xml under

app/code/STech/Link/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="STech_Link" setup_version="0.0.1">
        <sequence>
            <module name="Magento_Sales"/>
            <module name="Magento_Quote"/>
            <module name="Magento_Checkout"/>
            <module name="Magento_Customer"/>
        </sequence>
    </module>
</config>

Step 3: Create customer_account.xml under

app/code/STech/Link/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="demo-link">
                <arguments>
                    <argument name="path" xsi:type="string">module/controller/action</argument>
                    <argument name="label" xsi:type="string">Demo Link</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

Now you need to create your own controller and action according to your requirement and put that to above path.

Thats it!. Now run setup upgrade and other required commands and check.

| improve this answer | |
0

Can't comment on code... I'm not special enough lol. New magento test install...

The "accepted answer" code does not work for Magento 2.3.5. I copied the code and folder structure exactly as shown in "accepted answer" yet it does not display link. What am i doing wrong. I know that's a very broad question but throwing into the wind. maybe magento 2.3.5 no longer allows this options?

EDIT: the code seems to mess with the top.nav dropdown. no longer clickable. when I remove the code it works fine.

| improve this answer | |
0

The "accepted answer" code seems to not work for Magento 2.3.5. I copied the code and folder structure exactly as shown in "accepted answer". I got the link to display finally by using not accepted answer. but link gos to an opps error page. What am i doing wrong? I know that's a very broad question but throwing into the wind. maybe magento 2.3.5 no longer allows this options? Refer to above code as its an exact copy.

| improve this answer | |

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.