I am trying to override the following function below within the following file:
vendor/magento/framework/View/Element/Html/Link/Current.php
protected function _toHtml()
So I created a plugin in the following location:
app/code/Sho/CurrentLinks
Created registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Sho_CurrentLinks',
__DIR__
);
Created 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="Sho_CurrentLinks" setup_version="1.0.1">
</module>
</config>
Created etc/di.xml (tried it in etc/frontend/di.xml as well)
<?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" type="Sho\CurrentLinks\Framework\View\Element\Html\Link" />
</config>
Created Framework/View/Element/Html/Link/Current.php (Excert from top of file as I understand function _toHtml() is protected so I was overriding the entire class.
/**
* Copyright Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Sho\CurrentLinks\Framework\View\Element\Html\Link;
/**
* Block representing link with two possible states.
* "Current" state means link leads to URL equivalent to URL of currently displayed page.
*
* @api
* @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
{Entire file: https://pastebin.com/ydU0nwsj
Its not working but I am not sure why, of course a programmer might take a look at say WTH is this guy doing
Hello SamB_GB
Kindly use below for overriding "vendor/magento/framework/View/Element/Html/Link/Current.php"
app/code/Magebull/CurrentLinks/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Magebull_CurrentLinks',
__DIR__
);
app/code/Magebull/CurrentLinks/composer.json
{
"name": "magebull/currentlinks",
"description": "Magebull CurrentLinks",
"require": {
"php": "~5.6.0|7.0.2|~7.0.6",
"magento/module-store": "0.74.0-beta4",
"magento/module-theme": "0.74.0-beta4",
"magento/module-widget": "0.74.0-beta4",
"magento/module-backend": "0.74.0-beta4",
"magento/module-catalog": "0.74.0-beta4",
"magento/module-email": "0.74.0-beta4",
"magento/module-ui": "0.74.0-beta4",
"magento/module-variable": "0.74.0-beta4",
"magento/module-media-storage": "0.74.0-beta4",
"magento/framework": "0.74.0-beta4",
"magento/magento-composer-installer": "*"
},
"type": "magento2-module",
"version": "0.74.0-beta4",
"license": [
"OSL-3.0",
"AFL-3.0"
],
"extra": {
"map": [
[
"*",
"Magebull/CurrentLinks"
]
]
}
}
app/code/Magebull/CurrentLinks/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Magebull_CurrentLinks" setup_version="1.0.0"></module>
<sequence>
<module name="Magento_Backend"/>
<module name="Magento_Sales"/>
<module name="Magento_Quote"/>
<module name="Magento_Checkout"/>
</sequence>
</config>
app/code/Magebull/CurrentLinks/etc/di.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="Magebull\CurrentLinks\Framework\Current" />
</config>app/code/Magebull/CurrentLinks/Framework/Current.php
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magebull\CurrentLinks\Framework;
/**
* Block representing link with two possible states.
* "Current" state means link leads to URL equivalent to URL of currently displayed page.
*
* @api
* @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;
/**
* 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,
array $data = []
) {
parent::__construct($context, $data);
$this->_defaultPath = $defaultPath;
}
/**
* 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()
{
echo "fff";
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 {
$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;
}
}
You can download extension from below link.
URL: https://we.tl/t-Fq3gi1vsIW
Hope it will help to solve the problem then please accept as solution and kudos.
@prakash786 , above module link is expired as well as I have tried the same but not able to make it around.
/**
* @return string
*/
protected function _toHtml()
{
$cssClasses = $this->hasData('css_classes') ? explode(' ', $this->getData('css_classes')) : [];
$cssClasses[] = 'price-' . $this->getPrice()->getPriceCode();
$this->setData('css_classes', implode(' ', $cssClasses));
return parent::_toHtml();
}