OnlyOneModuleHandler.php in Allow a content type only once (Only One) 8
File
src/OnlyOneModuleHandler.php
View source
<?php
namespace Drupal\onlyone;
use Drupal\Core\Url;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Extension\ModuleExtensionList;
class OnlyOneModuleHandler implements OnlyOneModuleHandlerInterface {
protected $moduleHandler;
protected $renderer;
protected $moduleExtensionList;
public function __construct(ModuleHandlerInterface $module_handler, RendererInterface $renderer, ModuleExtensionList $module_extension_list) {
$this->moduleHandler = $module_handler;
$this->renderer = $renderer;
$this->moduleExtensionList = $module_extension_list;
}
public function getModuleHelpPageLink($module_machine_name, $module_name_alternate, $emphasize = FALSE) {
$modules = $this->moduleExtensionList
->getAllInstalledInfo();
if (isset($modules[$module_machine_name])) {
$module_name = $modules[$module_machine_name]['name'];
if (in_array($module_machine_name, $this->moduleHandler
->getImplementations('help'))) {
$build = [
'#type' => 'link',
'#title' => $module_name,
'#url' => Url::fromRoute('help.page', [
'name' => $module_machine_name,
]),
'#cache' => [
'tags' => [
'config:core.extension',
],
],
];
$output = $this->renderer
->render($build);
}
else {
$output = $emphasize ? Markup::create('<em>' . $module_name . '</em>') : $module_name;
}
}
else {
$output = $emphasize ? Markup::create('<em>' . $module_name_alternate . '</em>') : $module_name_alternate;
}
return $output;
}
}