You are here

class ModulesWeight in Modules weight 8.2

Same name and namespace in other branches
  1. 8 src/ModulesWeight.php \Drupal\modules_weight\ModulesWeight

Class Modules Weight.

@package Drupal\modules_weight

Hierarchy

Expanded class hierarchy of ModulesWeight

1 file declares its use of ModulesWeight
ModulesWeightTest.php in tests/src/Unit/ModulesWeightTest.php
1 string reference to 'ModulesWeight'
modules_weight.services.yml in ./modules_weight.services.yml
modules_weight.services.yml
1 service uses ModulesWeight
modules_weight in ./modules_weight.services.yml
Drupal\modules_weight\ModulesWeight

File

src/ModulesWeight.php, line 13

Namespace

Drupal\modules_weight
View source
class ModulesWeight implements ModulesWeightInterface {

  /**
   * Drupal\Core\Config\ConfigFactoryInterface definition.
   *
   * @var Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Drupal\Core\Extension\ModuleExtensionList definition.
   *
   * @var Drupal\Core\Extension\ModuleExtensionList
   */
  protected $moduleExtensionList;

  /**
   * Constructs a new ModulesWeight object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory.
   * @param Drupal\Core\Extension\ModuleExtensionList $module_extension_list
   *   The module extension list.
   */
  public function __construct(ConfigFactoryInterface $config_factory, ModuleExtensionList $module_extension_list) {
    $this->configFactory = $config_factory;
    $this->moduleExtensionList = $module_extension_list;
  }

  /**
   * {@inheritdoc}
   */
  public function getModulesList($show_core_modules = FALSE) {
    $modules = [];

    // Getting the module list.
    $installed_modules = $this->moduleExtensionList
      ->getAllInstalledInfo();

    // Getting the modules weight from the config.
    $modules_weight = $this->configFactory
      ->get('core.extension')
      ->get('module');

    // Iterating over each module.
    foreach ($installed_modules as $filename => $module_info) {

      // We don't want to show the hidden modules, or the Core modules
      // (if the $force is set to FALSE).
      if (!isset($module_info['hidden']) && ($show_core_modules || $module_info['package'] != 'Core')) {
        $modules[$filename]['name'] = $module_info['name'];
        $modules[$filename]['description'] = $module_info['description'];
        $modules[$filename]['weight'] = $modules_weight[$filename];
        $modules[$filename]['package'] = $module_info['package'];
      }
    }

    // Sorting all modules by their weight.
    uasort($modules, [
      'Drupal\\modules_weight\\Utility\\SortArray',
      'sortByWeightAndName',
    ]);
    return $modules;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ModulesWeight::$configFactory protected property Drupal\Core\Config\ConfigFactoryInterface definition.
ModulesWeight::$moduleExtensionList protected property Drupal\Core\Extension\ModuleExtensionList definition.
ModulesWeight::getModulesList public function Return the modules list ordered by the modules weight. Overrides ModulesWeightInterface::getModulesList
ModulesWeight::__construct public function Constructs a new ModulesWeight object.