You are here

function node_preview in Drupal 7

Same name and namespace in other branches
  1. 4 modules/node.module \node_preview()
  2. 5 modules/node/node.module \node_preview()
  3. 6 modules/node/node.pages.inc \node_preview()

Generates a node preview.

Parameters

$node: The node to preview.

Return value

An HTML-formatted string of a node preview.

See also

node_form_build_preview()

1 call to node_preview()
node_form_build_preview in modules/node/node.pages.inc
Form submission handler for node_form().
1 string reference to 'node_preview'
node_update_7004 in modules/node/node.install
Extend the existing default preview and teaser settings to all node types.

File

modules/node/node.pages.inc, line 373
Page callbacks for adding, editing, deleting, and revisions management for content.

Code

function node_preview($node) {

  // Clone the node before previewing it to prevent the node itself from being
  // modified.
  $cloned_node = clone $node;
  if (node_access('create', $cloned_node) || node_access('update', $cloned_node)) {
    _field_invoke_multiple('load', 'node', array(
      $cloned_node->nid => $cloned_node,
    ));

    // Load the user's name when needed.
    if (isset($cloned_node->name)) {

      // The use of isset() is mandatory in the context of user IDs, because
      // user ID 0 denotes the anonymous user.
      if ($user = user_load_by_name($cloned_node->name)) {
        $cloned_node->uid = $user->uid;
        $cloned_node->picture = $user->picture;
      }
      else {
        $cloned_node->uid = 0;

        // anonymous user
      }
    }
    elseif ($cloned_node->uid) {
      $user = user_load($cloned_node->uid);
      $cloned_node->name = $user->name;
      $cloned_node->picture = $user->picture;
    }
    $cloned_node->changed = REQUEST_TIME;
    $nodes = array(
      $cloned_node->nid => $cloned_node,
    );

    // Display a preview of the node.
    if (!form_get_errors()) {
      $cloned_node->in_preview = TRUE;
      $output = theme('node_preview', array(
        'node' => $cloned_node,
      ));
      unset($cloned_node->in_preview);
    }
    drupal_set_title(t('Preview'), PASS_THROUGH);
    return $output;
  }
}