Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In Magento 1.x I was able to read my module's version like so:

(string)Mage::getConfig()->getNode('modules/MyModuleName/version');

I'm unable to figure out how I can do the same in Magento 2.0. Thanks.

share|improve this question
up vote 8 down vote accepted

You could inject Magento\Framework\Module\ModuleListInterface in to your module helper class and then have a helper function to get it. Something like;

<?php

namespace [Vendor]\[ModuleName]\Helper;

use Magento\Framework\App\Helper\Context;
use Magento\Framework\Module\ModuleListInterface;

class Data extends AbstractHelper
{
    const MODULE_NAME = 'xxx';

    protected $_moduleList;

    public function __construct(
        Context $context,
        ModuleListInterface $moduleList)
    {
        $this->_moduleList = $moduleList;
        parent::__construct($context);
    }

    public function getVersion()
    {
        return $this->_moduleList
            ->getOne(self::MODULE_NAME)['setup_version'];
    }
}

Sorry, i haven't had time to put this in to code and test it thoroughly.

share|improve this answer

Try following way:

$moduleInfo =  $this->_objectManager->get('Magento\Framework\Module\ModuleList')->getOne('SR_Learning'); // SR_Learning is module name

print_r($moduleInfo);

Output

Array ( [name] => SR_Learning [setup_version] => 2.0.0 [sequence] => Array ( ) )
share|improve this answer

It's bit different but not that hard.

All you have to do is inject the \Magento\Framework\Module\ModuleListInterface via helper constructor and use that in custom function.

Here is the full working code taken from https://github.com/MagePsycho/magento2-easy-template-path-hints

<?php
namespace MagePsycho\Easypathhints\Helper;

/**
 * Utility Helper
 *
 * @category   MagePsycho
 * @package    MagePsycho_Easypathhints
 * @author     Raj KB <magepsycho@gmail.com>
 * @website    http://www.magepsycho.com
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * @var \Magento\Framework\Module\ModuleListInterface
     */
    protected $_moduleList;

    /**
     * @param \Magento\Framework\App\Helper\Context $context
     * @param \Magento\Framework\Module\ModuleListInterface $moduleList
     */
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Framework\Module\ModuleListInterface $moduleList
    ) {
        $this->_moduleList              = $moduleList;

        parent::__construct($context);
    }



    public function getExtensionVersion()
    {
        $moduleCode = 'MagePsycho_Easypathhints'; #Edit here with your Namespace_Module
        $moduleInfo = $this->_moduleList->getOne($moduleCode);
        return $moduleInfo['setup_version'];
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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