Categories


Archives


Recent Posts


Categories


Magento 2: Give your Helpers a _moduleName

astorm

Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

Updated for Magento 2! No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

When you’re converting you Magento modules to Magento 2, make sure your helper classes have a _moduleName property.

protected $_moduleName='Packagename_Modulename'

I ran into an issue where the isModuleOutputEnabled method relied on this value being set. If the property isn’t there, Magento will attempt to infer a module name via the helper’s _getModuleName method

protected function _getModuleName()
{
    if (!$this->_moduleName) {
        $class = get_class($this);
        $this->_moduleName = substr($class, 0, strpos($class, '\Helper'));
    }
    return str_replace(MagentoAutoloadIncludePath::NS_SEPARATOR, '_', $this->_moduleName);
}

but this method assumes you’ve converted your module to namespaces. For greenfield Magento 2 work, this will be fine — but I imagine most of us will be supporting Magento 1 during the eventual overlap period.

Copyright © Alan Storm 1975 – 2019 All Rights Reserved

Originally Posted: 9th November 2013