Magento 2 Modules – Get List of All Modules Using Code in Magento 2

magento2magento2.2magento2.2.4

I am working on creating a base module for all other modules under one namespace.

I want to show a list of all modules (with their version and their status as enabled or disabled).

I have injected dependency of \Magento\Framework\Module\ModuleListInterface in my block file, but I am not getting the module in the list if I disable it.

I have tried getAll() and getNames() functions. No function is returning disabled module names in the list.

Previously it was possible to get the list of all modules because there was a section in admin panel (Advanced), where all modules were listed with respective module output.

Kindly help me to get the list of all modules.

Best Answer

After some study of Magento core modules, I checked the source, from where the following command is executed:

php bin/magento module:status

I found that Magento is using \Magento\Framework\Module\FullModuleList class to get the list of enabled and disabled modules.

Based on Magento's implementation, I have implemented my code in the following way:

<?php
namespace Mohit\Base\Block;

class Modules extends \Parent\Class
{
    protected $fullModuleList;

    public function __construct(
        \Other\Dependenciy\Classes,
        \Magento\Framework\Module\FullModuleList $fullModuleList
    ) {

        $this->fullModuleList = $fullModuleList;
    }

    public function modulesList()
    {
        ...
        $allModules = $this->fullModuleList->getAll();
        ...
    }
}

Also, tons of thanks to @sanne, who helped me to go in the right direction.

Related Topic