0

I tried below code which gives me all modules name not specifying the status whether it is enable or disable.

__construct

public function __construct(
\Magento\Framework\Module\ModuleList $moduleList
) {
$this->moduleList = $moduleList;
}

and here's the list

$modules = $this->moduleList->getAll();

Any idea to get module names with it's status ?

3

There is a CLI command bin/magento module:status which returns the list of enabled modules and disabled. Check \Magento\Setup\Console\Command\ModuleStatusCommand::execute how it's done.

Basicaly \Magento\Framework\Module\ModuleList::getNames returns you a list of enabled modules and \Magento\Framework\Module\FullModuleList::getNames returns you a full list of all modules, so you will have to do an array_diff() to get the disabled modules.

I could not find an option to get the list with statuses out of the box.

  • thanks for the answer+1, will try and let you know. – Aditya Shah Oct 22 '18 at 12:25
1

You can take help from Magento's core codebase. As you know that below command lists all modules:

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

I hope it would be helpful.

  • It gives the full list of modules not status, still thanks for the answer +1 – Aditya Shah Oct 22 '18 at 12:24
1

You can get by using below 2 ways :


EDIT 1 :

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
        }
    }
    ...
}

EDIT 2 :

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
    ...
}
  • thanks for the answer+1 :) achieved, for-each will slow the process :) – Aditya Shah Oct 22 '18 at 12:55
  • used array diff – Aditya Shah Oct 22 '18 at 12:55
  • 1
    okay. I'l take care. You can take more reference from here : /vendor/magento/magento2-base/setup/src/Magento/Setup/Console/Command/ModuleStatusCommand.php – Rohan Hapani Oct 22 '18 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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