10

How to create new Helper or override/rewrite Core Helper in Magento 2 module?

I tried adding dependency into my module.xml of Custom Module, but it doesn't load Helper class at all.

Below is my 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="Company1_Module1" schema_version="1.0.0" setup_version="1.0.0">
        <sequence>Magento_Directory</sequence>
  </module>
</config>
Rafael Corrêa Gomes
  • 13,309
  • 14
  • 84
  • 171
Vicky Dev
  • 1,992
  • 9
  • 29
  • 51

1 Answers1

21

Create: app/code/Company1/Module1/composer.json

{
    "name": "company1/module-module1",
    "description": "",
    "require": {
        "php": "~5.5.0|~5.6.0|~7.0.0",
        "magento/framework": "100.0.*",
        "magento/module-ui": "100.0.*",
        "magento/module-config": "100.0.*",
        "magento/module-directory": "100.0.*"
    },
    "type": "magento2-module",
    "version": "100.0.0",
    "license": [
        "OSL-3.0",
        "AFL-3.0"
    ],
    "autoload": {
        "files": [ "registration.php" ],
        "psr-4": {
            "Company1\\Module1\\": ""
        }
    }
}

Create: app/code/Company1/Module1/registration.php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Company1_Module1',
    __DIR__
);

Create: app/code/Company1/Module1/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="Company1_Module1" setup_version="2.0.0">
        <sequence>
            <module name="Magento_Directory"/>
        </sequence>
    </module>
</config>

Module creation done. So now create helper class inside Helper folder.

app/code/Company1/Module1/Helper/Data.php:

namespace Company1\Module1\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    public function someMethod()
    {
        return 1;
    }
}

How to use helper class inside controller

$this->_objectManager->create('Company1\Module1\Helper\Data')->someMethod();

How to use helper class inside block

public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Company1\Module1\Helper\Data $helper,
        array $data = []
    ) {
        $this->helper = $helper;
        parent::__construct($context, $data);
    }

So $this->helper is now instance of Data.

For overwrite any class you can use preference.

app/code/Company1/Module1/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\Directory\Helper\Data" type="Company1\Module1\Helper\Data" />
</config>

You can also use plugin. Plugin is the best way to overcome rewrite conflict. for more information plugin example

7ochem
  • 7,532
  • 14
  • 51
  • 80
Sohel Rana
  • 35,846
  • 3
  • 72
  • 90
  • Hi Sohel, Hi Fayyaz, How to instantiate an helper class from my custom module in a test program in the directory at the same level as the root of Magento? Could you please answer my post on SO here, https://magento.stackexchange.com/questions/360584/how-to-instantiate-an-helper-class-from-my-custom-module-in-a-test-program-in-th – CodeForGood Oct 08 '22 at 11:50