8

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>
18

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

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.