You are here

public function AdvancedHelpManager::parseHelp in Advanced Help 8

Function to parse yml / txt files.

@todo implement cache @todo check visibility of the method.

2 calls to AdvancedHelpManager::parseHelp()
AdvancedHelpManager::getSettings in src/AdvancedHelpManager.php
Returns advanced help settings.
AdvancedHelpManager::getTopics in src/AdvancedHelpManager.php
Search the system for all available help topics.

File

src/AdvancedHelpManager.php, line 124

Class

AdvancedHelpManager
AdvancedHelp plugin manager.

Namespace

Drupal\advanced_help

Code

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")) {

        // Look for one or more README files.
        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)) {

        // Get translated titles:
        $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']);

          // Check translated strings for translatable global 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) {

          // Each topic should have a name, a title, a file and path.
          $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,
            // Require extension.
            'file' => isset($topic['readme file']) ? $file : $file . '.html',
            // Not in .ini file.
            '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,
          ];
        }
      }
    }

    // drupal_alter('advanced_help_topic_info', $ini);.
    $this
      ->cacheSet('advanced_help_ini_' . $language, $ini);
  }
  return $ini;
}