module_instructions-2.0.x-dev/src/Controller/ModuleInstructionsController.php
src/Controller/ModuleInstructionsController.php
<?php
namespace Drupal\module_instructions\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Extension\ExtensionPathResolver;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Base controller for the 'module instructions' module.
*/
class ModuleInstructionsController extends ControllerBase {
/**
* Returns the extension.path.resolver service.
*
* @var \Drupal\Core\Extension\ExtensionPathResolver
*/
protected $pathResolver;
/**
* Returns the module_handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Constructs a ModuleInstructionsController object.
*
* @param \Drupal\Core\Extension\ExtensionPathResolver $path_resolver
* Factory for getting extension lists by type.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* Interface for classes that manage a set of enabled modules.
*/
public function __construct(ExtensionPathResolver $path_resolver, ModuleHandlerInterface $module_handler) {
$this->pathResolver = $path_resolver;
$this->moduleHandler = $module_handler;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('extension.path.resolver'),
$container->get('module_handler')
);
}
/**
* Creates a title for the instruction page.
*/
public function getTitle($module, $file) {
return $this->t('@module (@file)', ['@module' => $module, '@file' => $file]);
}
/**
* Displays the instruction file.
*/
public function instruction($module, $file) {
$fullpath = $this->pathResolver->getPath('module', $module) . '/' . $file;
$filter = (object) [];
$filter->settings = [
'filter_url_length' => 256,
];
if (file_exists($fullpath)) {
$content = file_get_contents($fullpath);
$context = [
'module' => $module,
'file' => $file,
'format' => 'full_html',
];
$this->moduleHandler->alter('module_instructions_pre_view', $content, $context);
return [
'#markup' => '<pre>' . check_markup(_filter_url($content, $filter), $context['format']) . '</pre>',
];
}
throw new NotFoundHttpException();
}
}
