Question

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

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top