You are here

function rabbit_hole_node_view in Rabbit Hole 7

Implements hook_node_view().

File

./rabbit_hole.module, line 216
Main module file for Rabbit Hole.

Code

function rabbit_hole_node_view($node, $view_mode, $langcode) {
  if ($view_mode != 'full' || !preg_match('/node\\/' . $node->nid . '(\\/view|)$/', current_path()) || user_access('bypass rabbit hole')) {

    // The node is not being viewed at it's own page, or the user is able to
    // bypass Rabbit Hole, exit early.
    return;
  }

  // Get the action. Use the one specified for this node, or fallback to the
  // default value for the content type.
  $action = isset($node->rabbit_hole_action) ? $node->rabbit_hole_action != RABBIT_HOLE_USE_DEFAULT ? $node->rabbit_hole_action : variable_get('rabbit_hole_action_' . $node->type) : variable_get('rabbit_hole_action_' . $node->type);

  // If we should perform a redirect, we will also get the path and response.
  if ($action == RABBIT_HOLE_PAGE_REDIRECT) {
    if (isset($node->rabbit_hole_action) && $node->rabbit_hole_action != RABBIT_HOLE_USE_DEFAULT) {

      // Get the redirect path and response from the node.
      $redirect_path = token_replace($node->rabbit_hole_redirect, array(
        'node' => $node,
      ));
      $redirect_response = $node->rabbit_hole_redirect_response;
    }
    else {

      // Get the redirect path and response from the content type.
      $redirect_path = token_replace(variable_get('rabbit_hole_redirect_' . $node->type, '<front>'), array(
        'node' => $node,
      ));
      $redirect_response = variable_get('rabbit_hole_redirect_response_' . $node->type, RABBIT_HOLE_PAGE_REDIRECT_RESPONSE_DEFAULT);
    }
  }

  // Now, let's see what we should do.
  switch ($action) {
    case RABBIT_HOLE_ACCESS_DENIED:

      // TODO: Is this the proper way to deliver an access denied page?
      drupal_access_denied();
      exit;
    case RABBIT_HOLE_PAGE_NOT_FOUND:

      // TODO: Is this the proper way to deliver a not found page?
      drupal_not_found();
      exit;
    case RABBIT_HOLE_PAGE_REDIRECT:

      // Redirect the user to the specified path.
      drupal_goto($redirect_path, array(), $redirect_response);
  }
}