You are here

function internal_nodes_url_outbound_alter in Internal Nodes 7

Implements hook_url_outbound_alter().

Disabled by default in the configuration: admin/config/search/internal-nodes This function is called many times per page, so I've tried to make it as efficient as possible. Multiple if statements are ordered to reduce overall cpu cycles for most use cases.

File

./internal_nodes.module, line 186
Internal nodes

Code

function internal_nodes_url_outbound_alter(&$path, &$options, $original_path) {

  //If function is enabled.
  if (variable_get('internal_nodes_outbound_alter', 0)) {

    //If path is for a node.
    if (preg_match('|^node/([0-9]+)$|', $path, $matches)) {
      global $user;

      // No node_load here! Note: Specifically don't want db_rewrite_sql() here.
      $result = db_query('SELECT type FROM {node} n WHERE nid = :nid', array(
        ':nid' => $matches[1],
      ))
        ->fetchAssoc();

      // If enabled for content type, and user doesn't have access.
      if (variable_get('internal_nodes_outbound_alter_' . $result['type'], 0) && !user_access('access ' . $result['type'] . ' node view', $user)) {

        // I'd rather not run node_load at all, but tokens can't work without it.
        $node = node_load($matches[1]);
        if ($node->internal_nodes['action'] == INTERNAL_NODES_REDIRECT) {
          $redirect = internal_nodes_create_redirect($node->internal_nodes['url'], $node);
          $path = $redirect['path'];
          $options = array_merge($options, $redirect['options']);
          $options['alias'] = TRUE;

          // Lies! Otherwise the path will get "fixed" to the URL alias.
        }

        // If not 301, do nothing.
        return;
      }
    }
  }
}