You are here

unused_modules.module in Unused Modules 7

Same filename and directory in other branches
  1. 8 unused_modules.module
  2. 6 unused_modules.module

This module lists modules and projects that are unused.

File

unused_modules.module
View source
<?php

/**
 * @file
 * This module lists modules and projects that are unused.
 */

// require_once() will only prevent including the same file more than once if
// file paths are the same. For the case in which unused_modules is enabled both
// as a drush command and a site module we need to control include files
// inclusion ourselves to avoid fatal errors generated by redeclaring functions
// from those file more than once.
// @see https://www.drupal.org/node/2755351
if (!function_exists('_unused_modules_get_modules_by_project')) {
  require_once __DIR__ . '/unused_modules.inc';
}

/**
 * Implements hook_menu().
 */
function unused_modules_menu() {

  // Base path.
  $items['admin/config/development/unused_modules'] = array(
    'title' => 'Unused Modules',
    'description' => 'Show projects that are unused',
    'page callback' => 'unused_modules_show_projects',
    'page arguments' => array(
      'disabled',
    ),
    'access arguments' => array(
      'administer modules',
    ),
  );

  // Projects.
  $items['admin/config/development/unused_modules/projects'] = array(
    'title' => 'Projects',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => 0,
  );
  $items['admin/config/development/unused_modules/projects/disabled'] = array(
    'title' => 'Fully disabled',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => 0,
  );
  $items['admin/config/development/unused_modules/projects/all'] = array(
    'title' => 'Also enabled',
    'description' => 'Show projects that are unused',
    'page callback' => 'unused_modules_show_projects',
    'page arguments' => array(
      'all',
    ),
    'access arguments' => array(
      'administer modules',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 1,
  );

  // Modules.
  $items['admin/config/development/unused_modules/modules'] = array(
    'title' => 'Modules',
    'description' => 'Show modules that are unused',
    'page callback' => 'unused_modules_show_modules',
    'page arguments' => array(
      'disabled',
    ),
    'access arguments' => array(
      'administer modules',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 1,
  );
  $items['admin/config/development/unused_modules/modules/disabled'] = array(
    'title' => 'Fully disabled',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => 0,
  );
  $items['admin/config/development/unused_modules/modules/all'] = array(
    'title' => 'Also enabled',
    'description' => 'Show modules that are unused',
    'page callback' => 'unused_modules_show_modules',
    'page arguments' => array(
      'all',
    ),
    'access arguments' => array(
      'administer modules',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 1,
  );
  return $items;
}

/**
 * Page callback.
 *
 * Returns a table with orphaned projects.
 *
 * @param string $op
 *   Either 'all' or 'disabled'.
 *
 * @return string
 *   themed table.
 */
function unused_modules_show_projects($op = 'all') {
  $modules = _unused_modules_get_modules_by_project();
  $header = array(
    'Project',
    'Project has Enabled Modules',
    'Project Path',
  );
  $rows = array();
  foreach ($modules as $module) {
    if ($op == 'all') {
      $rows[$module->project] = array(
        $module->project,
        $module->project_has_enabled_modules ? t("Yes") : t("No"),
        $module->project_path,
      );
    }
    elseif ($op == 'disabled') {
      if (!$module->project_has_enabled_modules) {
        $rows[$module->project] = array(
          $module->project,
          $module->project_has_enabled_modules ? t("Yes") : t("No"),
          $module->project_path,
        );
      }
    }
  }
  if (!$rows) {
    return t("Hurray, no orphaned projects!");
  }
  return theme('table', array(
    'header' => $header,
    'rows' => $rows,
  ));
}

/**
 * Page callback.
 *
 * Returns a table with orphaned modules.
 *
 * @param string $op
 *   Either 'all' or 'disabled'.
 *
 * @return string
 *   themed table.
 */
function unused_modules_show_modules($op = 'all') {
  $modules = _unused_modules_get_modules_by_project();
  $header = array(
    'Project',
    'Module',
    'Module enabled',
    'Project has Enabled Modules',
    'Project Path',
  );
  $rows = array();
  foreach ($modules as $module) {
    if ($op == 'all') {
      $rows[$module->name] = array(
        $module->project,
        $module->name,
        $module->module_is_enabled ? t("Yes") : t("No"),
        $module->project_has_enabled_modules ? t("Yes") : t("No"),
        $module->project_path,
      );
    }
    elseif ($op == 'disabled') {
      if (!$module->project_has_enabled_modules) {
        $rows[$module->name] = array(
          $module->project,
          $module->name,
          $module->module_is_enabled ? t("Yes") : t("No"),
          $module->project_has_enabled_modules ? t("Yes") : t("No"),
          $module->project_path,
        );
      }
    }
  }
  if (!$rows) {
    return t("Hurray, no orphaned modules!");
  }
  return theme('table', array(
    'header' => $header,
    'rows' => $rows,
  ));
}

Functions

Namesort descending Description
unused_modules_menu Implements hook_menu().
unused_modules_show_modules Page callback.
unused_modules_show_projects Page callback.