please check the below code.
step 1
path - app/code/Training/Msg/registration.php
<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(ComponentRegistrar::MODULE,'Training_Msg',__DIR__);
step 2
path - app/code/Training/Msg/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="Training_Msg" setup_version="1.0.0">
<sequence>
<module name="Magento_Customer"/>
</sequence>
</module>
</config>
step 3
path - app/code/Training/Customerlink/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Framework\View\Element\Html\Link\Current" type="Training\Msg\Block\Current"/>
</config>
step 4
path - app/code/Training/Msg/etc/frontend/routes.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="msg" frontName="msg">
<module name="Training_Msg"/>
</route>
</router>
</config>
step 5
path - app/code/Training/Msg/Controller/Index/Msg.php
<?php
namespace Training\Msg\Controller\Index;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\View\Result\PageFactory;
class Msg extends Action
{
private $jsonFactory;
private $pageFactory;
public function __construct(
Context $context,
JsonFactory $jsonFactory,
PageFactory $pageFactory
){
$this->jsonFactory = $jsonFactory;
$this->pageFactory = $pageFactory;
parent::__construct($context);
}
public function execute()
{
//$result = $this->jsonFactory->create();
$result = $this->pageFactory->create();
$result->getConfig()->getTitle()->set("Welcome Msg");
//$data = ['msg' => 'tets'];
//return $result->setData($data);
return $result;
}
}
step 6
path - app/code/Training/Msg/Block/Current.php
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Training\Msg\Block;
/**
* Block representing link with two possible states.
* "Current" state means link leads to URL equivalent to URL of currently displayed page.
*
* @method string getLabel()
* @method string getPath()
* @method string getTitle()
* @method null|array getAttributes()
* @method null|bool getCurrent()
* @method \Magento\Framework\View\Element\Html\Link\Current setCurrent(bool $value)
*/
class Current extends \Magento\Framework\View\Element\Template
{
/**
* Default path
*
* @var \Magento\Framework\App\DefaultPathInterface
*/
protected $_defaultPath;
protected $customerSession;
/**
* Constructor
*
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Framework\App\DefaultPathInterface $defaultPath
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Framework\App\DefaultPathInterface $defaultPath,
\Magento\Customer\Model\Session $customerSession,
array $data = []
) {
parent::__construct($context, $data);
$this->_defaultPath = $defaultPath;
$this->customerSession = $customerSession;
}
/**
* Get href URL
*
* @return string
*/
public function getHref()
{
return $this->getUrl($this->getPath());
}
/**
* Get current mca
*
* @return string
*/
private function getMca()
{
$routeParts = [
'module' => $this->_request->getModuleName(),
'controller' => $this->_request->getControllerName(),
'action' => $this->_request->getActionName(),
];
$parts = [];
foreach ($routeParts as $key => $value) {
if (!empty($value) && $value != $this->_defaultPath->getPart($key)) {
$parts[] = $value;
}
}
return implode('/', $parts);
}
/**
* Check if link leads to URL equivalent to URL of currently displayed page
*
* @return bool
*/
public function isCurrent()
{
return $this->getCurrent() || $this->getUrl($this->getPath()) == $this->getUrl($this->getMca());
}
/**
* Render block HTML
*
* @return string
*/
protected function _toHtml()
{
if (false != $this->getTemplate()) {
return parent::_toHtml();
}
$highlight = '';
if ($this->getIsHighlighted()) {
$highlight = ' current';
}
if ($this->isCurrent()) {
$html = '<li class="nav item current">';
$html .= '<strong>'
. $this->escapeHtml((string)new \Magento\Framework\Phrase($this->getLabel()))
. '</strong>';
$html .= '</li>';
} else {
if( $this->customerSession->getCustomerGroupId() == 1 ){
if ( strpos($this->getHref(),'msg') == false ) {
$html = '<li class="nav item' . $highlight . '"><a href="' . $this->escapeHtml($this->getHref()) . '"';
$html .= $this->getTitle()
? ' title="' . $this->escapeHtml((string)new \Magento\Framework\Phrase($this->getTitle())) . '"'
: '';
$html .= $this->getAttributesHtml() . '>';
if ($this->getIsHighlighted()) {
$html .= '<strong>';
}
$html .= $this->escapeHtml((string)new \Magento\Framework\Phrase($this->getLabel()));
if ($this->getIsHighlighted()) {
$html .= '</strong>';
}
$html .= '</a></li>';
}
}else
{
$html = '<li class="nav item' . $highlight . '"><a href="' . $this->escapeHtml($this->getHref()) . '"';
$html .= $this->getTitle()
? ' title="' . $this->escapeHtml((string)new \Magento\Framework\Phrase($this->getTitle())) . '"'
: '';
$html .= $this->getAttributesHtml() . '>';
if ($this->getIsHighlighted()) {
$html .= '<strong>';
}
$html .= $this->escapeHtml((string)new \Magento\Framework\Phrase($this->getLabel()));
if ($this->getIsHighlighted()) {
$html .= '</strong>';
}
$html .= '</a></li>';
}
}
return $html;
}
/**
* Generate attributes' HTML code
*
* @return string
*/
private function getAttributesHtml()
{
$attributesHtml = '';
$attributes = $this->getAttributes();
if ($attributes) {
foreach ($attributes as $attribute => $value) {
$attributesHtml .= ' ' . $attribute . '="' . $this->escapeHtml($value) . '"';
}
}
return $attributesHtml;
}
}
Here the custom link Customer Message only display to user group id is not equal to 1.
step 7
path - app/code/Training/Msg/Block/Msg.php
<?php
namespace Training\Msg\Block;
use Magento\Framework\View\Element\Template;
class Msg extends Template
{
public function __construct(Template\Context $context, array $data = [])
{
parent::__construct($context, $data);
}
public function getTitle()
{
return "Test Message";
}
}
step 8
path - app/code/Training/Msg/view/frontend/layout/customer_account.xml
<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-myemail">
<arguments>
<argument name="path" xsi:type="string">msg/index/msg</argument>
<argument name="label" xsi:type="string">Customer Message</argument>
</arguments>
</block>
</referenceBlock>
</body>
</page>
step 9
path - app/code/Training/Msg/view/frontend/layout/msg_index_msg.xml
<?xml version="1.0"?>
<page layout="2columns-left" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="customer_account"/>
<body>
<referenceBlock name="content">
<block name="msg" class="Training\Msg\Block\Msg" template="Training_Msg::msg.phtml" />
</referenceBlock>
</body>
</page>
step 10
path - app/code/Training/Msg/view/frontend/templates/msg.phtml
<?php echo $block->getTitle(); ?>
OR
You can download the module from github link https://github.com/jothibasuj/customer-dashboard-link-based-on-condition
- Run setup upgrade.
- Clear cache.
- set permission to var folder.