You are here

function _print_generate_node in Printer, email and PDF versions 6

Same name and namespace in other branches
  1. 5.4 print.pages.inc \_print_generate_node()
  2. 5.3 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

$format: format of the page being generated

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

$message: optional sender's message (used by the send email 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 503

Code

function _print_generate_node($nid, $cid = NULL, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
  global $_print_urls;

  // We can take a node id
  $node = node_load($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(check_plain($node->title));

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

  // use the proper build_mode
  $node->build_mode = NODE_BUILD_PRINT;

  // Turn off Pagination by the Paging module
  unset($node->pages);
  unset($node->page_count);

  // Make this page a member of the original page's organic group
  if (function_exists('og_set_group_context') && isset($node->og_groups)) {
    og_set_group_context($node->og_groups);
  }
  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);

    // Disable the AdSense module ads
    $content = preg_replace('!<div class=[\'"]adsense[\'"].*?</div>!sim', '', $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.*?>' . t('Post new comment') . '</h2>!', '', $comments);

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

    // Remove the comment author link
    $pattern = '!(<(?:span|div) class="submitted">.*?)<a.*?>(.*?)</a>(.*?</(?:span|div)>)!sim';
    if (preg_match($pattern, $comments)) {
      $comments = preg_replace($pattern, '$1$2$3', $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);
  $content = theme('print_node', $node, $teaser, TRUE, $format);
  if ($teaser) {
    $content = $node->teaser;
  }

  // Check URL list settings
  $_print_urls = _print_url_list_enabled($node, $format);

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