You are here

unpublished_nodes_redirect.module in Unpublished Nodes Redirect 7

Same filename and directory in other branches
  1. 8 unpublished_nodes_redirect.module

File

unpublished_nodes_redirect.module
View source
<?php

/**
 * @file
 * unpublished_nodes_redirect.module
 */

/**
 * Implements hook_menu().
 */
function unpublished_nodes_redirect_menu() {
  $items['admin/config/system/unpublished-nodes-redirect'] = array(
    'title' => 'Unpublished Nodes Redirects',
    'description' => 'Setup redirect paths for unpublished nodes.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'unpublished_nodes_redirect_admin_settings_form',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file path' => drupal_get_path('module', 'unpublished_nodes_redirect'),
    'file' => 'unpublished_nodes_redirect.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_menu_alter().
 */
function unpublished_nodes_redirect_menu_alter(&$items) {
  $items['node/%node']['access callback'] = 'unpublished_nodes_redirect_access_callback';
}

/**
 * Custom access callback to redirect the user if a node is unpublished.
 */
function unpublished_nodes_redirect_access_callback($op, $node, $account = NULL) {

  // Node is unpublished and the user is not logged in.
  if ($node->status == 0 && !user_is_logged_in()) {

    // Make sure cron isn't running.
    if (variable_get('cron_semaphore', FALSE) === FALSE && $_SERVER['SCRIPT_NAME'] !== '/cron.php' && arg(3) !== 'run-cron') {

      // Not a JS callback.
      if (arg(0) != 'js') {

        // Get the redirect path for this node type.
        $redirect_path = variable_get($node->type . '_unpublished_redirect_path', '');

        // Get the response code for this node type.
        $response_code = variable_get($node->type . '_unpublished_redirect_response_code');

        // If the redirect path exists, do a permanent redirect.
        if (!empty($redirect_path) && $response_code != 0) {
          drupal_goto($redirect_path, array(), $response_code);
        }
      }
    }
  }
  return node_access($op, $node, $account);
}

/**
 * Implements hook_module_implements_alter().
 */
function unpublished_nodes_redirect_module_implements_alter(&$implementations, $hook) {

  // Do nothing unless we implement the hook being tested (safety).
  if (isset($implementations['unpublished_nodes_redirect'])) {
    switch ($hook) {

      // Make hook menu_alter run last.
      case 'menu_alter':
        $group = $implementations['unpublished_nodes_redirect'];
        unset($implementations['unpublished_nodes_redirect']);
        $implementations['unpublished_nodes_redirect'] = $group;
        break;
    }
  }
}