:::: MENU ::::

Get all module list in Magento 2

Get all module list in Magento 2

This tutorial is about Get all module list in Magento 2 .i.e how to get all module or extension list in magento 2. Also the status, whether it’s enable or disabled. I assume your question is,how to get it? And in this tutorial i will try to explain it briefly and also make it convenient.

There are few ways to achieve it. We will discuss few here.

Lets start with a method, in which we can get the desired result using CLI command.

Using CLI command

There is a CLI command

bin/magento module:status

which returns the list of enabled and disbaled modules. Check the below given path to know how it’s done.

\Magento\Setup\Console\Command\ModuleStatusCommand::execute

Basically

\Magento\Framework\Module\ModuleList::getNames

returns you a list of enabled modules. Similarly

\Magento\Framework\Module\FullModuleList::getNames

returns you a full list of all modules.Furthermore we will have to do an array_diff() to get the disabled modules.

Secondly we can take help from Magento’s core codebase.

Magento Corebase

Since we already discuss the command that lists all modules:

php bin/magento module:status

Let me share with you that magento uses

\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 QaisarSatti\HelloWorld\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();
        ...
    }
}

I hope it is quite efficient and fulfill its use.

Furthermore there are another two ways to get this done.

Lets have a look at the them respectively.

1.

Following code will give the enabled and disabled modules list. I have commented the code to make it easy to understand what the code is doing.

protected $fullModuleList;
protected $moduleManager;

public function __construct(
    ...................
    \Magento\Framework\Module\FullModuleList $fullModuleList,
    \Magento\Framework\Module\Manager $moduleManager,
    ...................
) {

    $this->fullModuleList = $fullModuleList;
    $this->moduleManager = $moduleManager;
}

public function yourFunction()
{
    ...
    $allModules = $this->fullModuleList->getAll();
    $listOfModules = [];
    foreach ($allModules as $key => $value) {
        $listOfModules[] = $key;
    }
    foreach ($listOfModules as $key => $value) {
        if ($this->moduleManager->isEnabled($value)) {
            echo $value; //Enable Module List
        } else {
            echo $value; //Disable Module List
        }
    }
    ...
}
Similarly the second method is as follows.<strong>
2.</strong>

protected $fullModuleList;
protected $moduleList;

public function __construct(
    ...................
    \Magento\Framework\Module\FullModuleList $fullModuleList,
    \Magento\Framework\Module\ModuleList $moduleList,
    ...................
) {

    $this->fullModuleList = $fullModuleList;
    $this->moduleList = $moduleList;
}

public function yourFunction()
{
    ...
    $allModules = $this->fullModuleList->getNames(); //List of All Module Names
    $enabled_list = $this->moduleList->getNames(); //List of Enabled Module Names
    $disabled_list = array_diff($enabled_list, $allModules); //List of Disabled Module Names

That’s it from this tutorial. I hope it serves the purpose.
Since these are learning tutorials,please feel free to drop any suggestions or queries in comments section. That will definitely be highly appreciated.

Author: Qaisar Satti
Category: Magento 2
Last Modified: November 9, 2018


So, what do you think ?