Skip to content

Commit

Permalink
Improve logging when installing modules (#6109)
Browse files Browse the repository at this point in the history
* Improve logging when installing modules

* Update wording

Co-authored-by: Moshe Weitzman <weitzman@tejasa.com>

* Inject permission handler

---------

Co-authored-by: Moshe Weitzman <weitzman@tejasa.com>
  • Loading branch information
DieterHolvoet and weitzman authored Oct 30, 2024
1 parent 8459590 commit 65d6b7c
Showing 1 changed file with 50 additions and 2 deletions.
52 changes: 50 additions & 2 deletions src/Commands/pm/PmCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
Expand Down Expand Up @@ -63,6 +68,11 @@ public function getExtensionListModule(): ModuleExtensionList
return $this->extensionListModule;
}

public function getPermissionHandler(): PermissionHandlerInterface
{
return $this->permissionHandler;
}

/**
* Enable one or more modules.
*/
Expand Down Expand Up @@ -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('<href=%s>%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)]));
}
}
}

/**
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 65d6b7c

Please sign in to comment.