You are here

function _intlinks_process_bad_link in Internal Links 6

Same name and namespace in other branches
  1. 8 intlinks_hide_bad_links.inc \_intlinks_process_bad_link()
  2. 7.2 intlinks_hide_bad_links.inc \_intlinks_process_bad_link()
  3. 7 intlinks_hide_bad_links.inc \_intlinks_process_bad_link()

Processes regex matches of links for intlinks_hide_bad_filter_process().

If no changes should be made (e.g. if link is external or is to a published node), the function simply returns $matches[0], the unchanged link.

However, if $matches[1] isn't a published node, function returns $matches[2], the text (and any other tags) between the <a> tagset.

Example link $matches array for external link: [0]: <a href="http://jetlag.info" title="Good info about jetlag"><em>jetlag</em></a> [1]: http://jetlag.info [2]: <em>Jetlag</em>

Example link $matches array for internal link with URL alias: [0]: <a href="/articles/random-content.html">random article</a> [1]: /articles/random-content.html [2]: random article

Example link to internal "normal Drupal path" Note: "#anchor" is possible and should be dealt with before looking up nodes. [0]: <a href="/node/2#jetlag" title="Read more about jetlag">jetlag</a> [1]: /node/2#jetlag [2]: jetlag

Parameters

$matches: Parameter passed by intlinks_hide_bad_filter_process(); an array from regex match of the link.

Return value

Original link, or link stripped of <a> tagset, if it seems visitor would have got a "page not found" error.

1 string reference to '_intlinks_process_bad_link'
intlinks_filter in ./intlinks.module
Implementation of hook_filter().

File

./intlinks_hide_bad_links.inc, line 107

Code

function _intlinks_process_bad_link($matches) {
  if ($matches[1][0] != "/") {

    // Do nothing if the link is not internal (root relative).
    return $matches[0];

    // All links *should* be root-relative.
    // If you want this filter to ignore a link, make it "absolute". ;-)
    // @TODO: Possibly add handling for document-relative links.
  }

  //Strip the leading slash from link path to look up.
  $path = ltrim($matches[1], '/');
  $path_parts = explode("#", $path);

  // We don't want the "anchor", if there is one.
  $path = $path_parts[0];

  // We also don't want any arguments for special paths.
  $path_parts = explode("&", $path);
  $path = $path_parts[0];
  if (strpos($path, 'node/') === FALSE) {
    $node_path = drupal_get_normal_path($path);
    if ($path != $node_path) {

      // Drupal successfully got a node path from $path (the URL alias)
      $nid = ltrim($node_path, 'node/');
      if (intlinks_is_published($nid)) {

        // Return link, unchanged.
        return $matches[0];
      }
      else {

        // Remove link from text if node isn't published.
        return $matches[2];
      }
    }
    else {

      // Attempt to look up a node path for the alias failed.
      // Look in the menu_router table and see what we find there.
      if (intlinks_is_special_path($path)) {

        // Return the link, unchanged; it's a special Drupal path or Views
        // display path that's linked; we don't want to remove those links.
        return $matches[0];
      }

      // Otherwise hide the link; it will most likely not impress visitors.
      // @TODO: Possibly log such links (once) and pages where they are found
      // to assist with troubleshooting and site maintenance.
      return $matches[2];
    }
  }
  else {

    // We have a path that might be a node path. Test it.
    if ($nid = intlinks_is_node_path($path)) {
      if (intlinks_is_published($nid)) {
        return $matches[0];
      }
      else {
        return $matches[2];
      }
    }
    else {
      if (intlinks_is_special_path($path)) {

        // Return the link, unchanged; it's a special Drupal path or Views
        // display path that's linked; we don't want to remove those links.
        return $matches[0];
      }

      // Otherwise hide the link.
      return $matches[2];
    }
  }
}