4 * @defgroup print Printer, e-mail and PDF versions
6 * Welcome to the print module developer's documentation. The interesting
7 * functions for themers are those that start with 'theme_'.
9 * - Printer-friendly pages
10 * - @link print.module Module main file @endlink
11 * - @link print.admin.inc Settings form @endlink
12 * - @link print.pages.inc HTML generation @endlink
13 * - @link print.install (Un)Install routines @endlink
14 * - @link print.tpl.php Page generation template @endlink
16 * - @link print_mail.module Module main file @endlink
17 * - @link print_mail.admin.inc Settings form @endlink
18 * - @link print_mail.inc Mail form and send mail routine @endlink
19 * - @link print_mail.install (Un)Install routines @endlink
21 * - @link print_pdf.module Module main file @endlink
22 * - @link print_pdf.admin.inc Settings form @endlink
23 * - @link print_pdf.pages.inc PDF generation @endlink
24 * - @link print_pdf.class.inc Auxiliary PHP5 class @endlink
25 * - @link print_pdf.class_php4.inc Auxiliary PHP4 class @endlink
26 * - @link print_pdf.install (Un)Install routines @endlink
31 * Displays Printer-friendly versions of Drupal pages.
33 * This is the core module of the PF package, with the Drupal hooks
34 * and other administrative functions.
39 define('PRINT_PATH', 'print');
41 define('PRINT_HTML_FORMAT', 'html');
42 define('PRINT_MAIL_FORMAT', 'mail');
43 define('PRINT_PDF_FORMAT', 'pdf');
44 define('PRINT_LOGO_OPTIONS_DEFAULT', 1);
45 define('PRINT_LOGO_URL_DEFAULT', '');
46 define('PRINT_FOOTER_OPTIONS_DEFAULT', 1);
47 define('PRINT_FOOTER_USER_DEFAULT', '');
48 define('PRINT_CSS_DEFAULT', '');
49 define('PRINT_URLS_DEFAULT', 1);
50 define('PRINT_URLS_ANCHORS_DEFAULT', 0);
51 define('PRINT_COMMENTS_DEFAULT', 0);
52 define('PRINT_NEWWINDOW_DEFAULT', 1);
54 define('PRINT_HTML_LINK_POS_DEFAULT', 'link');
55 define('PRINT_HTML_LINK_TEASER_DEFAULT', 0);
56 define('PRINT_HTML_SHOW_LINK_DEFAULT', 1);
57 define('PRINT_HTML_NODE_LINK_VISIBILITY_DEFAULT', 0);
58 define('PRINT_HTML_NODE_LINK_PAGES_DEFAULT', '');
59 define('PRINT_HTML_LINK_CLASS_DEFAULT', 'print-page');
60 define('PRINT_HTML_SYS_LINK_VISIBILITY_DEFAULT', 1);
61 define('PRINT_HTML_SYS_LINK_PAGES_DEFAULT', '');
62 define('PRINT_HTML_LINK_USE_ALIAS_DEFAULT', 0);
63 define('PRINT_HTML_BOOK_LINK_DEFAULT', 1);
64 define('PRINT_HTML_NEW_WINDOW_DEFAULT', 0);
65 define('PRINT_HTML_SENDTOPRINTER_DEFAULT', 0);
66 define('PRINT_HTML_WINDOWCLOSE_DEFAULT', 1);
68 define('PRINT_SOURCEURL_ENABLED_DEFAULT', 1);
69 define('PRINT_SOURCEURL_DATE_DEFAULT', 0);
70 define('PRINT_SOURCEURL_FORCENODE_DEFAULT', 0);
72 define('PRINT_ROBOTS_NOINDEX_DEFAULT', 1);
73 define('PRINT_ROBOTS_NOFOLLOW_DEFAULT', 1);
74 define('PRINT_ROBOTS_NOARCHIVE_DEFAULT', 0);
76 define('PRINT_TYPE_SHOW_LINK_DEFAULT', 1);
77 define('PRINT_TYPE_COMMENT_LINK_DEFAULT', 0);
78 define('PRINT_TYPE_URLLIST_DEFAULT', 1);
79 define('PRINT_TYPE_SYS_URLLIST_DEFAULT', 0);
81 define('PRINT_ALLOW_NORMAL_LINK', 1);
82 define('PRINT_ALLOW_BOOK_LINK', 2);
83 define('PRINT_TYPE_FIELDS_WEIGHT', 30);
86 * Implements hook_permission().
88 function print_permission() {
90 'administer print' => array(
91 'title' => t('Administer the module'),
92 'description' => t('Perform maintenance tasks for the print module.'),
94 'node-specific print configuration' => array(
95 'title' => t('Node-specific configuration'),
96 'description' => t('Enable access to the per-node settings.'),
98 'access print' => array(
99 'title' => t('Access the printer-friendly page'),
100 'description' => t('View the printer-friendly pages and the links to them in the original pages.'),
106 * Implements hook_theme().
108 function print_theme() {
110 'print_format_link' => array(
111 'variables' => array(),
113 'print_node' => array(
114 'variables' => array('node' => NULL
, 'teaser' => FALSE
, 'page' => FALSE
, 'type' => PRINT_HTML_FORMAT
),
115 'template' => 'print_node',
118 'variables' => array('print' => array(), 'type' => PRINT_HTML_FORMAT
, 'node' => NULL
),
119 'template' => 'print',
125 * Implements hook_preprocess_HOOK().
127 function print_preprocess_print_node(&$variables) {
128 static
$hooks = NULL
;
129 if (!isset($hooks)) {
130 drupal_theme_initialize();
131 $hooks = theme_get_registry();
134 //Stolen from theme() so that ALL preprocess functions are called
136 $info = $hooks[$hook];
137 if (isset($info['preprocess functions']) || isset($info['process functions'])) {
138 $variables['theme_hook_suggestions'] = array();
139 foreach (array('preprocess functions', 'process functions') as
$phase) {
140 if (!empty($info[$phase])) {
141 foreach ($info[$phase] as
$processor_function) {
142 if (function_exists($processor_function)) {
143 // We don't want a poorly behaved process function changing $hook.
145 $processor_function($variables, $hook_clone);
152 $variables['node'] = $variables['elements']['#node'];
153 $type = $variables['node']->type
;
154 $format = $variables['type'];
155 // template_preprocess_node($variables);
157 $variables['theme_hook_suggestions'][] = "node";
158 $variables['theme_hook_suggestions'][] = "node__{$type}";
159 $variables['theme_hook_suggestions'][] = "print_node";
160 // $variables['theme_hook_suggestions'][] = "print_node_{$format}";
161 // $variables['theme_hook_suggestions'][] = "print_node_{$format}.node__{$type}";
165 * Implements hook_preprocess_HOOK().
167 function print_preprocess_print(&$variables) {
168 static
$hooks = NULL
;
169 if (!isset($hooks)) {
170 drupal_theme_initialize();
171 $hooks = theme_get_registry();
174 $variables['page']['#show_messages'] = FALSE
;
176 //Stolen from theme() so that ALL preprocess functions are called
178 $info = $hooks[$hook];
179 if (isset($info['preprocess functions']) || isset($info['process functions'])) {
180 $variables['theme_hook_suggestions'] = array();
181 foreach (array('preprocess functions', 'process functions') as
$phase) {
182 if (!empty($info[$phase])) {
183 foreach ($info[$phase] as
$processor_function) {
184 if (function_exists($processor_function)) {
185 // We don't want a poorly behaved process function changing $hook.
187 $processor_function($variables, $hook_clone);
194 $format = $variables['type'];
195 $type = (isset($variables['node']->type
)) ?
$variables['node']->type
: '';
196 $nid = (isset($variables['node']->nid
)) ?
$variables['node']->nid
: '';
198 // $variables['theme_hook_suggestions'][] = "print";
199 $variables['theme_hook_suggestions'][] = "print__node__{$type}";
200 $variables['theme_hook_suggestions'][] = "print__node__{$type}__{$nid}";
201 $variables['theme_hook_suggestions'][] = "print__{$format}";
202 $variables['theme_hook_suggestions'][] = "print__{$format}__node__{$type}";
203 $variables['theme_hook_suggestions'][] = "print__{$format}__node__{$type}__{$nid}";
207 * Implements hook_menu().
209 function print_menu() {
212 $items[PRINT_PATH
] = array(
213 'title' => 'Printer-friendly',
214 'page callback' => 'print_controller_html',
215 'access arguments' => array('access print'),
216 'type' => MENU_CALLBACK
,
217 'file' => 'print.pages.inc',
219 $items[PRINT_PATH .
'/' . PRINT_PATH
] = array(
220 'access callback' => FALSE
,
222 $items['admin/config/user-interface/print'] = array(
223 'title' => 'Printer, e-mail and PDF versions',
224 'description' => 'Adds a printer-friendly version link to content and administrative pages.',
225 'page callback' => 'drupal_get_form',
226 'page arguments' => array('print_html_settings'),
227 'access arguments' => array('administer print'),
228 'file' => 'print.admin.inc',
230 $items['admin/config/user-interface/print/html'] = array(
231 'title' => 'Web page',
233 'type' => MENU_DEFAULT_LOCAL_TASK
,
235 $items['admin/config/user-interface/print/html/options'] = array(
236 'title' => 'Options',
238 'type' => MENU_DEFAULT_LOCAL_TASK
,
240 $items['admin/config/user-interface/print/html/strings'] = array(
241 'title' => 'Text strings',
242 'page callback' => 'drupal_get_form',
243 'page arguments' => array('print_html_strings_settings'),
244 'access arguments' => array('administer print'),
246 'type' => MENU_LOCAL_TASK
,
247 'file' => 'print.admin.inc',
249 $items['admin/config/user-interface/print/common'] = array(
250 'title' => 'Settings',
251 'page callback' => 'drupal_get_form',
252 'page arguments' => array('print_main_settings'),
253 'access arguments' => array('administer print'),
255 'type' => MENU_LOCAL_TASK
,
256 'file' => 'print.admin.inc',
258 $items['admin/config/user-interface/print/common/options'] = array(
259 'title' => 'Options',
261 'type' => MENU_DEFAULT_LOCAL_TASK
,
263 $items['admin/config/user-interface/print/common/strings'] = array(
264 'title' => 'Text strings',
265 'page callback' => 'drupal_get_form',
266 'page arguments' => array('print_main_strings_settings'),
267 'access arguments' => array('administer print'),
269 'type' => MENU_LOCAL_TASK
,
270 'file' => 'print.admin.inc',
277 * Implements hook_block_info().
279 function print_block_info() {
280 $block['print-links']['info'] = t('Printer, e-mail and PDF versions');
281 $block['print-links']['cache'] = DRUPAL_CACHE_PER_PAGE
;
282 $block['print-top']['info'] = t('Most printed');
283 $block['print-top']['cache'] = DRUPAL_CACHE_GLOBAL
;
288 * Implements hook_block_view().
290 function print_block_view($delta = '') {
293 $nid = preg_replace('!^node/!', '', $_GET['q']);
294 if (ctype_digit($nid)) {
295 $node = node_load($nid);
296 if (!node_access('view', $node)) {
297 // If the user doesn't have access to the node, don't show any links
298 $block['content'] == '';
305 $funcs = get_defined_functions();
306 $block['content'] = '';
307 foreach ($funcs['user'] as
$func) {
308 if (preg_match('!^print.*?_insert_link$!', $func)) {
309 $link = $func(NULL
, $node);
311 $block['content'] .
= $link;
317 $block['subject'] = t('Most printed');
318 $result = db_query_range("SELECT path FROM {print_page_counter} ORDER BY totalcount DESC", 0, 3)
320 if (count($result)) {
321 $block['content'] = '<div class="item-list"><ul>';
322 foreach ($result as
$obj) {
323 $block['content'] .
= '<li>' .
l(_print_get_title($obj->path
), $obj->path
) .
'</li>';
325 $block['content'] .
= '</ul></div>';
333 * Implements hook_node_view_alter().
335 function print_node_view_alter(&$build) {
336 if (isset($build['links']['book']['#links']['book_printer'])) {
337 $print_html_book_link = variable_get('print_html_book_link', PRINT_HTML_BOOK_LINK_DEFAULT
);
339 if ($print_html_book_link) {
340 $print_html_link_pos = variable_get('print_html_link_pos', array(PRINT_HTML_LINK_POS_DEFAULT
=> PRINT_HTML_LINK_POS_DEFAULT
));
342 if (!empty($print_html_link_pos['link'])) {
343 $format = theme('print_format_link');
345 switch ($print_html_book_link) {
347 $path = $build['links']['book']['#links']['book_printer']['href'];
350 $print_html_link_use_alias = variable_get('print_html_link_use_alias', PRINT_HTML_LINK_USE_ALIAS_DEFAULT
);
351 $path = ($print_html_link_use_alias && isset($node->path
)) ?
$node->path
: $node->nid
;
355 $build['links']['book']['#links']['book_printer'] = array(
356 'href' => PRINT_PATH .
'/' .
$path,
357 'title' => $format['text'],
358 'attributes' => $format['attributes'],
359 'html' => $format['html'],
363 unset($build['links']['book']['#links']['book_printer']);
370 * Implements hook_help().
372 function print_help($path, $arg) {
374 case
'admin/help#print':
375 // Return a line-break version of the module README
376 return _filter_autop(file_get_contents(drupal_get_path('module', 'print') .
'/README.txt'));
379 $print_html_link_pos = variable_get('print_html_link_pos', array(PRINT_HTML_LINK_POS_DEFAULT
=> PRINT_HTML_LINK_POS_DEFAULT
));
380 if (($path !== 'node/%') && !(empty($print_html_link_pos['link']) && empty($print_html_link_pos['corner']))) {
381 static
$output = FALSE
;
383 if ($output === FALSE
) {
386 $link = print_insert_link();
388 return "<span class='print-syslink'>$link</span>";
395 * Implements hook_node_view().
397 function print_node_view($node, $view_mode) {
399 $print_html_link_pos = variable_get('print_html_link_pos', array(PRINT_HTML_LINK_POS_DEFAULT
=> PRINT_HTML_LINK_POS_DEFAULT
));
400 $print_html_link_use_alias = variable_get('print_html_link_use_alias', PRINT_HTML_LINK_USE_ALIAS_DEFAULT
);
401 $allowed_type = print_link_allowed(array('type' => $type, 'node' => $node, 'view_mode' => $view_mode));
402 if (($allowed_type === PRINT_ALLOW_NORMAL_LINK
) && !isset($node->book
) && !empty($print_html_link_pos['link'])) {
403 drupal_add_css(drupal_get_path('module', 'print') .
'/css/printlinks.css');
405 $format = theme('print_format_link');
408 if ($type == 'comment') {
409 $query_arr['comment'] = $node->cid
;
411 $query = print_query_string_encode($query_arr, array('q'));
412 if (empty($query)) $query = NULL
;
414 if ($print_html_link_use_alias) {
415 $path = drupal_get_path_alias('node/' .
$node->nid
);
421 $links['print_html'] = array(
422 'href' => PRINT_PATH .
'/' .
$path,
423 'title' => $format['text'],
424 'attributes' => $format['attributes'],
425 'html' => $format['html'],
429 $node->content
['links']['print_html'] = array(
432 '#attributes' => array('class' => array('links', 'inline')),
436 if ($view_mode == 'full') {
437 // Insert content corner links
438 $node->content
['print_links'] = array(
439 '#prefix' => '<span class="print-link">',
441 '#suffix' => '</span>',
444 if (!empty($print_html_link_pos['corner'])) {
445 $node->content
['print_links']['#markup'] .
= print_insert_link(NULL
, $node);
451 * Implements hook_node_load().
453 function print_node_load($nodes, $types) {
455 foreach ($nodes as
$node) {
459 $result = db_query('SELECT nid, link, comments, url_list FROM {print_node_conf} WHERE nid IN (:nids)', array(':nids' => $ids))->fetchAllAssoc('nid');
461 foreach ($nodes as
$node) {
462 $node->print_display
= isset($result[$node->nid
]) ?
intval($result[$node->nid
]->link) : variable_get('print_display_' .
$node->type
, PRINT_TYPE_SHOW_LINK_DEFAULT
);
463 $node->print_display_comment
= isset($result[$node->nid
]) ?
intval($result[$node->nid
]->comments
) : variable_get('print_display_comment_' .
$node->type
, PRINT_TYPE_COMMENT_LINK_DEFAULT
);
464 $node->print_display_urllist
= isset($result[$node->nid
]) ?
intval($result[$node->nid
]->url_list
) : variable_get('print_display_urllist_' .
$node->type
, PRINT_TYPE_URLLIST_DEFAULT
);
469 * Implements hook_node_insert().
471 function print_node_insert($node) {
472 if (user_access('administer print') || user_access('node-specific print configuration')) {
473 if (!isset($node->print_display
) || $node->print_display
=== NULL
) {
474 $node->print_display
= variable_get('print_display_' .
$node->type
, PRINT_TYPE_SHOW_LINK_DEFAULT
);
476 if (!isset($node->print_display_comment
) || $node->print_display_comment
=== NULL
) {
477 $node->print_display_comment
= variable_get('print_display_comment_' .
$node->type
, PRINT_TYPE_COMMENT_LINK_DEFAULT
);
479 if (!isset($node->print_display_urllist
) || $node->print_display_urllist
=== NULL
) {
480 $node->print_display_urllist
= variable_get('print_display_urllist_' .
$node->type
, PRINT_TYPE_URLLIST_DEFAULT
);
483 _print_node_conf_modify($node->nid
, $node->print_display
, $node->print_display_comment
, $node->print_display_urllist
);
488 * Implements hook_node_update().
490 function print_node_update($node) {
491 if (user_access('administer print') || user_access('node-specific print configuration')) {
492 if ($node->print_display
=== NULL
) $node->print_display
= variable_get('print_display_' .
$node->type
, PRINT_TYPE_SHOW_LINK_DEFAULT
);
493 if ($node->print_display_comment
=== NULL
) $node->print_display_comment
= variable_get('print_display_comment_' .
$node->type
, PRINT_TYPE_COMMENT_LINK_DEFAULT
);
494 if ($node->print_display_urllist
=== NULL
) $node->print_display_urllist
= variable_get('print_display_urllist_' .
$node->type
, PRINT_TYPE_URLLIST_DEFAULT
);
496 _print_node_conf_modify($node->nid
, $node->print_display
, $node->print_display_comment
, $node->print_display_urllist
);
501 * Implements hook_node_delete().
503 function print_node_delete($node) {
504 db_delete('print_node_conf')
505 ->condition('nid', $node->nid
)
507 db_delete('print_page_counter')
508 ->condition('path', 'node/' .
$node->nid
)
513 * Implements hook_form_alter().
515 function print_form_alter(&$form, &$form_state, $form_id) {
516 // Add the node-type settings option to activate the printer-friendly version link
517 if ((user_access('administer print') || user_access('node-specific print configuration')) &&
518 (($form_id == 'node_type_form') || !empty($form['#node_edit_form']))) {
519 $form['print'] = array(
520 '#type' => 'fieldset',
521 '#title' => t('Printer, e-mail and PDF versions'),
522 '#collapsible' => TRUE
,
523 '#collapsed' => TRUE
,
524 '#weight' => PRINT_TYPE_FIELDS_WEIGHT
,
525 '#group' => 'additional_settings',
528 $form['print']['label'] = array(
530 '#markup' => '<p><strong>' .
t('Printer-friendly version') .
'</strong></p>',
533 $form['print']['print_display'] = array(
534 '#type' => 'checkbox',
535 '#title' => t('Show link'),
537 $form['print']['print_display_comment'] = array(
538 '#type' => 'checkbox',
539 '#title' => t('Show link in individual comments'),
541 $form['print']['print_display_urllist'] = array(
542 '#type' => 'checkbox',
543 '#title' => t('Show Printer-friendly URLs list'),
546 if ($form_id == 'node_type_form') {
547 $form['print']['print_display']['#default_value'] = variable_get('print_display_' .
$form['#node_type']->type
, PRINT_TYPE_SHOW_LINK_DEFAULT
);
548 $form['print']['print_display_comment']['#default_value'] = variable_get('print_display_comment_' .
$form['#node_type']->type
, PRINT_TYPE_COMMENT_LINK_DEFAULT
);
549 $form['print']['print_display_urllist']['#default_value'] = variable_get('print_display_urllist_' .
$form['#node_type']->type
, PRINT_TYPE_URLLIST_DEFAULT
);
552 $node = $form['#node'];
553 $form['print']['print_display']['#default_value'] = isset($node->print_display
) ?
$node->print_display
: variable_get('print_display_' .
$node->type
, PRINT_TYPE_SHOW_LINK_DEFAULT
);
554 $form['print']['print_display_comment']['#default_value'] = isset($node->print_display_comment
) ?
$node->print_display_comment
: variable_get('print_display_comment_' .
$node->type
, PRINT_TYPE_COMMENT_LINK_DEFAULT
);
555 $form['print']['print_display_urllist']['#default_value'] = isset($node->print_display_urllist
) ?
$node->print_display_urllist
: variable_get('print_display_urllist_' .
$node->type
, PRINT_TYPE_URLLIST_DEFAULT
);
561 * Implements hook_entity_info_alter().
563 function print_entity_info_alter(&$info) {
564 // Add the 'Print' view mode for nodes.
565 $info['node']['view modes'] += array(
567 'label' => t('Print'),
568 'custom settings' => FALSE
,
574 * Auxiliary function to discover a given page's title
577 * path of the page being identified
579 * string with the page's title
581 function _print_get_title($path) {
582 $path = drupal_get_normal_path($path);
583 $nid = preg_replace('!^node/!', '', $path);
584 if (ctype_digit($nid)) {
585 $res = db_query("SELECT title FROM {node} WHERE nid = :nid", array(':nid' => $nid))
589 $res = db_query("SELECT link_title FROM {menu_links} WHERE link_path = :link_path", array(':link_path' => $path))
596 * Update the print_node_conf table to reflect the given attributes
597 * If updating to the default values, delete the record.
600 * value of the nid field (primary key)
602 * value of the link field (0 or 1)
604 * value of the comments field (0 or 1)
606 * value of the url_list field (0 or 1)
608 function _print_node_conf_modify($nid, $link, $comments, $url_list) {
609 db_merge('print_node_conf')
610 ->key(array('nid' => $nid))
613 'comments' => $comments,
614 'url_list' => $url_list,
620 * Auxiliary function to fill the Printer-friendly link attributes
623 * text to displayed by the link when hovering over it with the mouse
625 * class attribute to be used in the link
627 * if TRUE opens the target page in a new window
629 * array of formatted attributes
631 function print_fill_attributes($title = '', $class = '', $new_window = FALSE
) {
632 $print_newwindow = variable_get('print_newwindow', PRINT_NEWWINDOW_DEFAULT
);
633 $print_robots_noindex = variable_get('print_robots_noindex', PRINT_ROBOTS_NOINDEX_DEFAULT
);
635 $attributes = array();
636 $attributes['title'] = $title;
637 if (!empty($class)) {
638 $attributes['class'] = array($class);
642 switch ($print_newwindow) {
644 $attributes['target'] = '_blank';
647 $attributes['onclick'] = 'window.open(this.href); return false';
651 if (!empty($print_robots_noindex)) {
652 $attributes['rel'] = 'nofollow';
658 * Auxiliary function to set the link text and html flag
661 * type of link: 0 or 1 for a text-only link, 2 for icon-only and 3 for
664 * text to be displayed on the link to the printer-friendly page
666 * path to the icon file
668 * array with the link text and html flag
670 function _print_format_link_aux($type = 0, $text = '', $img = '') {
675 $text = theme('image', array('path' => $img, 'alt' => $text, 'title' => $text, 'attributes' => array('class' => array('print-icon'))));
678 $text = theme('image', array('path' => $img, 'alt' => $text, 'title' => $text, 'attributes' => array('class' => array('print-icon', 'print-icon-margin')))) .
$text;
686 return array('text' => $text,
692 * Format the Printer-friendly link
695 * array of formatted attributes
698 function theme_print_format_link() {
699 $print_html_link_class = variable_get('print_html_link_class', PRINT_HTML_LINK_CLASS_DEFAULT
);
700 $print_html_new_window = variable_get('print_html_new_window', PRINT_HTML_NEW_WINDOW_DEFAULT
);
701 $print_html_show_link = variable_get('print_html_show_link', PRINT_HTML_SHOW_LINK_DEFAULT
);
702 $print_html_link_text = filter_xss(variable_get('print_html_link_text', t('Printer-friendly version')));
704 $img = drupal_get_path('module', 'print') .
'/icons/print_icon.gif';
705 $title = t('Display a printer-friendly version of this page.');
706 $class = strip_tags($print_html_link_class);
707 $new_window = $print_html_new_window;
708 $format = _print_format_link_aux($print_html_show_link, $print_html_link_text, $img);
710 return array('text' => $format['text'],
711 'html' => $format['html'],
712 'attributes' => print_fill_attributes($title, $class, $new_window),
717 * Auxiliary function to display a formatted Printer-friendly link
719 * Function made available so that developers may call this function from
720 * their defined pages/blocks.
723 * path of the original page (optional). If not specified, the current URL
726 * an optional node object, to be used in defining the path, if used, the
727 * path argument is irrelevant
729 * string with the HTML link to the printer-friendly page
731 function print_insert_link($path = NULL
, $node = NULL
) {
732 if ($node !== NULL
) {
734 $path = 'node/' .
$nid;
735 $allowed_type = print_link_allowed(array('node' => $node));
738 if ($path === NULL
) {
739 $nid = preg_replace('!^node/!', '', $_GET['q']);
745 $allowed_type = print_link_allowed(array('path' => $path));
750 if ($allowed_type === PRINT_ALLOW_BOOK_LINK
) {
751 $path = 'book/export/html/' .
$nid;
754 if (variable_get('print_html_link_use_alias', PRINT_HTML_LINK_USE_ALIAS_DEFAULT
)) {
755 $path = drupal_get_path_alias($path);
761 $path = PRINT_PATH .
'/' .
$path;
762 $query = print_query_string_encode($_GET, array('q'));
770 drupal_add_css(drupal_get_path('module', 'print') .
'/css/printlinks.css');
771 $format = theme('print_format_link');
772 return '<span class="print_html">' .
l($format['text'], $path, array('attributes' => $format['attributes'], 'query' => $query, 'absolute' => TRUE
, 'html' => $format['html'])) .
'</span>';
780 * Determine if the provided page is enabled according to the visibility settings
783 * current visibility settings:
784 * 0 for show on every page except the listed pages
785 * 1 for show on only the listed pages
789 * TRUE if it is enabled, FALSE otherwise
791 function _print_page_match($visibility, $path, $pages) {
793 if ($visibility == 2) {
794 if (module_exists('php')) {
795 return php_eval($pages);
801 $alias = drupal_get_path_alias($path);
802 $page_match = drupal_match_path($path, $pages);
803 if ($alias != $path) {
804 $page_match = $page_match || drupal_match_path($alias, $pages);
807 return !($visibility xor
$page_match);
815 * Determine a the link to the PF version is allowed depending on all possible settings
818 * array containing the possible parameters:
819 * teaser, node, type, path
821 * FALSE if not allowed
822 * PRINT_ALLOW_NORMAL_LINK if a normal link is allowed
823 * PRINT_ALLOW_BOOK_LINK if a link is allowed in a book node
825 function print_link_allowed($args) {
826 $view_mode = isset($args['view_mode']) ?
$args['view_mode'] : '';
827 if ((($view_mode == 'teaser') && !variable_get('print_html_link_teaser', PRINT_HTML_LINK_TEASER_DEFAULT
))
828 || !in_array($view_mode, array('full', 'teaser', '')) || !user_access('access print')) {
829 // If the teaser link is disabled or the user is not allowed
832 if (!empty($args['path'])) {
833 $nid = preg_replace('!^node/!', '', drupal_get_normal_path($args['path']));
834 if (ctype_digit($nid)) {
835 $args['node'] = node_load($nid);
838 if (!empty($args['node'])) {
839 static
$node_type = '';
841 $node = $args['node'];
842 if (isset($node->type
)) {
843 $node_type = $node->type
;
846 $print_html_node_link_visibility = variable_get('print_html_node_link_visibility', PRINT_HTML_NODE_LINK_VISIBILITY_DEFAULT
);
847 $print_html_node_link_pages = variable_get('print_html_node_link_pages', PRINT_HTML_NODE_LINK_PAGES_DEFAULT
);
849 if (!_print_page_match($print_html_node_link_visibility, "node/" .
$node->nid
, $print_html_node_link_pages)) {
850 // Page not in visibility list
853 elseif (isset($args['type']) && ($args['type'] == 'comment') && isset($node_type)) {
854 // Link is for a comment, return the configured setting
855 // Cache this statically to avoid duplicate queries for every comment.
856 static
$res = array();
857 if (!isset($res[$node->nid
])) {
858 $res[$node->nid
] = db_query("SELECT comments FROM {print_node_conf} WHERE nid = :nid", array(':nid' => $node->nid
))
861 $print_display_comment = $res ?
intval($res[$node->nid
]->comments
) : variable_get('print_display_comment_' .
$node_type, PRINT_TYPE_COMMENT_LINK_DEFAULT
);
862 if ($print_display_comment) {
863 return PRINT_ALLOW_NORMAL_LINK
;
868 if (!$node->print_display
) {
869 // Link for this node is disabled
872 elseif (isset($node->book
)) {
874 $print_html_book_link = variable_get('print_html_book_link', PRINT_HTML_BOOK_LINK_DEFAULT
);
875 switch ($print_html_book_link) {
877 if (user_access('access printer-friendly version')) {
878 return PRINT_ALLOW_BOOK_LINK
;
882 return PRINT_ALLOW_NORMAL_LINK
;
886 return PRINT_ALLOW_NORMAL_LINK
;
892 $print_html_sys_link_visibility = variable_get('print_html_sys_link_visibility', PRINT_HTML_SYS_LINK_VISIBILITY_DEFAULT
);
893 $print_html_sys_link_pages = variable_get('print_html_sys_link_pages', PRINT_HTML_SYS_LINK_PAGES_DEFAULT
);
895 return _print_page_match($print_html_sys_link_visibility, $_GET['q'], $print_html_sys_link_pages);
901 * Parse an array into a valid urlencoded query string.
902 * Modified from drupal_query_string_encode to prevent re-encoding of
906 * The array to be processed e.g. $_GET
908 * The array filled with keys to be excluded.
910 * urlencoded string which can be appended to/as the URL query string
912 function print_query_string_encode($query, $exclude = array(), $parent = '') {
914 foreach ($query as
$key => $value) {
916 $key = $parent .
'[' .
$key .
']';
919 if (in_array($key, $exclude)) {
923 if (is_array($value)) {
924 $params[] = print_query_string_encode($value, $exclude, $key);
927 $params[] = $key .
'=' .
rawurlencode($value);
935 * Implements hook_contextual_links_view_alter().
937 function print_contextual_links_view_alter(&$element, $items) {
938 // Hide contextual links, always
939 $element['#attributes']['class'][] = 'element-hidden';