You are here

function intlinks_filter in Internal Links 6

Implementation of hook_filter().

The bulk of filtering work is done here. This hook is quite complicated, so we'll discuss each operation it defines.

File

./intlinks.module, line 52
Input filters which rewrite internal links and/or unlink unpublished nodes.

Code

function intlinks_filter($op, $delta = 0, $format = -1, $text = '') {

  //
  if ($op == 'list') {
    return array(
      0 => t('Internal links title filter'),
      1 => t('Internal links hide bad filter'),
    );
  }
  switch ($delta) {

    // This is the internal links "title" filter which adds node titles for
    // root relative links (if no title already exists), and replaces node/123
    // -type paths with URL aliases (if an alias exists).
    case 0:
      switch ($op) {

        // This description is shown in the administrative interface, unlike the
        // filter tips which are shown in the content editing interface.
        case 'description':
          return t('Identifies internal links in content and adds an HTML "title" attribute (if none already exists), using the linked  node\'s node title. Also re-writes links to "normal Drupal paths" (e.g. node/123) as path alias if an alias exists.');

        // We don't need the "prepare" operation for this filter.
        case 'prepare':
          return $text;

        // Process internal links to add title elements (using the "title" of
        // the linked node in the HTML title element). Also replace "normal"
        // Drupal paths (e.g. node/123) with a path alias, if one exists.
        case 'process':
          include_once 'intlinks_title_filter.inc';
          return preg_replace_callback('%<a([^>]*?href="([^"]+?)"[^>]*?)>%i', "_intlinks_title_process_link", $text);
      }
      break;

    // This is the "hide bad" internal links filter.
    case 1:
      switch ($op) {

        // This description is shown in the administrative interface.
        case 'description':
          return t('Identifies internal links in content and adds an HTML "title" attribute (if none already exists), using the linked node\'s node title. Also re-writes links to "normal Drupal paths" (e.g. node/123) as path alias if an alias exists.');
        case 'prepare':
          return $text;

        // Process internal links to unpublished or non-existent content,
        // replacing linked text with the same text, unlinked.
        // Internal links to bad paths are removed, leaving just the unlinked
        // text (what was between the <a> tagset).
        // If all is well, it simply returns the unchanged link. Allows
        // editorial staff to link unpublished articles and not have links
        // show up till the linked content is published.
        case 'process':
          include_once 'intlinks_hide_bad_links.inc';
          return preg_replace_callback('%<a[^>]*?href="([^"]+?)"[^>]*?>(.*?)</a>%i', "_intlinks_process_bad_link", $text);
      }
      break;
  }
}