You are here

function _print_generate_node in Printer, email and PDF versions 5.3

Same name and namespace in other branches
  1. 5.4 print.pages.inc \_print_generate_node()
  2. 6 print.pages.inc \_print_generate_node()
  3. 7.2 print.pages.inc \_print_generate_node()
  4. 7 print.pages.inc \_print_generate_node()
  5. 5.x print.pages.inc \_print_generate_node()

Prepare a Printer-friendly-ready node body for content nodes

Parameters

$nid: node ID of the node to be rendered into a printer-friendly page

$cid: comment ID of the individual comment to be rendered

$teaser: if set to TRUE, outputs only the node's teaser

$message: optional sender's message (used by the send e-mail module)

Return value

filled array ready to be used in the template

1 call to _print_generate_node()
print_controller in ./print.pages.inc
Select the print generator function based on the page type

File

./print.pages.inc, line 486
Contains the functions to generate Printer-friendly pages.

Code

function _print_generate_node($nid, $cid = NULL, $teaser = FALSE, $message = NULL) {

  // We can take a node id
  $node = node_load(array(
    'nid' => $nid,
  ));
  if (!$node) {

    // Node not found
    drupal_not_found();
    return FALSE;
  }
  elseif (!node_access('view', $node)) {

    // Access is denied
    drupal_access_denied();
    return FALSE;
  }
  drupal_set_title($node->title);

  //alert other modules that we are generating a printer-friendly page, so they can choose to show/hide info
  $node->printing = TRUE;

  // Turn off Pagination by the Paging module
  unset($node->pages);
  unset($node->pages_count);
  unset($node->page_count);
  if ($cid === NULL) {

    // Adapted (simplified) version of node_view

    //Render the node content
    $node = node_build_content($node, $teaser, TRUE);

    // Disable fivestar widget output
    unset($node->content['fivestar_widget']);

    // Disable service links module output
    unset($node->content['service_links']);
    $content = drupal_render($node->content);
    if ($teaser) {
      $node->teaser = $content;
      unset($node->body);
    }
    else {
      $node->body = $content;
      unset($node->teaser);
    }
  }
  $print_comments = variable_get('print_comments', PRINT_COMMENTS_DEFAULT);
  if (function_exists('comment_render') && ($cid != NULL || $print_comments)) {

    //Print only the requested comment (or if $cid is NULL, all of them)
    $comments = comment_render($node, $cid);

    //Remove the comment forms
    $comments = preg_replace('!<form.*?id="comment-.*?">.*?</form>!sim', '', $comments);

    //Remove the 'Post new comment' title
    $comments = preg_replace('!<h2.*?>Post new comment</h2>!', '', $comments);

    //Remove the comment title hyperlink
    $comments = preg_replace('!(<h3.*?>)(<a.*?>)(.*?)</a>(</h3>)!', '$1$3$4', $comments);

    //Remove the comment author link
    $pattern = '!(<span class="submitted">)(.*?)<a.*?>(.*?)</a>(</span>)!sim';
    if (preg_match($pattern, $comments)) {
      $comments = preg_replace($pattern, '$1$2$3$4', $comments);
    }

    //Remove the comment links
    $comments = preg_replace('!\\s*<ul class="links">.*?</ul>!sim', '', $comments);
    if ($cid != NULL) {

      // Single comment requested, output only the comment
      unset($node->body);
    }
    $node->body .= $comments;
  }
  node_invoke_nodeapi($node, 'alter', $teaser, TRUE);

  // Convert the a href elements
  $pattern = '!<(a\\s[^>]*?)>(.*?)(</a>)!is';
  $node->body = preg_replace_callback($pattern, '_print_rewrite_urls', $node->body);
  init_theme();
  $print = _print_var_generator($node, $message, $cid);
  return $print;
}