8

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.

5 Answers 5

18

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.

1
  • 3
    setup version is not the extension version but db schema version, it is not one from composer.json Commented Mar 22, 2017 at 12:29
8

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 ( ) ) 
3

Not sure if i understood your question completely, but you could use n98-magerun2 and then export the result:

n98-magerun2 dev:module:list 
2

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 <[email protected]> * @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']; } } 
0

All previous answers retrieve the setup version of the extension.
Use code below for getting the composer version of the extension.

Case 1. We'll get version from composer.json file from extension directory

class VersionFinder { public function __construct( \Magento\Framework\App\Utility\Files $files ) { $this->files = $files; } public function getVersion() { $pathToNeededModule = ''; $composerFilePaths = array_keys( $this->files->getComposerFiles(\Magento\Framework\Component\ComponentRegistrar::MODULE) ); foreach ($composerFilePaths as $path) { //from app/code - may be specific logic if (strpos($path, 'MageWorx/Downloads/composer.json')) { $pathToNeededModule = $path; break; } //from vendor - may be specific logic if (strpos($path, 'mageworx/module-downloads/composer.json')) { $pathToNeededModule = $path; break; } } if ($pathToNeededModule) { $content = file_get_contents($pathToNeededModule); if ($content) { $jsonContent = json_decode($content, true); if (!empty($jsonContent['version'])) { return $jsonContent['version']; } } } return null; } } 

Case 2. We'll get the extension's version from composer.lock file.
In this case we can get only the versions for extensions which were installed using composer installation. If you find the composer metapackage version - choose this case also.

class ComposerVersionFinder { /** * @var \Magento\Framework\Composer\ComposerInformation */ private $composerInformation; public function __construct( \Magento\Framework\Composer\ComposerInformation $composerInformation ) { $this->composerInformation = $composerInformation; } public function getVersion() { $data = $this->composerInformation->getInstalledMagentoPackages(); if (!empty($data['mageworx/module-customergroupprices']['version'])) { var_dump($data['mageworx/module-customergroupprices']['version']); } return null; } } 

P.S. The code is executed slowly and I don't advise using this code in the frontend area. I supposed you need it on own extension page in the admin area.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.