Created
August 20, 2012 19:05
-
-
Save rjpeter2/3406766 to your computer and use it in GitHub Desktop.
Show a teaser (or other view mode) on content the user does not have access to.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implements hook_page_delivery_callback_alter(). | |
* | |
* The idea for this came from this page: | |
* http://drupal.stackexchange.com/questions/327/how-can-a-module-detect-when-drupal-is-outputting-the-access-denied-page | |
*/ | |
function custom_page_delivery_callback_alter(&$callback) { | |
if ($callback == 'drupal_deliver_html_page') { | |
$callback = 'custom_deliver_html_page'; | |
} | |
} | |
/** | |
* This is a copy of drupal_deliver_html_page(). | |
* | |
* The MENU_ACCESS_DENIED section has been modified to show a view mode | |
* for a user who does not have access to the Press Kit node. | |
*/ | |
function custom_deliver_html_page($page_callback_result) { | |
// Emit the correct charset HTTP header, but not if the page callback | |
// result is NULL, since that likely indicates that it printed something | |
// in which case, no further headers may be sent, and not if code running | |
// for this page request has already set the content type header. | |
if (isset($page_callback_result) && is_null(drupal_get_http_header('Content-Type'))) { | |
drupal_add_http_header('Content-Type', 'text/html; charset=utf-8'); | |
} | |
// Send appropriate HTTP-Header for browsers and search engines. | |
global $language; | |
drupal_add_http_header('Content-Language', $language->language); | |
// Menu status constants are integers; page content is a string or array. | |
if (is_int($page_callback_result)) { | |
// @todo: Break these up into separate functions? | |
switch ($page_callback_result) { | |
case MENU_NOT_FOUND: | |
// Print a 404 page. | |
drupal_add_http_header('Status', '404 Not Found'); | |
watchdog('page not found', check_plain($_GET['q']), NULL, WATCHDOG_WARNING); | |
// Check for and return a fast 404 page if configured. | |
drupal_fast_404(); | |
// Keep old path for reference, and to allow forms to redirect to it. | |
if (!isset($_GET['destination'])) { | |
$_GET['destination'] = $_GET['q']; | |
} | |
$path = drupal_get_normal_path(variable_get('site_404', '')); | |
if ($path && $path != $_GET['q']) { | |
// Custom 404 handler. Set the active item in case there are tabs to | |
// display, or other dependencies on the path. | |
menu_set_active_item($path); | |
$return = menu_execute_active_handler($path, FALSE); | |
} | |
if (empty($return) || $return == MENU_NOT_FOUND || $return == MENU_ACCESS_DENIED) { | |
// Standard 404 handler. | |
drupal_set_title(t('Page not found')); | |
$return = t('The requested page "@path" could not be found.', array('@path' => request_uri())); | |
} | |
drupal_set_page_content($return); | |
$page = element_info('page'); | |
print drupal_render_page($page); | |
break; | |
case MENU_ACCESS_DENIED: | |
// Print a 403 page. | |
drupal_add_http_header('Status', '403 Forbidden'); | |
watchdog('access denied', check_plain($_GET['q']), NULL, WATCHDOG_WARNING); | |
// Keep old path for reference, and to allow forms to redirect to it. | |
if (!isset($_GET['destination'])) { | |
$_GET['destination'] = $_GET['q']; | |
} | |
$path = drupal_get_normal_path(variable_get('site_403', '')); | |
if ($path && $path != $_GET['q']) { | |
// Custom 403 handler. Set the active item in case there are tabs to | |
// display or other dependencies on the path. | |
menu_set_active_item($path); | |
$return = menu_execute_active_handler($path, FALSE); | |
} | |
if (empty($return) || $return == MENU_NOT_FOUND || $return == MENU_ACCESS_DENIED) { | |
// Standard 403 handler. | |
drupal_set_title(t('Access denied')); | |
$return = t('You are not authorized to access this page.'); | |
} | |
// Load the view mode for the non-logged in version of this node. | |
// Have to use node_load()/arg() here because menu_get_object() returns nothing. | |
$node = node_load(arg(1)); | |
if ($node->type = 'press_kit') { | |
$return = node_view($node, 'press_kit_not_authenticated'); | |
} | |
if (isset($return['links']['node']['#links']['node-readmore'])) { | |
unset($return['links']['node']['#links']['node-readmore']); | |
} | |
print drupal_render_page($return); | |
break; | |
case MENU_SITE_OFFLINE: | |
// Print a 503 page. | |
drupal_maintenance_theme(); | |
drupal_add_http_header('Status', '503 Service unavailable'); | |
drupal_set_title(t('Site under maintenance')); | |
print theme('maintenance_page', array('content' => filter_xss_admin(variable_get('maintenance_mode_message', | |
t('@site is currently under maintenance. We should be back shortly. Thank you for your patience.', array('@site' => variable_get('site_name', 'Drupal'))))))); | |
break; | |
} | |
} | |
elseif (isset($page_callback_result)) { | |
// Print anything besides a menu constant, assuming it's not NULL or | |
// undefined. | |
print drupal_render_page($page_callback_result); | |
} | |
// Perform end-of-request tasks. | |
drupal_page_footer(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment