You are here

function advanced_help_view_topic in Advanced Help 7

Same name and namespace in other branches
  1. 5 advanced_help.module \advanced_help_view_topic()
  2. 6 advanced_help.module \advanced_help_view_topic()

Load and render a help topic.

Parameters

string $module: Name of the module.

string $topic: Name of the topic.

bool $popup: Whether to show in popup or not.

Return value

string Returns formatted topic.

2 calls to advanced_help_view_topic()
advanced_help_search_execute in ./advanced_help.module
Implements hook_search_execute().
advanced_help_topic_page in ./advanced_help.module
Page callback to view a help topic.

File

./advanced_help.module, line 690
Pluggable system to provide advanced help facilities for Drupal and modules.

Code

function advanced_help_view_topic($module, $topic, $popup = FALSE) {
  $file_info = advanced_help_get_topic_file_info($module, $topic);
  if ($file_info) {
    $info = advanced_help_get_topic($module, $topic);
    $file = "./{$file_info['path']}/{$file_info['file']}";

    // Fix invalid byte sequences (https://www.drupal.org/node/2659746).
    $output = file_get_contents($file);
    mb_substitute_character(0xfffd);
    $output = mb_convert_encoding($output, 'UTF-8', 'UTF-8');
    if (isset($info['readme file']) && $info['readme file']) {
      $ext = pathinfo($file, PATHINFO_EXTENSION);
      if ('md' == $ext && module_exists('markdown')) {
        $filters = module_invoke('markdown', 'filter_info');
        $md_info = $filters['filter_markdown'];
        if (function_exists($md_info['process callback'])) {
          $function = $md_info['process callback'];
          $output = '<div class="advanced-help-topic">' . filter_xss_admin($function($output, NULL)) . '</div>';
        }
        else {
          $output = '<div class="advanced-help-topic"><pre class="readme">' . check_plain($output) . '</pre></div>';
        }
      }
      else {
        $readme = '';
        if ('md' == $ext) {
          $readme .= '<p>' . t('If you install the !module module, the text below will be filtered by the module, producing rich text.', array(
            '!module' => l(t('Markdown'), 'https://www.drupal.org/project/markdown', array(
              'attributes' => array(
                'title' => t('Link to project.'),
              ),
            )),
          )) . '</p>';
        }
        $readme .= '<div class="advanced-help-topic"><pre class="readme">' . check_plain($output) . '</pre></div>';
        $output = $readme;
      }
      return $output;
    }

    // Make some exchanges. The strtr is because url() translates $ into %24
    // but we need to change it back for the regex replacement.
    //
    // Change 'topic:' to the URL for another help topic.
    if ($popup) {
      $output = preg_replace('/href="topic:([^"]+)"/', 'href="' . strtr(url('help/$1', array(
        'query' => array(
          'popup' => 'true',
        ),
      )), array(
        '%24' => '$',
      )) . '"', $output);
      $output = preg_replace('/src="topic:([^"]+)"/', 'src="' . strtr(url('help/$1', array(
        'query' => array(
          'popup' => 'true',
        ),
      )), array(
        '%24' => '$',
      )) . '"', $output);
      $output = preg_replace('/&topic:([^"]+)&/', strtr(url('help/$1', array(
        'query' => array(
          'popup' => 'true',
        ),
      )), array(
        '%24' => '$',
      )), $output);
    }
    else {
      $output = preg_replace('/href="topic:([^"]+)"/', 'href="' . strtr(url('help/$1'), array(
        '%24' => '$',
      )) . '"', $output);
      $output = preg_replace('/src="topic:([^"]+)"/', 'src="' . strtr(url('help/$1'), array(
        '%24' => '$',
      )) . '"', $output);
      $output = preg_replace('/&topic:([^"]+)&/', strtr(url('help/$1'), array(
        '%24' => '$',
      )), $output);
    }
    global $base_path;

    // Change 'path:' to the URL to the base help directory.
    $output = preg_replace('/href="path:([^"]+)"/', 'href="' . $base_path . $info['path'] . '/$1"', $output);
    $output = preg_replace('/src="path:([^"]+)"/', 'src="' . $base_path . $info['path'] . '/$1"', $output);
    $output = str_replace('&path&', $base_path . $info['path'] . '/', $output);

    // Change 'trans_path:' to the URL to the actual help directory.
    $output = preg_replace('/href="trans_path:([^"]+)"/', 'href="' . $base_path . $file_info['path'] . '/$1"', $output);
    $output = preg_replace('/src="trans_path:([^"]+)"/', 'src="' . $base_path . $file_info['path'] . '/$1"', $output);
    $output = str_replace('&trans_path&', $base_path . $file_info['path'] . '/', $output);

    // Change 'base_url:' to the URL to the site.
    $output = preg_replace('/href="base_url:([^"]+)"/', 'href="' . strtr(url('$1'), array(
      '%24' => '$',
    )) . '"', $output);
    $output = preg_replace('/src="base_url:([^"]+)"/', 'src="' . strtr(url('$1'), array(
      '%24' => '$',
    )) . '"', $output);
    $output = preg_replace('/&base_url&([^"]+)"/', strtr(url('$1'), array(
      '%24' => '$',
    )) . '"', $output);

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

      // Remove the header since it adds an extra <br /> to the filter.
      $output = preg_replace('/^<!--[^\\n]*-->\\n/', '', $output);
      $output = _filter_autop($output);
    }
    if (!empty($info['ini']['format'])) {
      $filter = $info['ini']['format'];
      if (filter_format_exists($filter)) {
        $output = check_markup($output, $filter);
      }
      else {
        drupal_set_message(t('Ini-file request non-existing format: “!format”.', array(
          '!format' => $filter,
        )), 'error');
      }
    }
    if (!empty($info['navigation'])) {
      $topics = advanced_help_get_topics();
      advanced_help_get_topic_hierarchy($topics);
      if (!empty($topics[$module][$topic]['children'])) {
        $items = advanced_help_get_tree($topics, $topics[$module][$topic]['children']);
        $output .= theme('item_list', array(
          'items' => $items,
        ));
      }
      list($parent_module, $parent_topic) = $topics[$module][$topic]['_parent'];
      if ($parent_topic) {
        $parent = $topics[$module][$topic]['_parent'];
        $up = "help/{$parent[0]}/{$parent[1]}";
      }
      else {
        $up = "admin/help/ah/{$module}";
      }
      $siblings = $topics[$parent_module][$parent_topic]['children'];
      uasort($siblings, 'advanced_help_uasort');
      $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 .= advanced_help_l('«« ' . $topics[$prev[0]][$prev[1]]['title'], "help/{$prev[0]}/{$prev[1]}", array(
            'attributes' => array(
              'class' => 'help-left',
            ),
          ));
        }
        if ($up) {
          $navigation .= advanced_help_l(t('Up'), $up, array(
            'attributes' => array(
              'class' => $prev ? 'help-up' : 'help-up-noleft',
            ),
          ));
        }
        if ($next) {
          $navigation .= advanced_help_l($topics[$next[0]][$next[1]]['title'] . ' »»', "help/{$next[0]}/{$next[1]}", array(
            'attributes' => array(
              'class' => 'help-right',
            ),
          ));
        }
        $navigation .= '</div>';
        $output .= $navigation;
      }
    }
    if (!empty($info['css'])) {
      drupal_add_css($info['path'] . '/' . $info['css']);
    }
    $output = '<div class="advanced-help-topic">' . $output . '</div>';
    drupal_alter('advanced_help_topic', $output, $popup);
    return $output;
  }
}