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 !

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

4 Answers 4

1

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.

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

21
  • 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>
    – Fbr
    Apr 12, 2019 at 13:21
  • yes in that file you do sequence
    – fmsthird
    Apr 12, 2019 at 13:22
  • What do you mean by "sequence" ? Something is missing in this file ?
    – Fbr
    Apr 12, 2019 at 13:24
  • check updated answer . it should be like that in your module.xml
    – fmsthird
    Apr 12, 2019 at 13:25
  • 1
    Comments are not for extended discussion; this conversation has been moved to chat.
    – Amit Bera
    Apr 13, 2019 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 ?

0

We can override using preference.

  • Create di.xml

  • Create class php file

     <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
     <preference for="vendor\magento\framework\File\Uploader" type="NameSpace\Customer\Framework\File\Uploader" />
    
  • In the given path put your fle NameSpace\Customer\Framework\File\Uploader

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.