1

In my Drupal 7 site I use the Printer, email and PDF versions module. I want to show the printer link on some specific pages of a specific content type. So I have enabled the link in that content type, which include a boolean field (field_printable) where the admin should be able to choose to include the link or not. In my template.php file I try to figure out how to work this out. This is what I have got so far in my template.php file:

function mytheme_preprocess_node(&$variables) {
    if($variables['type'] == "mycontenttype"){
        // boolean field that returns 1 if checked
        if($node->field_printable['und'][0]['value'] == 1){
                 what to put here ...?
        }
    }
}

I should probably use print_insert_link(); to insert the link, but I cannot figure out how. Can anyone point me in the right direction? I have spent hours googling on similar questions, but I am stuck right now.

Any help would be very appreciated, thanks in advance.

Edited: Screen goes blank after I try this...

function mytheme_preprocess_node(&$variables) {
    if($node->nid == 408){
        $variables['print_custom_link'] = print_insert_link();
    }
}

I also added this in my node template file:

  print render($content);
  if(!empty($print_custom_link)){
      print render($print_custom_link);
  }

2 Answers 2

2

OK, so I thought it could be a good idea to post an answer to my own question, in case others struggling with the same problem is looking at this post. And thanks again Laurent!

Turns out that my boolean field 'field_printable" is available directly in $variables. And $node was not available at all.

So in my template.php I ended up with this:

function mytheme_preprocess_node(&$variables) {
    if($variables['type'] == "mycontentpage"){
        if($variables['field_printable']['und'][0]['value'] == 1){
                $variables['print_custom_link'] = print_insert_link();
        }
    }
}

In my node template (right after the row "print render($content);"):

if(!empty($print_custom_link)){
    print $print_custom_link;
}

That's it. Now it works exactly as I wanted :-)

1

The fist step should be to configure your node type to be printable in structure>content type>the node type you want to print. Then by default a print link will appears when you render your node content.

If you want to display a print link in a particular area and/or for a specific node (or even a view) then you can decide to use print_insert_link() to display the print link : https://www.drupal.org/node/306888

6
  • Thanks for your answer. Yes, I have already done all that. The problem is that I only want to show the link in 5 specific nodes of that specific content type, that in total have about 20 nodes, And I cannot figure out how to use print_insert_link()... I keep struggling :-)
    – TBJ
    Jan 11, 2016 at 8:11
  • Then in your them_preprocess_node function test if the $node->nid is in the list of nids targets. If they are : save the value of print_insert_link() in $variables['print_custom_link'] and in your node template print $print_custom_link if not empty. Jan 11, 2016 at 8:22
  • Sorry to bother you like this,,, When I try to open a page in the specific content typem the screen goes blank. Can you see what I do wrong in the code above (I edited my question).
    – TBJ
    Jan 11, 2016 at 8:55
  • Hmm I guess you don t need to render $print_custom_link because it is allready rendered html. If you want to have errors infos, activate dblog module and check under report>recent log message after having the blank page. Jan 11, 2016 at 10:17
  • Thanks a lot... In the database logging module there is an error message that states that $node is un undefined variable... so I added a check to see if node is set , like this: if (isset($node) && count($node->field_adds) != 0){ ... } I keep investigating .... :-) thanks a lot anyway, you have been a great help.
    – TBJ
    Jan 11, 2016 at 11:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.