printfriendly.module in PrintFriendly & PDF 7.2
Same filename and directory in other branches
Adds PrintFriendly button to chosen node types and provides a block.
File
printfriendly.moduleView source
<?php
/**
* @file
* Adds PrintFriendly button to chosen node types and provides a block.
*/
/**
* Implements hook_help().
*/
function printfriendly_help($path) {
switch ($path) {
case 'admin/config/services/printfriendly':
case 'admin/help#printfriendly':
return t('PrintFriendly module lets you include a link to let your site users to quickly print, email or download the page as a PDF file using !printfriendly service.', array(
'!printfriendly' => '<a href="http://www.printfriendly.com">PrintFriendly.com</a>',
));
}
}
/**
* Implements hook_permission().
*/
function printfriendly_permission() {
return array(
'administer printfriendly' => array(
'title' => t('Administer PrintFriendly'),
'description' => t('Configure how PrintFriendly is used on the site.'),
),
'access printfriendly' => array(
'title' => t('Access PrintFriendly'),
'description' => t('Users with this permission will be able to use and see PrintFriendly.'),
),
);
}
/**
* Implements hook_menu().
*/
function printfriendly_menu() {
$items['admin/config/services/printfriendly'] = array(
'title' => 'PrintFriendly',
'description' => 'Provides the configuration options for how PrintFriendly operates on the site.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'printfriendly_admin_settings',
),
'access arguments' => array(
'administer printfriendly',
),
'file' => 'printfriendly.admin.inc',
);
return $items;
}
/**
* Implements hook_block_info().
*/
function printfriendly_block_info() {
$blocks['printfriendly'] = array(
'info' => t('PrintFriendly Widget'),
'cache' => DRUPAL_CACHE_GLOBAL,
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function printfriendly_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'printfriendly':
$block['subject'] = t('PrintFriendly');
$block['content'] = printfriendly_create_button();
break;
}
return $block;
}
/**
* Implements hook_node_view().
*/
function printfriendly_node_view($node, $view_mode) {
if (in_array($view_mode, array_filter(variable_get('printfriendly_display', array(
'full',
))))) {
if (in_array($node->type, variable_get('printfriendly_types', array()), TRUE) && user_access('access printfriendly')) {
if ($view_mode == 'teaser') {
$block = printfriendly_create_button('node/' . $node->nid, FALSE);
}
else {
$block = printfriendly_create_button();
$js = key($block['#attached']['js']);
drupal_add_js($js, $block['#attached']['js'][$js]);
}
$links['printfriendly'] = $block;
$node->content['links']['printfriendly'] = array(
'#theme' => 'links',
'#links' => $links,
'#attributes' => array(
'class' => array(
'links',
'inline',
),
),
);
}
}
}
/**
* Register module with views api.
*/
function printfriendly_views_api() {
return array(
'api' => '3.0',
'path' => drupal_get_path('module', 'printfriendly') . '/views',
);
}
/**
* Shared function generate code for printfriendly button for nodes and block.
*
* @param string $url
* Path to the page to pass to PrintFriendly.
* @param bool $popup
* Whether the JavaScript should be added or not (popup).
* @return string
* String containing html code for the button
*
*/
function printfriendly_create_button($url = NULL, $popup = TRUE) {
if (!$url && !is_numeric($url)) {
$url = $_GET['q'];
}
$query_string = $_GET;
unset($query_string['q']);
$url = url($url, array(
'absolute' => TRUE,
'query' => $query_string,
));
$image = variable_get('printfriendly_image', 'button-print-grnw20.png');
if ($image == '_text_') {
$link_content = '<span class="printfriendly-link printfriendly-text-link">';
$link_content .= check_plain(variable_get('printfriendly_text', t('Print this page')));
$link_content .= '</span>';
}
else {
$image = drupal_get_path('module', 'printfriendly') . '/images/' . variable_get('printfriendly_image', 'button-print-grnw20.png');
$link_content = theme('image', array(
'path' => $image,
'alt' => variable_get('printfriendly_description', t('Printer Friendly and PDF')),
));
}
$options = array(
'attributes' => array(
'class' => 'printfriendly',
'onclick' => 'window.print(); return false;',
'title' => variable_get('printfriendly_description', t('Printer Friendly and PDF')),
),
'html' => TRUE,
'query' => array(
'url' => $url,
),
'external' => TRUE,
);
if ($popup) {
$return = array(
'#markup' => l($link_content, 'http://www.printfriendly.com/print', $options),
);
if ($attach = _printfriendly_get_button_script()) {
$return['#attached']['js']['printfriendly_script'] = array(
'data' => $attach,
'type' => 'inline',
'scope' => 'footer',
);
}
return $return;
}
else {
unset($options['attributes']['onclick']);
return array(
'#markup' => l($link_content, 'http://www.printfriendly.com/print', $options),
);
}
}
function _printfriendly_get_button_script() {
$added =& drupal_static(__FUNCTION__, FALSE);
if ($added) {
return NULL;
}
$added = TRUE;
$print_settings = variable_get('printfriendly_options', array(
'pfdisableClickToDel' => 0,
'pfHideImages' => 0,
'pfImageDisplayStyle' => 'right',
'pfDisableEmail' => 0,
'pfDisablePDF' => 0,
'pfDisablePrint' => 0,
));
$script = '';
foreach ($print_settings as $setting => $value) {
$script .= 'var ' . $setting . ' = ';
if (is_numeric($value)) {
$script .= (int) $value;
}
else {
$script .= "'" . (string) addslashes($value) . "'";
}
$script .= ';';
}
$script .= <<<SNIPPET
(function(){var js, pf;pf = document.createElement('script');pf.type = 'text/javascript';
if ('https:' === document.location.protocol){
js='https://pf-cdn.printfriendly.com/ssl/main.js'
}else{
js='http://cdn.printfriendly.com/printfriendly.js'
}
pf.src=js;document.getElementsByTagName('head')[0].appendChild(pf)})();
SNIPPET;
return $script;
}
Functions
Name | Description |
---|---|
printfriendly_block_info | Implements hook_block_info(). |
printfriendly_block_view | Implements hook_block_view(). |
printfriendly_create_button | Shared function generate code for printfriendly button for nodes and block. |
printfriendly_help | Implements hook_help(). |
printfriendly_menu | Implements hook_menu(). |
printfriendly_node_view | Implements hook_node_view(). |
printfriendly_permission | Implements hook_permission(). |
printfriendly_views_api | Register module with views api. |
_printfriendly_get_button_script |