2

I need to add the link Messages(3 new) in the customer Dashboard left menu where the (3 New) is the count being calculated some where in block etc. Also How can I make font bold IF there is any unread messages.

The look is as follows with 'Notification (3 new)'.

enter image description here

What is the standard Magento2 recommended way?

More detail is that I am using a block to filter out against a customer group. the code is:

namespace Namespace\Module\Block\Customergroup;

class Link extends \Magento\Framework\View\Element\Html\Link\Current {

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;
    if ($this->_customerSession->isLoggedIn()) {

        $customerGroup = $this->_customerSession->getCustomer()->getGroupId();

        if ($customerGroup == '4') {
            $responseHtml = parent::_toHtml();
        }
    }
    return $responseHtml;
}

I am curently lost as to how I can integrate the custom Labeling and bold thing into this.

0

You will need to create a custom block to do this.

First in your module's view/frontend/layout/customer_account.xml you'll have something like this.

<block class="Company\Module\Block\Customer\Account\NotificationsLink" name="customer-account-navigation-notifications-link">
    <arguments>
        <argument name="label" xsi:type="string" translate="true">Notifications</argument>
        <argument name="path" xsi:type="string">custom/path</argument>
        <argument name="sortOrder" xsi:type="number">300</argument>
    </arguments>
</block>

Notice the custom class we reference Company\Module\Block\Customer\Account\NotificationsLink. Next you should create this block class and have it extend Magento\Customer\Block\Account\SortLink. In this class you can create a function called getLabel() which will override the default behavior. This is where you can call whatever logic you need for calculations.

As for making it dynamically bold you will need to override the _toHtml() function. You'll see in Magento\Framework\View\Element\Html\Link\Current.php::_toHtml() how it is currently working. Copy that into your new class and add logic to have the <strong></strong> tags when necessary.

Update

After looking at the code in Magento\Framework\View\Element\Html\Link\Current.php::_toHtml() again I realized there's an easier way to accomplish the bold text. You simply need to set isHighlighted to true and the parent class will take care of it. So if you look at the sample code you added to the question, then inside your if($customerGroup == '4') you could just add $this->setIsHighlighted(true) before you call parent::_toHtml() and it should be bolded.

  • Thankyou for you answer, and I have added more detail regarding the customer group filter by a block. I am unable to understand how to integrate the custom labeling and bold into this. how can I iterate the menu links so that i can use my logic in it? Kindly help. Thanks in advance. – M Shoaib Qureshi Feb 25 '19 at 6:04
  • You will need to actually override the code that builds the html. Look at Magento\Framework\View\Element\Html\Link\Current.php::_toHtml() to see the code in there. You'll need to copy that code into yours and then add an extra condition where it adds the <strong></strong> tags. – rain2o Feb 25 '19 at 8:57
  • An alternative option could be to set isCurrent to true. So in your condition you could try $this->setCurrent(true) and that might do it. Haven't tested this theory, but seems like it would work. – rain2o Feb 25 '19 at 9:00
  • I am able to add the dynamic count of items via another method. Now only if I could know how can I iterate between the links in the _toHtml() , so I could add <strong> tag prior/post the label. – M Shoaib Qureshi Feb 25 '19 at 12:30
  • You do not need to iterate the links. The class you are in represents the block class for the link you added (Notifications). If you look at the code in _toHtml() of Current.php as I mentioned before it shows you exactly how to build the HTML around the link. github.com/magento/magento2/blob/… – rain2o Feb 25 '19 at 13:12

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.