Magento – How to override Magento 2 framework file

magento2moduleoverrides

I try to override this file :

vendor/magento/framework/View/Element/Html/Link/Current.php

I create a module.xml, registration.php and di.xml

<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="MyNamespace\MagentoFramework\Model\Framework\View\Element\Html\Link\Current" />
</config>

My override class :

namespace MyNamespace\MagentoFramework\Model\Framework\View\Element\Html\Link;

    class Current extends \Magento\Framework\View\Element\Html\Link\Current
    {
        /**
         * Get href URL
         *
         * @return string
         */
        public function getHref()
        {
            return $this->getUrl($this->getPath());
        }
    }

The module is enable but this file doesn't override the original.

Do you have any suggestions ?
Thanks !

Best Answer

You can refer the link How to Override Framework Class in Magento 2

They are following the same folder structure in custom namespace or module section.

You can try the same logic without introducing additional folders [model/framework]

OR

You can implement plugin to overwrite the function.

di.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<type name="Magento\Framework\View\Element\Html\Link\Current">
    <plugin name="customermgmt_index_plugin" type="Devi\Customview\Plugin\Current" sortOrder="10" disabled="false"  />
</type>
</config>

Current.php ( Plugin File )

namespace Devi\Customview\Plugin;

class Current
{

  public function afterGetHref()
  {
    echo 'plugin working';

   }
 }

Please check and let me know whether its solving your needs.