1

enter image description here

can anyone tell me how to create a custom link ("MY VEHICLE") in the dropdown from username of a logged in user. I am a beginner and have no idea where to start, any help is greatly appretiated

1

Find a block that renders those elements and try this

<referenceContainer name="block-name">
    <block class="Magento\Framework\View\Element\Html\Link\Current" name="home" after="-">
        <arguments>
            <argument name="class" xsi:type="string">custom-link-class</argument>
            <argument name="label" xsi:type="helper" helper="Vendor\Module\Helper\Menu::getVehicleLinkLabel></argument>
            <argument name="path" xsi:type="helper" helper="Vendor\Module\Helper\Menu::getVehicleLinkUrl"/>
        </arguments>
    </block>
</referenceContainer>

then create helper class like

namespace Vendor\Module\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Customer\Model\Session;

class Menu extends AbstractHelper
{
    protected $customerSession;

    public function __construct(Context $context, Session $customerSession)
    {
        $this->customerSession = $customerSession;
        parent::__construct($context);
    }

    public function getVehicleLinkLabel()
    {
        return $this->customerSession->isLoggedIn() ? __("My Vehicle (%1)", $this->customerSession->getCustomer()->getName()) : null;
    }

    public function getVehicleLinkUrl()
    {
        return $this->_urlBuilder->getUrl('link/to/page');
    }
}

Remember that block may be cached by full page cache and block_html so this customer dependent link might require some tweaking to generate correctly for each customer.

  • is there something i need to add along with these, i just tried adding to magento-customer core module (not the best practice, just wanted to see if it will work, and its just a local machine) nothing happened, i didn't change the code in anyway(except for a quote that you seem to be missing), can you tell me if i am doing something wrong? – HKR Mar 20 '17 at 11:12
  • What exactly you've added and to which class? – Zefiryn Mar 20 '17 at 11:20
  • i added the xml part to customer_account_index.xml layout page and created a helper Menu.php – HKR Mar 20 '17 at 11:59
  • Link to customer_account_index.xml, in case you want to see what i did – HKR Mar 20 '17 at 12:08
  • You are missing one </block>, check pastebin.com/Hv7taV3e – Zefiryn Mar 20 '17 at 12:42
0

i got it to work another way, its similar to the codes @Zefiryn gave.

Link.php

namespace [vendor]\[module]\Block;
class Link extends \Magento\Framework\View\Element\Html\Link
{
public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    array $data = []
) {
    parent::__construct($context, $data);
}

/**
 * Render block HTML.
 *
 * @return string
 */
protected function _toHtml() {
    return '<li><a'.$this->getLinkAttributes().'>'.$this->escapeHtml($this->getLabel()).'</a></li>';
    }
}

default.xml

<?xml version="1.0" encoding="UTF-8"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceBlock name="top.links">
        <block class="[vendor]\[module]\Block\Link" name="test-top-link" before="my-account-link">
            <arguments>
                <argument name="label" xsi:type="string" translate="true">Something</argument>
            </arguments>
        </block>
    </referenceBlock>
</body>

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.