2

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 !

  • Why you overwrite the whole class? – Sohel Rana Apr 12 at 12:25
  • I need to override getHref() and _toHtml() method of Current class. this is not the way to go? – fabienb Apr 12 at 12:27
0

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.

  • Thanks for your answer. I removed the model/framework folders but it still does not work. Do I need to run specific commands ? – fabienb Apr 12 at 12:44
  • you can delete the files inside generated folder and clean your cache and try again – Thamo Apr 12 at 12:46
  • Unfortunately it still does not work... – fabienb Apr 12 at 12:51
  • you can use the plugin to overwrite the specific function. I have updated the answer with code also. – Thamo Apr 12 at 13:31
  • Plugin solution works, but not for protected method like _toHtml() – fabienb Apr 23 at 13:33
0

In your etc/module sequence you should put the Magento_Framework

To make sure you have the right order of module dependencies.

UPDATE:

<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\View\Element\Html\Link\Current" />
</config>

Note: Make sure to put the Current.php inside View\Element\Html\Link directory

  • In the file app/code/MyNamespace/MagentoFramework/etc/module.xml <?xml version="1.0" encoding="UTF-8"?> <config xmlns:xsi="w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="MyNamespace_MagentoFramework" setup_version="0.0.1"> </module> </config> – fabienb Apr 12 at 13:21
  • yes in that file you do sequence – magefms Apr 12 at 13:22
  • What do you mean by "sequence" ? Something is missing in this file ? – fabienb Apr 12 at 13:24
  • check updated answer . it should be like that in your module.xml – magefms Apr 12 at 13:25
  • 1
    Comments are not for extended discussion; this conversation has been moved to chat. – Amit Bera Apr 13 at 5:08
0

After following this article How to Override Framework Class in Magento 2, The override works for this file :

vendor/magento/framwork/View/Page/Title.php

but not for this file :

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

Do you know if this file can not be overridden ?

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.