You are here

function printfriendly_create_button in PrintFriendly & PDF 7.4

Same name and namespace in other branches
  1. 8.3 printfriendly.module \printfriendly_create_button()
  2. 8 printfriendly.module \printfriendly_create_button()
  3. 8.2 printfriendly.module \printfriendly_create_button()
  4. 7.5 printfriendly.module \printfriendly_create_button()
  5. 7 printfriendly.module \printfriendly_create_button()
  6. 7.2 printfriendly.module \printfriendly_create_button()
  7. 7.3 printfriendly.module \printfriendly_create_button()

Shared function generate code for printfriendly button for nodes and block.

Parameters

string $url: Path to the page to pass to PrintFriendly.

bool $popup: Whether the JavaScript should be added or not (popup).

Return value

string String containing html code for the button

3 calls to printfriendly_create_button()
printfriendly_block_view in ./printfriendly.module
Implements hook_block_view().
printfriendly_node_view in ./printfriendly.module
Implements hook_node_view().
printfriendly_views_handler_field_pfbutton::render in views/printfriendly_views_handler_field_pfbutton.inc
Render function: return html output.

File

./printfriendly.module, line 182
Adds PrintFriendly button to chosen node types and provides a block.

Code

function printfriendly_create_button($url = NULL, $popup = TRUE) {
  printfriendly_upgrade_db();
  if (!$url && !is_numeric($url)) {
    $url = $_GET['q'];
  }
  $query_string = $_GET;

  // Attach JS custom settings
  $custom_js = "var pfHeaderImgUrl = '" . variable_get('printfriendly_page_custom_header') . "';";
  $custom_js .= "var pfHeaderTagline = '" . variable_get('printfriendly_tagline') . "';";
  $custom_js .= "var pfdisableClickToDel  = '" . variable_get('printfriendly_click_delete') . "';";
  $custom_js .= "var pfHideImages = " . variable_get('printfriendly_images') . ";";
  $custom_js .= "var pfImageDisplayStyle = '" . variable_get('printfriendly_image_style') . "';";
  $custom_js .= "var pfDisablePDF = " . variable_get('printfriendly_pdf') . ";";
  $custom_js .= "var pfDisableEmail = " . variable_get('printfriendly_email') . ";";
  $custom_js .= "var pfDisablePrint = " . variable_get('printfriendly_print') . ";";
  $custom_js .= "var pfCustomCSS = '" . variable_get('printfriendly_custom_css') . "';";
  $custom_js .= "var pfPlatform = 'Drupal 7';";
  unset($query_string['q']);
  $url = url($url, array(
    'absolute' => TRUE,
    'query' => $query_string,
  ));

  // Use schema less URLs to load all PF Assets
  $js = '//cdn.printfriendly.com/printfriendly.js';
  if (variable_get('printfriendly_image') == 'custom-button-img-url') {
    $image = variable_get('custom_button_img_url', '');
  }
  else {
    $image_name = variable_get('printfriendly_image', 'button-print-grnw20.png');
    if (strpos($image_name, 'button') !== FALSE) {
      $folder = 'buttons';
    }
    else {
      $folder = 'icons';
    }
    $image = '//cdn.printfriendly.com/' . $folder . '/' . $image_name;
  }
  $link_content = theme('image', array(
    'path' => $image,
    'alt' => variable_get('printfriendly_description', t('Print Friendly, PDF & Email')),
  ));
  $options = array(
    'attributes' => array(
      'class' => 'printfriendly',
      'onclick' => 'window.print(); return false;',
      'title' => variable_get('printfriendly_description', t('Print Friendly, PDF & Email')),
    ),
    'html' => TRUE,
    'query' => array(
      'url' => $url,
    ),
    'external' => TRUE,
  );
  if ($popup) {
    return array(
      '#markup' => l($link_content, 'https://www.printfriendly.com/print', $options),
      '#attached' => array(
        'js' => array(
          $custom_js => array(
            'type' => 'inline',
            'scope' => 'footer',
          ),
          $js => array(
            'type' => 'external',
            'scope' => 'footer',
          ),
        ),
      ),
    );
  }
  else {
    unset($options['attributes']['onclick']);
    return array(
      '#markup' => l($link_content, 'https://www.printfriendly.com/print', $options),
    );
  }
}