You are here

public function AdvancedHelpController::viewTopic in Advanced Help 8

Load and render a help topic.

@todo port the drupal_alter functionality.

Parameters

string $module: Name of the module.

string $topic: Name of the topic.

Return value

string Returns formatted topic.

1 call to AdvancedHelpController::viewTopic()
AdvancedHelpController::topicPage in src/Controller/AdvancedHelpController.php

File

src/Controller/AdvancedHelpController.php, line 282

Class

AdvancedHelpController
Class AdvancedHelpController.

Namespace

Drupal\advanced_help\Controller

Code

public function viewTopic($module, $topic, $is_modal = FALSE) {
  $file_info = $this->advanced_help
    ->getTopicFileInfo($module, $topic);
  if ($file_info) {
    $info = $this->advanced_help
      ->getTopic($module, $topic);
    $file = "{$file_info['path']}/{$file_info['file']}";
    $build = [
      '#type' => 'markup',
    ];
    if (!empty($info['css'])) {
      $build['#attached']['library'][] = $info['module'] . '/' . $info['css'];
    }
    $build['#markup'] = file_get_contents($file);
    if (isset($info['readme file']) && $info['readme file']) {
      $ext = pathinfo($file, PATHINFO_EXTENSION);
      if ('md' == $ext) {
        $build['#markup'] = '<div class="advanced-help-topic">' . Xss::filterAdmin(MarkdownExtra::defaultTransform($build['#markup'])) . '</div>';
      }
      return $build;
    }

    // Change 'topic:' to the URL for another help topic.
    preg_match('/&topic:([^"]+)&/', $build['#markup'], $matches);
    if (isset($matches[1]) && preg_match('/[\\w\\-]\\/[\\w\\-]+/', $matches[1])) {
      list($umodule, $utopic) = explode('/', $matches[1]);
      $path = new Url('advanced_help.help', [
        'module' => $umodule,
        'topic' => $utopic,
      ]);
      $build['#markup'] = preg_replace('/&topic:([^"]+)&/', $path
        ->toString(), $build['#markup']);
    }
    global $base_path;

    // Change 'path:' to the URL to the base help directory.
    $build['#markup'] = str_replace('&path&', $base_path . $info['path'] . '/', $build['#markup']);

    // Change 'trans_path:' to the URL to the actual help directory.
    $build['#markup'] = str_replace('&trans_path&', $base_path . $file_info['path'] . '/', $build['#markup']);

    // Change 'base_url:' to the URL to the site.
    $build['#markup'] = preg_replace('/&base_url&([^"]+)"/', $base_path . '$1' . '"', $build['#markup']);

    // Run the line break filter if requested.
    if (!empty($info['line break'])) {

      // Remove the header since it adds an extra <br /> to the filter.
      $build['#markup'] = preg_replace('/^<!--[^\\n]*-->\\n/', '', $build['#markup']);
      $build['#markup'] = _filter_autop($build['#markup']);
    }
    if (!empty($info['navigation']) && !$is_modal) {
      $topics = $this->advanced_help
        ->getTopics();
      $topics = $this
        ->getTopicHierarchy($topics);
      if (!empty($topics[$module][$topic]['children'])) {
        $items = $this
          ->getTree($topics, $topics[$module][$topic]['children']);
        $links = [
          '#theme' => 'item_list',
          '#items' => $items,
        ];
        $build['#markup'] .= \Drupal::service('renderer')
          ->render($links, FALSE);
      }
      list($parent_module, $parent_topic) = $topics[$module][$topic]['_parent'];
      if ($parent_topic) {
        $parent = $topics[$module][$topic]['_parent'];
        $up = new Url('advanced_help.help', [
          'module' => $parent[0],
          'topic' => $parent[1],
        ]);
      }
      else {
        $up = new Url('advanced_help.module_index', [
          'module' => $module,
        ]);
      }
      $siblings = $topics[$parent_module][$parent_topic]['children'];
      uasort($siblings, [
        $this,
        'helpUasort',
      ]);
      $prev = $next = NULL;
      $found = FALSE;
      foreach ($siblings as $sibling) {
        list($sibling_module, $sibling_topic) = $sibling;
        if ($found) {
          $next = $sibling;
          break;
        }
        if ($sibling_module == $module && $sibling_topic == $topic) {
          $found = TRUE;
          continue;
        }
        $prev = $sibling;
      }
      if ($prev || $up || $next) {
        $navigation = '<div class="help-navigation clear-block">';
        if ($prev) {
          $navigation .= Link::fromTextAndUrl('«« ' . $topics[$prev[0]][$prev[1]]['title'], Url::fromRoute('advanced_help.help', [
            'module' => $prev[0],
            'topic' => $prev[1],
          ], [
            'attributes' => [
              'class' => 'help-left',
            ],
          ]))
            ->toString();
        }
        if ($up) {
          $navigation .= Link::fromTextAndUrl($this
            ->t('Up'), $up
            ->setOption('attributes', [
            'class' => $prev ? 'help-up' : 'help-up-noleft',
          ]))
            ->toString();
        }
        if ($next) {
          $navigation .= Link::fromTextAndUrl($topics[$next[0]][$next[1]]['title'] . ' »»', Url::fromRoute('advanced_help.help', [
            'module' => $next[0],
            'topic' => $next[1],
          ], [
            'attributes' => [
              'class' => 'help-right',
            ],
          ]))
            ->toString();
        }
        $navigation .= '</div>';
        $build['#markup'] .= $navigation;
      }
    }
    $build['#markup'] = '<div class="advanced-help-topic">' . $build['#markup'] . '</div>';

    // drupal_alter('advanced_help_topic', $output, $popup);.
    return $build;
  }
}