You are here

recipe_html.module in Recipe 7.2

Enables a print view for recipes.

This supports full 8-1/2" x 11", and 5"x7" and 3"x5" index cards. Some printers may not be able to deal with small page sizes like this. They may have to print on 8-1/2"x11" paper and cut.

File

modules/recipe_html.module
View source
<?php

/**
 * @file
 * Enables a print view for recipes.
 *
 * This supports full 8-1/2" x 11", and 5"x7" and 3"x5" index cards.
 * Some printers may not be able to deal with small page sizes like this.
 * They may have to print on 8-1/2"x11" paper and cut.
 */

/**
 * Implements hook_recipeio().
 */
function recipe_html_recipeio($type) {
  $supported = array(
    'export_single' => array(
      'format_name' => t('Print View'),
      'callback' => 'recipe_html_export_single',
      'format_help' => t('NOTE: You must use your web browser\'s print dialog to change the page size and orientation.'),
      'access arguments' => 'access content',
    ),
  );
  if (isset($supported[$type])) {

    // Key needs to be lower case.
    return array(
      'recipeprint' => $supported[$type],
    );
  }
  else {
    return FALSE;
  }
}
function recipe_html_export_single($nid = NULL, $yield = NULL) {
  if ($nid === NULL) {
    drupal_set_message(t('Recipe not found.'));
    drupal_not_found();
    return;
  }
  $node = node_load($nid);

  // Set the custom yield so we can scale up/down the recipe quantities.
  $node->recipe_custom_yield = $yield;

  // Remove the yield buttons.
  $node->recipe_show_yield_form = FALSE;

  // you should not be able to export unpublished recipes
  if ($node->status == 0) {
    drupal_access_denied();
    return;
  }
  $build = node_view($node, 'print', $yield);

  // Don't pass to theme handlers.
  unset($build['#theme']);

  // Don't want to show links in print view.
  unset($build['links']);

  // Pass a fully rendered variable.  This is modeled after the book module in core.
  $node->rendered = drupal_render($build);
  drupal_add_http_header('Content-Type', 'text/html; charset=utf-8', FALSE);
  return theme('recipe_html_page', array(
    'node' => $node,
  ));
}

/**
 * Implements hook_theme().
 */
function recipe_html_theme() {
  return array(
    'recipe_html_page' => array(
      'template' => 'recipe_html_node',
      'variables' => array(
        'node' => NULL,
      ),
    ),
  );
}
function template_preprocess_recipe_html_page(&$variables) {
  $css_path = drupal_get_path("module", "recipe") . "/modules/recipe_html.css";
  $variables['styles'] = '<style type="text/css" media="all">@import url("' . file_create_url($css_path) . '");</style>';
  $variables['title'] = check_plain($variables['node']->title);

  // 'contents' is already rendered.  Use the contents key so as not to confuse template authors.
  $variables['contents'] = $variables['node']->rendered;
}