Skip to content
print.module 9.94 KiB
Newer Older
João Ventura's avatar
João Ventura committed
 * Displays Printer-friendly versions of Drupal pages.
 *
 * This is the core module of the PF package, with the Drupal hooks
 * and other administrative functions.
define('PRINT_LOGO_OPTIONS_DEFAULT', 1);
define('PRINT_LOGO_URL_DEFAULT', '');
define('PRINT_FOOTER_OPTIONS_DEFAULT', 1);
define('PRINT_FOOTER_USER_DEFAULT', '');
define('PRINT_CSS_DEFAULT', '');
define('PRINT_KEEP_THEME_CSS_DEFAULT', 0);
define('PRINT_URLS_DEFAULT', 1);
define('PRINT_URLS_ANCHORS_DEFAULT', 0);
define('PRINT_COMMENTS_DEFAULT', 0);
define('PRINT_NEWWINDOW_DEFAULT', 1);
define('PRINT_TYPE_URLLIST_DEFAULT', 1);
define('PRINT_TYPE_SYS_URLLIST_DEFAULT', 0);
João Ventura's avatar
João Ventura committed
define('PRINT_TYPE_LINK_TEXT_ENABLED_DEFAULT', 0);
define('PRINT_HTML_NEW_WINDOW_DEFAULT', 0);
define('PRINT_HTML_SENDTOPRINTER_DEFAULT', 0);
define('PRINT_HTML_WINDOWCLOSE_DEFAULT', 1);

define('PRINT_SOURCEURL_ENABLED_DEFAULT', 1);
define('PRINT_SOURCEURL_DATE_DEFAULT', 0);
define('PRINT_SOURCEURL_FORCENODE_DEFAULT', 0);

define('PRINT_ROBOTS_NOINDEX_DEFAULT', 1);
define('PRINT_ROBOTS_NOFOLLOW_DEFAULT', 1);
define('PRINT_ROBOTS_NOARCHIVE_DEFAULT', 0);

define('PRINT_LIB_PATH', 'sites/all/libraries');

/**
 * Implements hook_print_link().
 */
function print_print_link() {
  return array(
    'format' => 'html',
    'text' => t('Printer-friendly version'),
    'description' => t('Display a printer-friendly version of this page.'),
    'path' => 'print',
    'class' => 'print-page',
João Ventura's avatar
João Ventura committed
    'icon' => 'print_icon.png',
João Ventura's avatar
João Ventura committed
/**
 * Implements hook_print_new_window_alter().
 */
function print_print_new_window_alter(&$new_window, $format) {
  $new_window = variable_get('print_html_new_window', PRINT_HTML_NEW_WINDOW_DEFAULT);
}

 * Implements hook_permission().
function print_permission() {
  return array(
    'administer print' => array(
      'title' => t('Administer the module'),
      'description' => t('Perform maintenance tasks for the print module.'),
    ),
João Ventura's avatar
João Ventura committed
    'access print' => array(
      'title' => t('Access the printer-friendly page'),
      'description' => t('View the printer-friendly pages and the links to them in the original pages.'),
    ),
João Ventura's avatar
João Ventura committed
/**
 * Implements hook_theme().
João Ventura's avatar
João Ventura committed
 */
function print_theme() {
  return array(
      'variables' => array(
        'node' => NULL,
        'query' => NULL,
        'format' => '',
        'expand_css' => FALSE,
        'message' => '',
      ),
      'file' => 'print.pages.inc',
    ),
    'print_published' => array(
      'variables' => array(),
      'file' => 'print.pages.inc',
    ),
    'print_breadcrumb' => array(
      'variables' => array('node' => NULL),
      'file' => 'print.pages.inc',
    ),
    'print_footer' => array(
      'variables' => array(),
      'file' => 'print.pages.inc',
    ),
    'print_sourceurl' => array(
      'variables' => array('url' => '', 'node' => NULL, 'cid' => NULL),
      'file' => 'print.pages.inc',
    ),
    'print_url_list' => array(
      'variables' => array(),
      'file' => 'print.pages.inc',
 * Implements hook_preprocess_HOOK().
function print_preprocess_node(&$variables) {
  if (($variables['elements']['#view_mode'] == PRINT_VIEW_MODE) && isset($variables['elements']['#print_format'])) {
    $type = $variables['elements']['#node']->type;
    $format = $variables['elements']['#print_format'];
    $nid = $variables['elements']['#node']->nid;

    $variables['theme_hook_suggestions'][] = "node__print";
    $variables['theme_hook_suggestions'][] = "node__print__{$format}";
    $variables['theme_hook_suggestions'][] = "node__print__{$format}__node__{$type}";
    $variables['theme_hook_suggestions'][] = "node__print__{$format}__node__{$nid}";
 * Implements hook_menu().
function print_menu() {
João Ventura's avatar
João Ventura committed
  $link = print_print_link();
João Ventura's avatar
João Ventura committed
  $items[$link['path']] = array(
    'title' => 'Printer-friendly',
João Ventura's avatar
João Ventura committed
    'page callback' => 'print_controller_html',
    'access arguments' => array('access print'),
João Ventura's avatar
João Ventura committed
    'type' => MENU_CALLBACK,
    'file' => 'print.pages.inc',
João Ventura's avatar
João Ventura committed
  $items[$link['path'] . '/' . $link['path']] = array(
  $items['admin/config/user-interface/print'] = array(
    'title' => 'Printer, email and PDF versions',
    'description' => 'Adds a printer-friendly version link to content and administrative pages.',
    'page callback' => 'drupal_get_form',
João Ventura's avatar
João Ventura committed
    'page arguments' => array('print_html_settings'),
    'access arguments'  => array('administer print'),
    'file' => 'print.admin.inc',
João Ventura's avatar
João Ventura committed
  );
  $items['admin/config/user-interface/print/html'] = array(
João Ventura's avatar
João Ventura committed
    'title' => 'Web page',
    'weight' => 1,
João Ventura's avatar
João Ventura committed
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['admin/config/user-interface/print/common'] = array(
João Ventura's avatar
João Ventura committed
    'title' => 'Settings',
João Ventura's avatar
João Ventura committed
    'description' => 'Settings for the common functionalities for all the print sub-modules.',
João Ventura's avatar
João Ventura committed
    'page callback' => 'drupal_get_form',
    'page arguments' => array('print_main_settings'),
    'access arguments'  => array('administer print'),
João Ventura's avatar
João Ventura committed
    'weight' => 10,
    'type' => MENU_LOCAL_TASK,
    'file' => 'print.admin.inc',
/**
 * Implements hook_variable_info().
 */
function print_variable_info($options) {
  $link = print_print_link();

  $variable['print_html_link_text'] = array(
    'title' => t('Printer-friendly version'),
    'description' => t('Text used in the link to the printer-friendly version.'),
    'type' => 'string',
    'default' => t($link['text']),
  );

  return $variable;
}

 * Implements hook_block_info().
function print_block_info() {
  $block['print-top']['info'] = t('Most printed');
  $block['print-top']['cache'] = DRUPAL_CACHE_GLOBAL;
  return $block;
João Ventura's avatar
João Ventura committed

/**
 * Implements hook_block_view().
João Ventura's avatar
João Ventura committed
 */
function print_block_view($delta = '') {
  switch ($delta) {
    case 'print-top':
      $block['subject'] = t('Most printed');
      $result = db_query_range("SELECT path FROM {print_page_counter} LEFT JOIN {node} n ON path = CONCAT('node/', n.nid) WHERE status <> 0 OR status IS NULL ORDER BY totalcount DESC", 0, 3)
        ->fetchAll();
          $items[] = l(_print_get_title($obj->path), $obj->path);
        $block['content'] = theme('item_list', array('items' => $items, 'type' => 'ul'));
João Ventura's avatar
João Ventura committed
/**
 * Implements hook_help().
 */
function print_help($path, $arg) {
João Ventura's avatar
João Ventura committed
  switch ($path) {
    case 'admin/help#print':
      // Return a line-break version of the module README.
      $ret = _filter_autop(file_get_contents(drupal_get_path('module', 'print') . '/README.txt'));
João Ventura's avatar
João Ventura committed
  }
  return $ret;
 * Implements hook_node_delete().
function print_node_delete($node) {
  db_delete('print_page_counter')
    ->condition('path', 'node/' . $node->nid)
    ->execute();
 * Implements hook_entity_info_alter().
function print_entity_info_alter(&$info) {
  // Add the 'Print' view mode for nodes.
  $info['node']['view modes'] += array(
      'label' => t('Print'),
      'custom settings' => FALSE,
  // Add the 'Print' view mode for field_collections.
  if (module_exists('field_collection')) {
    $info['field_collection_item']['view modes'] += array(
        'label' => t('Print'),
        'custom settings' => FALSE,
      ),
    );
  }
 * Auxiliary function to discover a given page's title.
João Ventura's avatar
João Ventura committed
 * @param string $path
 *   Path of the page being identified.
João Ventura's avatar
João Ventura committed
 *
 * @return string
 *   string with the page's title
 */
function _print_get_title($path) {
  $path = drupal_get_normal_path($path);
  $nid = preg_replace('!^node/!', '', $path);
    return db_query("SELECT title FROM {node} WHERE nid = :nid", array(':nid' => $nid))
      ->fetchField();
    // Not a node, try to get title from the menu system.
    $menu_item = menu_get_item($path);
    if (!empty($menu_item['title'])) {
      return $menu_item['title'];
    }
João Ventura's avatar
João Ventura committed
    elseif (drupal_substr($menu_item['page_callback'], 0, 6) == 'views_') {
      // It's a view, load the view to have access to the title.
      $view = views_get_view($menu_item['page_arguments']['0']);
      return $view->get_title();
    }
    else {
      return NULL;
    }
 * Auxiliary function to display a formatted Printer-friendly link.
João Ventura's avatar
João Ventura committed
 *
João Ventura's avatar
João Ventura committed
 * Function made available so that developers may call this function from
 * their defined pages/blocks.
 *
João Ventura's avatar
João Ventura committed
 * @param string $path
 *   path to be used in the link. If not specified, the current URL is used.
João Ventura's avatar
João Ventura committed
 * @param object $node
 *   node object, to be used in checking node access. If the path argument is
 *   not provided, the path used will be node/nid.
 * @param string $location
 *   Where in the page where the link is being inserted ('link', 'corner',
João Ventura's avatar
João Ventura committed
 *
 * @return string
 *   HTML link to the printer-friendly page
function print_insert_link($path = NULL, $node = NULL, $location = '') {
  if (function_exists('print_ui_insert_link')) {
    return print_ui_insert_link(print_print_link(), array(
      'path' => $path,
      'node' => $node,
      'location' => $location,
    ));
João Ventura's avatar
João Ventura committed
/**
 * Check if the link to the PF version is allowed depending on the settings.
João Ventura's avatar
João Ventura committed
 * @param array $args
 *   Array containing the possible parameters:
 *    view_mode, node, type, path.
João Ventura's avatar
João Ventura committed
 *
 * @return bool
João Ventura's avatar
João Ventura committed
 *   FALSE if not allowed, TRUE otherwise
 */
function print_link_allowed($args) {
  return (user_access('access print'));
}

/**
 * Implements hook_contextual_links_view_alter().
 */
function print_contextual_links_view_alter(&$element, $items) {
  // Hide all contextual links.
  if (preg_match('!^print!', $_GET['q'])) {
    unset($element['#links']);
  }
/**
 * Implements hook_views_api().
 */
function print_views_api() {
  return array(
    'api' => 2.0,
    'path' => drupal_get_path('module', 'print'),
  );
}