View source
<?php
namespace Drupal\advanced_help;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\File\FileSystemInterface;
class AdvancedHelpManager extends DefaultPluginManager {
use StringTranslationTrait;
protected $fileSystem;
public function __construct(ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler, CacheBackendInterface $cache_backend, TranslationInterface $string_translation, FileSystemInterface $file_system) {
$this->module_handler = $module_handler;
$this->theme_handler = $theme_handler;
$this
->setStringTranslation($string_translation);
$this
->alterInfo('advanced_help');
$this
->setCacheBackend($cache_backend, 'advanced_help', [
'advanced_help',
]);
$this->fileSystem = $file_system;
}
public function getModuleList() {
$modules = $this->module_handler
->getModuleList();
$themes = $this->theme_handler
->listInfo();
$result = [];
foreach ($modules as $name => $data) {
$result[$name] = $this->module_handler
->getName($name);
}
foreach ($themes as $name => $data) {
$result[$name] = $this->theme_handler
->getName($name);
}
return $result;
}
public function getTopic($module, $topic) {
$topics = $this
->getTopics();
if (!empty($topics[$module][$topic])) {
return $topics[$module][$topic];
}
return FALSE;
}
public function getModuleName($module) {
return $this->module_handler
->getName($module);
}
public function getTopics() {
$ini = $this
->parseHelp();
return $ini['topics'];
}
public function getSettings() {
$ini = $this
->parseHelp();
return $ini['settings'];
}
public function parseHelp() {
$language = \Drupal::languageManager()
->getCurrentLanguage()
->getId();
static $ini = NULL;
$cache = $this
->cacheGet('advanced_help_ini_' . $language);
if ($cache) {
$ini = $cache->data;
}
if (!isset($ini)) {
$ini = [
'topics' => [],
'settings' => [],
];
foreach ($this->module_handler
->getModuleList() + $this->theme_handler
->listInfo() as $plugin_name => $extension) {
$module = $plugin_name;
$module_path = $extension
->getPath();
$info = [];
if (file_exists("{$module_path}/help/{$module}.help.yml")) {
$path = "{$module_path}/help";
$info = Yaml::decode(file_get_contents("{$module_path}/help/{$module}.help.yml"));
}
elseif (!file_exists("{$module_path}/help")) {
if (floatval(\Drupal::VERSION) >= 8.800000000000001) {
$files = $this->fileSystem
->scanDirectory("./{$module_path}", '/^(readme).*\\.(txt|md)$/i', [
'recurse' => FALSE,
]);
}
else {
$files = file_scan_directory("./{$module_path}", '/^(readme).*\\.(txt|md)$/i', [
'recurse' => FALSE,
]);
}
$path = "./{$module_path}";
foreach ($files as $name => $fileinfo) {
$info[$fileinfo->filename] = [
'line break' => TRUE,
'readme file' => TRUE,
'file' => $fileinfo->filename,
'title' => $fileinfo->name,
];
}
}
if (!empty($info)) {
$translation = [];
if (file_exists("{$module_path}/translations/help/{$language}/{$module}.help.yml")) {
$translation = Yaml::decode(file_get_contents("{$module_path}/translations/help/{$language}/{$module}.help.yml"));
}
$ini['settings'][$module] = [];
if (!empty($info['advanced help settings'])) {
$ini['settings'][$module] = $info['advanced help settings'];
unset($info['advanced help settings']);
if (isset($translation['advanced help settings']['name'])) {
$ini['settings']['name'] = $translation['advanced help settings']['name'];
}
if (isset($translation['advanced help settings']['index name'])) {
$ini['settings']['index name'] = $translation['advanced help settings']['index name'];
}
}
foreach ($info as $name => $topic) {
$file = !empty($topic['file']) ? $topic['file'] : $name;
$ini['topics'][$module][$name] = [
'name' => $name,
'module' => $module,
'ini' => $topic,
'title' => !empty($translation[$name]['title']) ? $translation[$name]['title'] : $topic['title'],
'weight' => isset($topic['weight']) ? $topic['weight'] : 0,
'parent' => isset($topic['parent']) ? $topic['parent'] : 0,
'popup width' => isset($topic['popup width']) ? $topic['popup width'] : 500,
'popup height' => isset($topic['popup height']) ? $topic['popup height'] : 500,
'file' => isset($topic['readme file']) ? $file : $file . '.html',
'path' => $path,
'line break' => isset($topic['line break']) ? $topic['line break'] : (isset($ini['settings'][$module]['line break']) ? $ini['settings'][$module]['line break'] : FALSE),
'navigation' => isset($topic['navigation']) ? $topic['navigation'] : (isset($ini['settings'][$module]['navigation']) ? $ini['settings'][$module]['navigation'] : TRUE),
'css' => isset($topic['css']) ? $topic['css'] : (isset($ini['settings'][$module]['css']) ? $ini['settings'][$module]['css'] : NULL),
'readme file' => isset($topic['readme file']) ? $topic['readme file'] : FALSE,
];
}
}
}
$this
->cacheSet('advanced_help_ini_' . $language, $ini);
}
return $ini;
}
public function getTopicFileInfo($module, $topic) {
$language = \Drupal::languageManager()
->getCurrentLanguage()
->getId();
$info = $this
->getTopic($module, $topic);
if (empty($info)) {
return FALSE;
}
$path_type = preg_match('/themes/', $info['path']) ? 'theme' : 'module';
$paths = [
drupal_get_path($path_type, $module) . "/translations/help/{$language}",
$info['path'],
];
foreach ($paths as $path) {
if (file_exists("{$path}/{$info['file']}")) {
return [
'path' => $path,
'file' => $info['file'],
];
}
}
return FALSE;
}
public function getTopicFileName($module, $topic) {
$info = $this
->getTopicFileInfo($module, $topic);
if ($info) {
return "./{$info['path']}/{$info['file']}";
}
}
}