You are here

function pagepreview_init in Page Preview 6

Implements hook_init().

We need drupal_is_front_page() to reflect the page being previewed, not the path of the pagepreview callback. Since the result of drupal_is_front_page() is statically cached, we have to call it first and trick it.

File

./pagepreview.module, line 70
An alternative node previewing system for the node add/edit form.

Code

function pagepreview_init() {

  // We only want to do this on pagepreview requests.
  $path = $_GET['q'];
  $parts = explode('/', $path);
  if ($parts[0] == 'pagepreview' && isset($parts[1])) {

    // Get the cached temporary node.
    $form_token = drupal_get_token($parts[1]);
    $cache = cache_get('pagepreview:' . $form_token, 'cache_page');

    // If we can't find a cached node, might as well quit here.
    if (!$cache) {
      print t('There was a problem generating the preview. Please review the form for error and try again.');
      exit;
    }

    // Switch $_GET['q'] to the expected path, call drupal_is_front_page() to
    // set the static cache, the switch $_GET['q'] back to the original.
    $node = $cache->data;
    if ($node->nid) {
      $_GET['q'] = 'node/' . $node->nid;
    }
    drupal_is_front_page();
    $_GET['q'] = $path;

    // Meanwhile, don't allow the preview result to be cached.
    $GLOBALS['conf']['cache'] = FALSE;
  }
}