From 65d6b7c8f3dc87ea785607766a1c2ab58a85f581 Mon Sep 17 00:00:00 2001 From: Dieter Holvoet Date: Wed, 30 Oct 2024 14:05:51 +0100 Subject: [PATCH] Improve logging when installing modules (#6109) * Improve logging when installing modules * Update wording Co-authored-by: Moshe Weitzman * Inject permission handler --------- Co-authored-by: Moshe Weitzman --- src/Commands/pm/PmCommands.php | 52 ++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/src/Commands/pm/PmCommands.php b/src/Commands/pm/PmCommands.php index 82100f89dc..a00af96b83 100644 --- a/src/Commands/pm/PmCommands.php +++ b/src/Commands/pm/PmCommands.php @@ -8,11 +8,15 @@ use Consolidation\AnnotatedCommand\Hooks\HookManager; use Consolidation\OutputFormatters\StructuredData\RowsOfFields; use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\Core\Extension\Extension; use Drupal\Core\Extension\MissingDependencyException; use Drupal\Core\Extension\ModuleExtensionList; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Extension\ModuleInstallerInterface; use Drupal\Core\Extension\ThemeHandlerInterface; +use Drupal\Core\Link; +use Drupal\Core\Url; +use Drupal\user\PermissionHandlerInterface; use Drush\Attributes as CLI; use Drush\Commands\AutowireTrait; use Drush\Commands\DrushCommands; @@ -33,7 +37,8 @@ public function __construct( protected ModuleInstallerInterface $moduleInstaller, protected ModuleHandlerInterface $moduleHandler, protected ThemeHandlerInterface $themeHandler, - protected ModuleExtensionList $extensionListModule + protected ModuleExtensionList $extensionListModule, + protected PermissionHandlerInterface $permissionHandler ) { parent::__construct(); } @@ -63,6 +68,11 @@ public function getExtensionListModule(): ModuleExtensionList return $this->extensionListModule; } + public function getPermissionHandler(): PermissionHandlerInterface + { + return $this->permissionHandler; + } + /** * Enable one or more modules. */ @@ -93,7 +103,20 @@ public function install(array $modules): void if (batch_get()) { drush_backend_batch_process(); } - $this->logger()->success(dt('Successfully installed: !list', $todo_str)); + + $moduleData = $this->getExtensionListModule()->getList(); + foreach ($todo as $moduleName) { + $links = $this->getModuleLinks($moduleData[$moduleName]); + $links = array_map(function ($link) { + return sprintf('%s', $link->getUrl()->setAbsolute()->toString(), $link->getText()); + }, $links); + + if ($links === []) { + $this->logger()->success(dt('Module %name has been installed.', ['%name' => $moduleName])); + } else { + $this->logger()->success(dt('Module %name has been installed. (%links)', ['%name' => $moduleName, '%links' => implode(' - ', $links)])); + } + } } /** @@ -371,4 +394,29 @@ public function addUninstallDependencies($modules) } return $module_list; } + + protected function getModuleLinks(Extension $module): array + { + $links = []; + + // Generate link for module's help page. Assume that if a hook_help() + // implementation exists then the module provides an overview page, rather + // than checking to see if the page exists, which is costly. + if ($this->getModuleHandler()->moduleExists('help') && $module->status && $this->getModuleHandler()->hasImplementations('help', $module->getName())) { + $links[] = Link::fromTextAndUrl(dt('Help'), Url::fromRoute('help.page', ['name' => $module->getName()])); + } + + // Generate link for module's permissions page. + if ($module->status && $this->getPermissionHandler()->moduleProvidesPermissions($module->getName())) { + $links[] = Link::fromTextAndUrl(dt('Permissions'), Url::fromRoute('user.admin_permissions.module', ['modules' => $module->getName()])); + } + + // Generate link for module's configuration page, if it has one. + if ($module->status && isset($module->info['configure'])) { + $route_parameters = $module->info['configure_parameters'] ?? []; + $links[] = Link::fromTextAndUrl(dt('Configure'), Url::fromRoute($module->info['configure'], $route_parameters)); + } + + return $links; + } }