1

Problem

I want to add an attribute to current link in Magento 2 Customer Dashboard i Checked Magento\Framework\View\Element\Html\Link\Current, there in Line 119 All the Link Having an Li have a function $this->getAttributesHtml() by which all none current link can assigned an HTML attribute passed from XML

Now the current link hasn't assigned $this->getAttributesHtml() that why the attribute passed through XML is Not assigned to current link

I tried to Override Current Class as Follows

etc\frontend\di.xml

 <preference 
    for="Magento\Framework\View\Element\Html\Link\Current" 
    type="Vendor\Module\View\Element\Html\Link\Current" />

Vendor\Module\View\Element\Html\Link\Current

Changed this Condition

    if ($this->isCurrent()) {
        $html = '<li class="nav item current">';
        $html .= '<strong'>'
            . $this->escapeHtml(__($this->getLabel()))
            . '</strong>';
        $html .= '</li>';
    }

To

    if ($this->isCurrent()) {
        $html = '<li class="nav item current">';
        $html .= '<strong '.$this->getAttributesHtml() .'>'
            . $this->escapeHtml(__($this->getLabel()))
            . '</strong>';
        $html .= '</li>';
    }

If I make a change in Core File I works Fine But in Overridden Class Its Now Working

Need Help

  • is your module enabled? and did you clean the cache? – Philipp Sander Jan 16 at 10:28
  • Yes i Have Clean cache – Waqar Ali Jan 16 at 10:30
  • and did you verify that your module is enabled? – Philipp Sander Jan 16 at 10:32
  • yes module is enable and all other functionalities are working fine only thi file is not overriding – Waqar Ali Jan 16 at 10:37
2

If you want to overwrite links on the customer account page:

menu on the customer account page

you should make a preference for the Magento\Customer\Block\Account\SortLink class, not for it's parent.

Use this preference in di.xml:

<preference for="Magento\Customer\Block\Account\SortLink" type="Vendor\Model\Block\CurrentLink" />

Extend a real class inside:

class CurrentLink extends \Magento\Customer\Block\Account\SortLink

and overwrite a protected method _toHtml per your needs:

/**
 * 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->getAttributesHtml() .'>'
            . $this->escapeHtml(__($this->getLabel()))
            . '</strong>';
        $html .= '</li>';
    } else {
        $html = '<li class="nav item' . $highlight . '"><a href="' . $this->escapeHtml($this->getHref()) . '"';
        $html .= $this->getTitle()
            ? ' title="' . $this->escapeHtml(__($this->getTitle())) . '"'
            : '';
        $html .= $this->getAttributesHtml() . '>';

        if ($this->getIsHighlighted()) {
            $html .= '<strong>';
        }

        $html .= $this->escapeHtml(__($this->getLabel()));

        if ($this->getIsHighlighted()) {
            $html .= '</strong>';
        }

        $html .= '</a></li>';
    }

    return $html;
}

Do not forget to clean cache and run setup:di:compile.

If you want to add the id for the current link, just change this line (inside the if ($this->isCurrent()) condition body):

$html = '<li class="nav item current">';

to the:

$html = '<li class="nav item current" id="current-link">';

clean cache and see updated result on the page:

current link with id attribute on the page

| improve this answer | |
  • how i can add id attribute on current link using 'SortLink' class ? – Waqar Ali Jan 16 at 10:41
  • @WaqarAli same way as you doing it before but with another class. I've update my answer. – Siarhey Uchukhlebau Jan 16 at 10:48
  • @WaqarAli I've update my answer added with id attribute for the current link. – Siarhey Uchukhlebau Jan 16 at 11:14
  • 1
    Thank You Sir Its Working Great – Waqar Ali Jan 16 at 11:24

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.