4 * @defgroup print Printer, email 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_KEEP_THEME_CSS_DEFAULT', 0);
50 define('PRINT_URLS_DEFAULT', 1);
51 define('PRINT_URLS_ANCHORS_DEFAULT', 0);
52 define('PRINT_COMMENTS_DEFAULT', 0);
53 define('PRINT_NEWWINDOW_DEFAULT', 1);
55 define('PRINT_HTML_LINK_POS_DEFAULT', 'link');
56 define('PRINT_HTML_LINK_TEASER_DEFAULT', 0);
57 define('PRINT_HTML_SHOW_LINK_DEFAULT', 1);
58 define('PRINT_HTML_NODE_LINK_VISIBILITY_DEFAULT', 0);
59 define('PRINT_HTML_NODE_LINK_PAGES_DEFAULT', '');
60 define('PRINT_HTML_LINK_CLASS_DEFAULT', 'print-page');
61 define('PRINT_HTML_SYS_LINK_VISIBILITY_DEFAULT', 1);
62 define('PRINT_HTML_SYS_LINK_PAGES_DEFAULT', '');
63 define('PRINT_HTML_LINK_USE_ALIAS_DEFAULT', 0);
64 define('PRINT_HTML_BOOK_LINK_DEFAULT', 1);
65 define('PRINT_HTML_NEW_WINDOW_DEFAULT', 0);
66 define('PRINT_HTML_SENDTOPRINTER_DEFAULT', 0);
67 define('PRINT_HTML_WINDOWCLOSE_DEFAULT', 1);
69 define('PRINT_SOURCEURL_ENABLED_DEFAULT', 1);
70 define('PRINT_SOURCEURL_DATE_DEFAULT', 0);
71 define('PRINT_SOURCEURL_FORCENODE_DEFAULT', 0);
73 define('PRINT_ROBOTS_NOINDEX_DEFAULT', 1);
74 define('PRINT_ROBOTS_NOFOLLOW_DEFAULT', 1);
75 define('PRINT_ROBOTS_NOARCHIVE_DEFAULT', 0);
77 define('PRINT_TYPE_SHOW_LINK_DEFAULT', 1);
78 define('PRINT_TYPE_COMMENT_LINK_DEFAULT', 0);
79 define('PRINT_TYPE_URLLIST_DEFAULT', 1);
80 define('PRINT_TYPE_SYS_URLLIST_DEFAULT', 0);
82 define('PRINT_ALLOW_NORMAL_LINK', 1);
83 define('PRINT_ALLOW_BOOK_LINK', 2);
84 define('PRINT_TYPE_FIELDS_WEIGHT', 30);
87 * Implements hook_permission().
89 function print_permission() {
91 'administer print' => array(
92 'title' => t('Administer the module'),
93 'description' => t('Perform maintenance tasks for the print module.'),
95 'node-specific print configuration' => array(
96 'title' => t('Node-specific configuration'),
97 'description' => t('Enable access to the per-node settings.'),
99 'access print' => array(
100 'title' => t('Access the printer-friendly page'),
101 'description' => t('View the printer-friendly pages and the links to them in the original pages.'),
107 * Implements hook_theme().
109 function print_theme() {
111 'print_format_link' => array(
112 'variables' => array(),
115 'variables' => array('print' => array(), 'type' => PRINT_HTML_FORMAT
, 'node' => NULL
),
116 'template' => 'print',
122 * Implements hook_preprocess_HOOK().
124 function print_preprocess_node(&$variables) {
125 if ($variables['elements']['#view_mode'] == 'print') {
126 $type = $variables['elements']['#node']->type
;
127 $format = $variables['elements']['#print_format'];
128 $nid = $variables['elements']['#node']->nid
;
130 $variables['theme_hook_suggestions'][] = "node__print";
131 $variables['theme_hook_suggestions'][] = "node__print__{$format}";
132 $variables['theme_hook_suggestions'][] = "node__print__{$format}__node__{$type}";
133 $variables['theme_hook_suggestions'][] = "node__print__{$format}__node__{$nid}";
138 * Implements hook_preprocess_HOOK().
140 function print_preprocess_print(&$variables) {
141 static
$hooks = NULL
;
142 if (!isset($hooks)) {
143 drupal_theme_initialize();
144 $hooks = theme_get_registry();
147 $variables['page']['#show_messages'] = FALSE
;
149 // Stolen from theme() so that ALL preprocess functions are called
151 $info = $hooks[$hook];
152 if (isset($info['preprocess functions']) || isset($info['process functions'])) {
153 foreach (array('preprocess functions', 'process functions') as
$phase) {
154 if (!empty($info[$phase])) {
155 foreach ($info[$phase] as
$processor_function) {
156 if (function_exists($processor_function)) {
157 // We don't want a poorly behaved process function changing $hook.
159 $processor_function($variables, $hook_clone);
166 $format = $variables['type'];
167 $type = (isset($variables['node']->type
)) ?
$variables['node']->type
: '';
168 $nid = (isset($variables['node']->nid
)) ?
$variables['node']->nid
: '';
170 $variables['theme_hook_suggestions'] = array();
171 $variables['theme_hook_suggestions'][] = "print__node__{$type}";
172 $variables['theme_hook_suggestions'][] = "print__node__{$type}__{$nid}";
173 $variables['theme_hook_suggestions'][] = "print__{$format}";
174 $variables['theme_hook_suggestions'][] = "print__{$format}__node__{$type}";
175 $variables['theme_hook_suggestions'][] = "print__{$format}__node__{$type}__{$nid}";
179 * Implements hook_menu().
181 function print_menu() {
184 $items[PRINT_PATH
] = array(
185 'title' => 'Printer-friendly',
186 'page callback' => 'print_controller_html',
187 'access arguments' => array('access print'),
188 'type' => MENU_CALLBACK
,
189 'file' => 'print.pages.inc',
191 $items[PRINT_PATH .
'/' . PRINT_PATH
] = array(
192 'access callback' => FALSE
,
194 $items['admin/config/user-interface/print'] = array(
195 'title' => 'Printer, email and PDF versions',
196 'description' => 'Adds a printer-friendly version link to content and administrative pages.',
197 'page callback' => 'drupal_get_form',
198 'page arguments' => array('print_html_settings'),
199 'access arguments' => array('administer print'),
200 'file' => 'print.admin.inc',
202 $items['admin/config/user-interface/print/html'] = array(
203 'title' => 'Web page',
205 'type' => MENU_DEFAULT_LOCAL_TASK
,
207 $items['admin/config/user-interface/print/html/options'] = array(
208 'title' => 'Options',
210 'type' => MENU_DEFAULT_LOCAL_TASK
,
212 $items['admin/config/user-interface/print/html/strings'] = array(
213 'title' => 'Text strings',
214 'description' => 'Override the user-facing strings used in the printer-friendly version.',
215 'page callback' => 'drupal_get_form',
216 'page arguments' => array('print_html_strings_settings'),
217 'access arguments' => array('administer print'),
219 'type' => MENU_LOCAL_TASK
,
220 'file' => 'print.admin.inc',
222 $items['admin/config/user-interface/print/common'] = array(
223 'title' => 'Settings',
224 'description' => 'Settings for the common functionalities for all the print sub-modules.',
225 'page callback' => 'drupal_get_form',
226 'page arguments' => array('print_main_settings'),
227 'access arguments' => array('administer print'),
229 'type' => MENU_LOCAL_TASK
,
230 'file' => 'print.admin.inc',
232 $items['admin/config/user-interface/print/common/options'] = array(
233 'title' => 'Options',
235 'type' => MENU_DEFAULT_LOCAL_TASK
,
237 $items['admin/config/user-interface/print/common/strings'] = array(
238 'title' => 'Text strings',
239 'description' => 'Override the user-facing strings used by the print module.',
240 'page callback' => 'drupal_get_form',
241 'page arguments' => array('print_main_strings_settings'),
242 'access arguments' => array('administer print'),
244 'type' => MENU_LOCAL_TASK
,
245 'file' => 'print.admin.inc',
252 * Implements hook_block_info().
254 function print_block_info() {
255 $block['print-links']['info'] = t('Printer, email and PDF versions');
256 $block['print-links']['cache'] = DRUPAL_CACHE_PER_PAGE
;
257 $block['print-top']['info'] = t('Most printed');
258 $block['print-top']['cache'] = DRUPAL_CACHE_GLOBAL
;
263 * Implements hook_block_view().
265 function print_block_view($delta = '') {
268 $nid = preg_replace('!^node/!', '', $_GET['q']);
269 if (ctype_digit($nid)) {
270 $node = node_load($nid);
271 if (!node_access('view', $node)) {
272 // If the user doesn't have access to the node, don't show any links
273 $block['content'] == '';
280 $block['content'] = '';
281 foreach (array('html' => 'print', 'mail' => 'print_mail', 'pdf' => 'print_pdf') as
$format => $module) {
282 $link_pos = variable_get('print_' .
$format .
'_link_pos', array(PRINT_HTML_LINK_POS_DEFAULT
=> PRINT_HTML_LINK_POS_DEFAULT
));
284 if (!(empty($link_pos['block']))) {
285 $func = $module .
'_insert_link';
287 if (function_exists($func)) {
288 $links = $func(NULL
, $node);
289 if (!empty($links)) {
290 $block['content'] .
= $links;
297 $block['subject'] = t('Most printed');
298 $result = db_query_range("SELECT path FROM {print_page_counter} LEFT JOIN {node} ON path = CONCAT('node/', node.nid) WHERE status <> 0 OR status IS NULL ORDER BY totalcount DESC", 0, 3)
300 if (count($result)) {
301 $block['content'] = '<div class="item-list"><ul>';
302 foreach ($result as
$obj) {
303 $block['content'] .
= '<li>' .
l(_print_get_title($obj->path
), $obj->path
) .
'</li>';
305 $block['content'] .
= '</ul></div>';
313 * Implements hook_node_view_alter().
315 function print_node_view_alter(&$build) {
316 if (isset($build['links']['book']['#links']['book_printer'])) {
317 $print_html_book_link = variable_get('print_html_book_link', PRINT_HTML_BOOK_LINK_DEFAULT
);
319 if ($print_html_book_link) {
320 $print_html_link_pos = variable_get('print_html_link_pos', array(PRINT_HTML_LINK_POS_DEFAULT
=> PRINT_HTML_LINK_POS_DEFAULT
));
322 if (!empty($print_html_link_pos['link'])) {
323 $format = theme('print_format_link');
325 switch ($print_html_book_link) {
327 $path = $build['links']['book']['#links']['book_printer']['href'];
330 $print_html_link_use_alias = variable_get('print_html_link_use_alias', PRINT_HTML_LINK_USE_ALIAS_DEFAULT
);
331 $path = ($print_html_link_use_alias && ($alias = drupal_lookup_path('alias', 'node/' .
$build['#node']->nid
))) ?
$alias : $build['#node']->nid
;
335 $build['links']['book']['#links']['book_printer'] = array(
336 'href' => PRINT_PATH .
'/' .
$path,
337 'title' => $format['text'],
338 'attributes' => $format['attributes'],
339 'html' => $format['html'],
343 unset($build['links']['book']['#links']['book_printer']);
350 * Implements hook_help().
352 function print_help($path, $arg) {
354 case
'admin/help#print':
355 // Return a line-break version of the module README
356 return _filter_autop(file_get_contents(drupal_get_path('module', 'print') .
'/README.txt'));
359 if ($path !== 'node/%') {
360 static
$output = FALSE
;
362 if ($output === FALSE
) {
365 $link = print_insert_link();
367 return "<span class='print-syslink'>$link</span>";
374 * Implements hook_node_view().
376 function print_node_view($node, $view_mode) {
377 $print_html_link_pos = variable_get('print_html_link_pos', array(PRINT_HTML_LINK_POS_DEFAULT
=> PRINT_HTML_LINK_POS_DEFAULT
));
378 $print_html_link_use_alias = variable_get('print_html_link_use_alias', PRINT_HTML_LINK_USE_ALIAS_DEFAULT
);
380 foreach (array('node', 'comment') as
$type) {
381 $allowed_type = print_link_allowed(array('type' => $type, 'node' => $node, 'view_mode' => $view_mode));
382 if (($allowed_type === PRINT_ALLOW_NORMAL_LINK
) && !isset($node->book
) && !empty($print_html_link_pos['link'])) {
383 drupal_add_css(drupal_get_path('module', 'print') .
'/css/printlinks.css');
385 $format = theme('print_format_link');
387 $path = (($print_html_link_use_alias) && ($alias = drupal_lookup_path('alias', 'node/' .
$node->nid
))) ?
$alias : $node->nid
;
389 $links['print_html'] = array(
390 'href' => PRINT_PATH .
'/' .
$path,
391 'title' => $format['text'],
392 'attributes' => $format['attributes'],
393 'html' => $format['html'],
394 'query' => print_query_string_encode($_GET, array('q')),
397 $link_content = array(
400 '#attributes' => array('class' => array('links', 'inline')),
403 if ($type == 'node') {
404 $node->content
['links']['print_html'] = $link_content;
406 elseif (($type == 'comment') && isset($node->content
['comments']['comments'])) {
407 foreach ($node->content
['comments']['comments'] as
$cid => $comment) {
408 if (is_numeric($cid)) {
409 $link_content['#links']['print_html']['query']['comment'] = $cid;
410 $node->content
['comments']['comments'][$cid]['links']['print_html'] = $link_content;
417 if ($view_mode == 'full') {
418 // Insert content corner links
419 $node->content
['print_links'] = array(
420 '#prefix' => '<span class="print-link">',
422 '#suffix' => '</span>',
425 if (!empty($print_html_link_pos['corner'])) {
426 $node->content
['print_links']['#markup'] .
= print_insert_link(NULL
, $node);
432 * Implements hook_node_load().
434 function print_node_load($nodes, $types) {
436 foreach ($nodes as
$node) {
440 $result = db_query('SELECT nid, link, comments, url_list FROM {print_node_conf} WHERE nid IN (:nids)', array(':nids' => $ids))->fetchAllAssoc('nid');
442 foreach ($nodes as
$node) {
443 $node->print_display
= isset($result[$node->nid
]) ?
intval($result[$node->nid
]->link) : variable_get('print_display_' .
$node->type
, PRINT_TYPE_SHOW_LINK_DEFAULT
);
444 $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
);
445 $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
);
450 * Implements hook_node_insert().
452 function print_node_insert($node) {
453 if (user_access('administer print') || user_access('node-specific print configuration')) {
454 if (!isset($node->print_display
)) $node->print_display
= variable_get('print_display_' .
$node->type
, PRINT_TYPE_SHOW_LINK_DEFAULT
);
455 if (!isset($node->print_display_comment
)) $node->print_display_comment
= variable_get('print_display_comment_' .
$node->type
, PRINT_TYPE_COMMENT_LINK_DEFAULT
);
456 if (!isset($node->print_display_urllist
)) $node->print_display_urllist
= variable_get('print_display_urllist_' .
$node->type
, PRINT_TYPE_URLLIST_DEFAULT
);
458 _print_node_conf_modify($node->nid
, $node->print_display
, $node->print_display_comment
, $node->print_display_urllist
);
463 * Implements hook_node_update().
465 function print_node_update($node) {
466 if (user_access('administer print') || user_access('node-specific print configuration')) {
467 if (!isset($node->print_display
)) $node->print_display
= variable_get('print_display_' .
$node->type
, PRINT_TYPE_SHOW_LINK_DEFAULT
);
468 if (!isset($node->print_display_comment
)) $node->print_display_comment
= variable_get('print_display_comment_' .
$node->type
, PRINT_TYPE_COMMENT_LINK_DEFAULT
);
469 if (!isset($node->print_display_urllist
)) $node->print_display_urllist
= variable_get('print_display_urllist_' .
$node->type
, PRINT_TYPE_URLLIST_DEFAULT
);
471 _print_node_conf_modify($node->nid
, $node->print_display
, $node->print_display_comment
, $node->print_display_urllist
);
476 * Implements hook_node_delete().
478 function print_node_delete($node) {
479 db_delete('print_node_conf')
480 ->condition('nid', $node->nid
)
482 db_delete('print_page_counter')
483 ->condition('path', 'node/' .
$node->nid
)
488 * Implements hook_form_alter().
490 function print_form_alter(&$form, &$form_state, $form_id) {
491 // Add the node-type settings option to activate the printer-friendly version link
492 if ((user_access('administer print') || user_access('node-specific print configuration')) &&
493 (($form_id == 'node_type_form') || !empty($form['#node_edit_form']))) {
494 $form['print'] = array(
495 '#type' => 'fieldset',
496 '#title' => t('Printer, email and PDF versions'),
497 '#collapsible' => TRUE
,
498 '#collapsed' => TRUE
,
499 '#weight' => PRINT_TYPE_FIELDS_WEIGHT
,
500 '#group' => 'additional_settings',
503 $form['print']['label'] = array(
505 '#markup' => '<p><strong>' .
t('Printer-friendly version') .
'</strong></p>',
508 $form['print']['print_display'] = array(
509 '#type' => 'checkbox',
510 '#title' => t('Show link'),
512 $form['print']['print_display_comment'] = array(
513 '#type' => 'checkbox',
514 '#title' => t('Show link in individual comments'),
516 $form['print']['print_display_urllist'] = array(
517 '#type' => 'checkbox',
518 '#title' => t('Show Printer-friendly URLs list'),
521 if ($form_id == 'node_type_form') {
522 $form['print']['print_display']['#default_value'] = variable_get('print_display_' .
$form['#node_type']->type
, PRINT_TYPE_SHOW_LINK_DEFAULT
);
523 $form['print']['print_display_comment']['#default_value'] = variable_get('print_display_comment_' .
$form['#node_type']->type
, PRINT_TYPE_COMMENT_LINK_DEFAULT
);
524 $form['print']['print_display_urllist']['#default_value'] = variable_get('print_display_urllist_' .
$form['#node_type']->type
, PRINT_TYPE_URLLIST_DEFAULT
);
527 $node = $form['#node'];
528 $form['print']['print_display']['#default_value'] = isset($node->print_display
) ?
$node->print_display
: variable_get('print_display_' .
$node->type
, PRINT_TYPE_SHOW_LINK_DEFAULT
);
529 $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
);
530 $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
);
536 * Implements hook_entity_info_alter().
538 function print_entity_info_alter(&$info) {
539 // Add the 'Print' view mode for nodes.
540 $info['node']['view modes'] += array(
542 'label' => t('Print'),
543 'custom settings' => FALSE
,
549 * Auxiliary function to discover a given page's title
552 * path of the page being identified
554 * string with the page's title
556 function _print_get_title($path) {
557 $path = drupal_get_normal_path($path);
558 $nid = preg_replace('!^node/!', '', $path);
559 if (ctype_digit($nid)) {
560 $res = db_query("SELECT title FROM {node} WHERE nid = :nid", array(':nid' => $nid))
564 $res = db_query("SELECT link_title FROM {menu_links} WHERE link_path = :link_path", array(':link_path' => $path))
571 * Modify the print_node_conf_table
573 * Update the print_node_conf table to reflect the given attributes.
574 * If updating to the default values, delete the record.
577 * value of the nid field (primary key)
579 * value of the link field (0 or 1)
581 * value of the comments field (0 or 1)
583 * value of the url_list field (0 or 1)
585 function _print_node_conf_modify($nid, $link, $comments, $url_list) {
586 db_merge('print_node_conf')
587 ->key(array('nid' => $nid))
590 'comments' => $comments,
591 'url_list' => $url_list,
597 * Auxiliary function to fill the Printer-friendly link attributes
600 * text to displayed by the link when hovering over it with the mouse
602 * class attribute to be used in the link
604 * if TRUE opens the target page in a new window
606 * array of formatted attributes
608 function print_fill_attributes($title = '', $class = '', $new_window = FALSE
) {
609 $print_newwindow = variable_get('print_newwindow', PRINT_NEWWINDOW_DEFAULT
);
610 $print_robots_noindex = variable_get('print_robots_noindex', PRINT_ROBOTS_NOINDEX_DEFAULT
);
612 $attributes = array();
613 $attributes['title'] = $title;
614 if (!empty($class)) {
615 $attributes['class'] = array($class);
619 switch ($print_newwindow) {
621 $attributes['target'] = '_blank';
624 $attributes['onclick'] = 'window.open(this.href); return false';
628 if (!empty($print_robots_noindex)) {
629 $attributes['rel'] = 'nofollow';
635 * Auxiliary function to set the link text and html flag
638 * type of link: 0 or 1 for a text-only link, 2 for icon-only and 3 for
641 * text to be displayed on the link to the printer-friendly page
643 * path to the icon file
645 * array with the link text and html flag
647 function _print_format_link_aux($type = 0, $text = '', $img = '') {
652 $text = theme('image', array('path' => $img, 'alt' => $text, 'title' => $text, 'attributes' => array('class' => array('print-icon'))));
655 $text = theme('image', array('path' => $img, 'alt' => $text, 'title' => $text, 'attributes' => array('class' => array('print-icon', 'print-icon-margin')))) .
$text;
663 return array('text' => $text,
669 * Format the Printer-friendly link
672 * array of formatted attributes
675 function theme_print_format_link() {
676 $print_html_link_class = variable_get('print_html_link_class', PRINT_HTML_LINK_CLASS_DEFAULT
);
677 $print_html_new_window = variable_get('print_html_new_window', PRINT_HTML_NEW_WINDOW_DEFAULT
);
678 $print_html_show_link = variable_get('print_html_show_link', PRINT_HTML_SHOW_LINK_DEFAULT
);
679 $print_html_link_text = filter_xss(variable_get('print_html_link_text', t('Printer-friendly version')));
681 $img = drupal_get_path('module', 'print') .
'/icons/print_icon.gif';
682 $title = t('Display a printer-friendly version of this page.');
683 $class = strip_tags($print_html_link_class);
684 $new_window = $print_html_new_window;
685 $format = _print_format_link_aux($print_html_show_link, $print_html_link_text, $img);
687 return array('text' => $format['text'],
688 'html' => $format['html'],
689 'attributes' => print_fill_attributes($title, $class, $new_window),
694 * Auxiliary function to display a formatted Printer-friendly link
696 * Function made available so that developers may call this function from
697 * their defined pages/blocks.
700 * path of the original page (optional). If not specified, the current URL
703 * an optional node object, to be used in defining the path, if used, the
704 * path argument is irrelevant
706 * string with the HTML link to the printer-friendly page
708 function print_insert_link($path = NULL
, $node = NULL
) {
709 if ($node !== NULL
) {
711 $path = 'node/' .
$nid;
712 $allowed_type = print_link_allowed(array('node' => $node));
715 if ($path === NULL
) {
716 $nid = preg_replace('!^node/([\d]+)!', '$1', $_GET['q']);
722 $allowed_type = print_link_allowed(array('path' => $path));
727 if ($allowed_type === PRINT_ALLOW_BOOK_LINK
) {
728 $path = 'book/export/html/' .
$nid;
731 if (variable_get('print_html_link_use_alias', PRINT_HTML_LINK_USE_ALIAS_DEFAULT
) && ($alias = drupal_lookup_path('alias', $path))) {
738 $path = PRINT_PATH .
'/' .
$path;
739 $query = print_query_string_encode($_GET, array('q'));
744 drupal_add_css(drupal_get_path('module', 'print') .
'/css/printlinks.css');
745 $format = theme('print_format_link');
746 return '<span class="print_html">' .
l($format['text'], $path, array('attributes' => $format['attributes'], 'query' => $query, 'absolute' => TRUE
, 'html' => $format['html'])) .
'</span>';
754 * Check if the provided page is enabled according to the visibility settings
757 * current visibility settings:
758 * 0 for show on every page except the listed pages
759 * 1 for show on only the listed pages
763 * TRUE if it is enabled, FALSE otherwise
765 function _print_page_match($visibility, $path, $pages) {
767 if ($visibility == 2) {
768 if (module_exists('php')) {
769 return php_eval($pages);
775 $alias = drupal_get_path_alias($path);
776 $page_match = drupal_match_path($path, $pages);
777 if ($alias != $path) {
778 $page_match = $page_match || drupal_match_path($alias, $pages);
781 return !($visibility xor
$page_match);
789 * Check if the link to the PF version is allowed depending on the settings
792 * array containing the possible parameters:
793 * teaser, node, type, path
795 * FALSE if not allowed
796 * PRINT_ALLOW_NORMAL_LINK if a normal link is allowed
797 * PRINT_ALLOW_BOOK_LINK if a link is allowed in a book node
799 function print_link_allowed($args) {
800 $view_mode = isset($args['view_mode']) ?
$args['view_mode'] : '';
801 if ((($view_mode == 'teaser') && !variable_get('print_html_link_teaser', PRINT_HTML_LINK_TEASER_DEFAULT
))
802 || !in_array($view_mode, array('full', 'teaser', '')) || !user_access('access print')) {
803 // If the teaser link is disabled or the user is not allowed
806 if (!empty($args['path'])) {
807 $nid = preg_replace('!^node/!', '', drupal_get_normal_path($args['path']));
808 if (ctype_digit($nid)) {
809 $args['node'] = node_load($nid);
812 if (!empty($args['node'])) {
813 static
$node_type = '';
815 $node = $args['node'];
816 if (isset($node->type
)) {
817 $node_type = $node->type
;
820 $print_html_node_link_visibility = variable_get('print_html_node_link_visibility', PRINT_HTML_NODE_LINK_VISIBILITY_DEFAULT
);
821 $print_html_node_link_pages = variable_get('print_html_node_link_pages', PRINT_HTML_NODE_LINK_PAGES_DEFAULT
);
823 if (!_print_page_match($print_html_node_link_visibility, "node/" .
$node->nid
, $print_html_node_link_pages)) {
824 // Page not in visibility list
827 elseif (isset($args['type']) && ($args['type'] == 'comment') && isset($node_type)) {
828 // Link is for a comment, return the configured setting
829 // Cache this statically to avoid duplicate queries for every comment.
830 static
$res = array();
831 if (!isset($res[$node->nid
])) {
832 $res[$node->nid
] = db_query("SELECT comments FROM {print_node_conf} WHERE nid = :nid", array(':nid' => $node->nid
))->fetchField();
834 $print_display_comment = ($res && ($res[$node->nid
] !== FALSE
)) ?
$res[$node->nid
] : variable_get('print_display_comment_' .
$node_type, PRINT_TYPE_COMMENT_LINK_DEFAULT
);
835 if ($print_display_comment) {
836 return PRINT_ALLOW_NORMAL_LINK
;
841 if (isset($node->print_display
) && !$node->print_display
) {
842 // Link for this node is disabled
845 elseif (isset($node->book
)) {
847 $print_html_book_link = variable_get('print_html_book_link', PRINT_HTML_BOOK_LINK_DEFAULT
);
848 switch ($print_html_book_link) {
850 if (user_access('access printer-friendly version')) {
851 return PRINT_ALLOW_BOOK_LINK
;
855 return PRINT_ALLOW_NORMAL_LINK
;
859 return PRINT_ALLOW_NORMAL_LINK
;
865 $print_html_sys_link_visibility = variable_get('print_html_sys_link_visibility', PRINT_HTML_SYS_LINK_VISIBILITY_DEFAULT
);
866 $print_html_sys_link_pages = variable_get('print_html_sys_link_pages', PRINT_HTML_SYS_LINK_PAGES_DEFAULT
);
868 return _print_page_match($print_html_sys_link_visibility, $_GET['q'], $print_html_sys_link_pages);
874 * Parse an array into a valid urlencoded query string.
876 * Modified from drupal_query_string_encode to prevent re-encoding of
877 * encoded original. (see #301192)
880 * The array to be processed e.g. $_GET
882 * The array filled with keys to be excluded.
884 * urlencoded string which can be appended to/as the URL query string
886 function print_query_string_encode($query, $exclude = array(), $parent = '') {
888 foreach ($query as
$key => $value) {
889 if (in_array($key, $exclude)) {
893 if (is_array($value)) {
894 $params[$key] = print_query_string_encode($value, $exclude, $key);
897 $params[$key] = $value;
901 return empty($params) ? NULL
: $params;
905 * Implements hook_contextual_links_view_alter().
907 function print_contextual_links_view_alter(&$element, $items) {
908 // Hide all contextual links
909 if (preg_match('!^print!', $_GET['q'])) {
910 unset($element['#links']);
915 * Callback function for the preg_replace_callback replacing spaces with %20
917 * Replace spaces in URLs with %20
919 * @param array $matches
920 * array with the matched tag patterns, usually <a...>+text+</a>
923 * tag with re-written URL
925 function _print_replace_spaces($matches) {
926 // first, split the html into the different tag attributes
927 $pattern = '!\s*(\w+\s*=\s*"(?:\\\"|[^"])*")\s*|\s*(\w+\s*=\s*\'(?:\\\\\'|[^\'])*\')\s*|\s*(\w+\s*=\s*\w+)\s*|\s+!';
928 $attribs = preg_split($pattern, $matches[1], -1, PREG_SPLIT_NO_EMPTY
| PREG_SPLIT_DELIM_CAPTURE
);
929 foreach ($attribs as
$key => $value) {
930 $attribs[$key] = preg_replace('!(\w)\s*=\s*(.*)!', '$1=$2', $value);
933 $size = count($attribs);
934 for ($i=1; $i < $size; $i++) {
935 // If the attribute is href or src, we may need to rewrite the URL in the value
936 if (preg_match('!^(?:href|src)\s*?=(.*)!i', $attribs[$i], $urls) > 0) {
937 $url = trim($urls[1], " \t\n\r\0\x0B\"'");
938 $new_url = str_replace(' ', '%20', $url);
939 $matches[1] = str_replace($url, $new_url, $matches[1]);
943 $ret = '<' .
$matches[1] .
'>';
944 if (count($matches) == 4) {
945 $ret .
= $matches[2] .
$matches[3];
952 * Implements hook_views_api().
954 function print_views_api() {
957 'path' => drupal_get_path('module', 'print'),