Check this for more info: blog.mageprince.com
You need to create block for dynamic link
1) Define Your block class {vendor}\{Module}\Block\Customer\Link
in navigation link
<?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="{vendor}\{Module}\Block\Customer\Link" name="customer-account-navigation-new-product-link" after="-">
<arguments>
<argument name="label" xsi:type="string">Some link</argument>
<argument name="path" xsi:type="string">customer/somelink/index</argument>
<argument name="sortOrder" xsi:type="number">250</argument>
</arguments>
</block>
</referenceBlock>
</body>
</page>
2) Now create Link.php at app/code/{Vendor}/{Module}/Block/Customer/Link.php
<?php
namespace Vendor\Module\Block\Customer;
use Magento\Customer\Block\Account\SortLinkInterface;
use Magento\Framework\View\Element\Html\Link\Current;
class Link extends Current implements SortLinkInterface
{
protected $_customerSession;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Framework\App\DefaultPathInterface $defaultPath,
\Magento\Customer\Model\Session $customerSession,
array $data = []
) {
$this->_customerSession = $customerSession;
parent::__construct($context, $defaultPath, $data);
}
protected function _toHtml()
{
$responseHtml = null; // need to return at-least null
if($this->_customerSession->isLoggedIn()) {
$customerGroup = $this->_customerSession->getCustomer()->getGroupId(); //Current customer groupID
//Your Logic Here
if($customerGroup == '1') {
$responseHtml = parent::_toHtml(); //Return link html
}
}
return $responseHtml;
}
public function getSortOrder()
{
return $this->getData(self::SORT_ORDER);
}
}
Here you make the dynamic link as per your logic. Here I check current customer groupId and return HTML of link if customer group = 1 otherwise return null.
This same way you can make your second link dynamic.
If you want only navigation link dynamic then use helper
1) Define helper class {vendor}\{Module}\Helper\Data
in navigation link
<?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\Customer\Block\Account\SortLinkInterface" name="customer-account-navigation-new-product-link" after="-">
<arguments>
<argument name="label" xsi:type="string">Some link</argument>
<argument name="path" xsi:type="helper" helper="{Vendor}\{Module}\Helper\Data::getLink"></argument>
</arguments>
</block>
</referenceBlock>
</body>
</page>
2) Now create Data.php at app/code/{Vendor}/{Module}/Helper/Data.php
<?php
namespace Vendor\Module\Helper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
public function getLink()
{
//Do your logic and return link
$link = "sales/order/mylink";
return $link;
}
}
Note :
In Magento 2.1.x remove var/generation
and flush cache
.
In Magento 2.2.x remove generated/code
and generated/metadata
and flush cache
.