3 namespace Drupal\advanced_help;
5 use Drupal\Core\Cache\CacheBackendInterface;
6 use Drupal\Core\Extension\ModuleHandlerInterface;
7 use Drupal\Core\Extension\ThemeHandlerInterface;
8 use Drupal\Core\StringTranslation\TranslationInterface;
9 use Drupal\Core\Plugin\DefaultPluginManager;
10 use Drupal\Core\StringTranslation\StringTranslationTrait;
11 use Drupal\Component\Serialization\Yaml;
14 * AdvancedHelp plugin manager.
16 class AdvancedHelpManager extends DefaultPluginManager {
17 use StringTranslationTrait;
20 * Constructs an AdvancedHelpManager instance.
22 * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
23 * Cache backend instance to use.
24 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
25 * The module handler to invoke the alter hook with.
26 * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
28 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
29 * The string translation service.
31 public function __construct(ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler, CacheBackendInterface $cache_backend, TranslationInterface $string_translation) {
32 $this->module_handler = $module_handler;
33 $this->theme_handler = $theme_handler;
34 $this->setStringTranslation($string_translation);
35 $this->alterInfo('advanced_help');
36 $this->setCacheBackend($cache_backend, 'advanced_help', ['advanced_help']);
40 * Get the modules/theme list.
43 public function getModuleList() {
44 $modules = $this->module_handler->getModuleList();
45 $themes = $this->theme_handler->listInfo();
48 foreach ($modules + $themes as $name => $data) {
49 $result[$name] = $this->module_handler->getName($name);
55 * Get the information for a single help topic.
60 function getTopic($module, $topic) {
61 $topics = $this->getTopics();
62 if (!empty($topics[$module][$topic])) {
63 return $topics[$module][$topic];
69 * Return the name of the module.
70 * @param string $module
73 function getModuleName($module) {
74 return $this->module_handler->getName($module);
78 * Search the system for all available help topics.
79 * @todo check visibility of the method.
81 public function getTopics() {
82 $ini = $this->parseHelp();
83 return $ini['topics'];
87 * Returns advanced help settings.
88 * @todo check visibility of the method.
90 public function getSettings() {
91 $ini = $this->parseHelp();
92 return $ini['settings'];
97 * Function to parse yml / txt files.
99 * @todo implement cache
100 * @todo check visibility of the method.
102 public function parseHelp() {
103 $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
106 $cache = $this->cacheGet('advanced_help_ini_' . $language);
112 $ini = ['topics' => [], 'settings' => []];
114 foreach ($this->module_handler->getModuleList() + $this->theme_handler->listInfo() as $plugin_name => $extension) {
115 $module = $plugin_name;
116 $module_path = $extension->getPath();
119 if (file_exists("$module_path/help/$module.help.yml")) {
120 $path = "$module_path/help";
121 $info = Yaml::decode(file_get_contents("$module_path/help/$module.help.yml"));
123 elseif (!file_exists("$module_path/help")) {
124 // Look for one or more README files.
125 $files = file_scan_directory("./$module_path",
126 '/^(readme).*\.(txt|md)$/i', ['recurse' => FALSE]);
127 $path = "./$module_path";
128 foreach ($files as $name => $fileinfo) {
129 $info[$fileinfo->filename] = [
130 'line break' => TRUE,
131 'readme file' => TRUE,
132 'file' => $fileinfo->filename,
133 'title' => $fileinfo->name,
139 // Get translated titles:
141 if (file_exists("$module_path/translations/help/$language/$module.help.yml")) {
142 $translation = Yaml::decode(file_get_contents("$module_path/translations/help/$language/$module.help.yml"));
145 $ini['settings'][$module] = [];
146 if (!empty($info['advanced help settings'])) {
147 $ini['settings'][$module] = $info['advanced help settings'];
148 unset($info['advanced help settings']);
150 // Check translated strings for translatable global settings.
151 if (isset($translation['advanced help settings']['name'])) {
152 $ini['settings']['name'] = $translation['advanced help settings']['name'];
154 if (isset($translation['advanced help settings']['index name'])) {
155 $ini['settings']['index name'] = $translation['advanced help settings']['index name'];
160 foreach ($info as $name => $topic) {
161 // Each topic should have a name, a title, a file and path.
162 $file = !empty($topic['file']) ? $topic['file'] : $name;
163 $ini['topics'][$module][$name] = [
167 'title' => !empty($translation[$name]['title']) ? $translation[$name]['title'] : $topic['title'],
168 'weight' => isset($topic['weight']) ? $topic['weight'] : 0,
169 'parent' => isset($topic['parent']) ? $topic['parent'] : 0,
170 'popup width' => isset($topic['popup width']) ? $topic['popup width'] : 500,
171 'popup height' => isset($topic['popup height']) ? $topic['popup height'] : 500,
172 // Require extension.
173 'file' => isset($topic['readme file']) ? $file : $file . '.html',
176 'line break' => isset($topic['line break']) ? $topic['line break'] : (isset($ini['settings'][$module]['line break']) ? $ini['settings'][$module]['line break'] : FALSE),
177 'navigation' => isset($topic['navigation']) ? $topic['navigation'] : (isset($ini['settings'][$module]['navigation']) ? $ini['settings'][$module]['navigation'] : TRUE),
178 'css' => isset($topic['css']) ? $topic['css'] : (isset($ini['settings'][$module]['css']) ? $ini['settings'][$module]['css'] : NULL),
179 'readme file' => isset($topic['readme file']) ? $topic['readme file'] : FALSE,
184 // drupal_alter('advanced_help_topic_info', $ini);
185 $this->cacheSet('advanced_help_ini_' . $language, $ini);
191 * Load and render a help topic.
193 * @todo allow the theme override the help.
198 public function getTopicFileInfo($module, $topic) {
199 $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
201 $info = $this->getTopic($module, $topic);
206 $path_type = (preg_match('/themes/', $info['path'])) ? 'theme' : 'module';
209 // // Allow theme override.
210 // path_to_theme() . '/help',
212 drupal_get_path($path_type, $module) . "/translations/help/$language",
213 // In same directory as .inc file.
217 foreach ($paths as $path) {
218 if (file_exists("$path/$info[file]")) {
219 return ['path' => $path, 'file' => $info['file']];
226 public function getTopicFileName($module, $topic) {
227 $info = $this->getTopicFileInfo($module, $topic);
229 return "./{$info['path']}/{$info['file']}";